1.对象类型转换:
public class Test
{
public static void main(String[] args)
{
// int i = 10;
// long l = i;
// long l = 101;
// int i = (int)l;
// 从子类到父类的类型转换可以自动进行
// Student s = new Student();
// Person p = s;
// 父类到子类必须通过强转实现
// Person p = new Person();
// Student s = (Student) p;
}
}
2.equals、==:
练习:
public class Order
{
public Order(int order,String orderName)
{
this.order = order;
this.orderName = orderName;
}
int order;
String orderName;
public int getOrder() {
return order;
}
public void setOrder(int order) {
this.order = order;
}
public String getOrderName() {
return orderName;
}
public void setOrderName(String orderName) {
this.orderName = orderName;
}
@Override
public boolean equals(Object obj)
{
boolean flag = false;
if (obj instanceof Order)
{
Order o = (Order) obj;
if(this.order == o.order && this.orderName.equals(o.orderName))
{
flag = true;
}
}
return flag;
}
}
public class Test
{
public static void main(String[] args)
{
Order o1 = new Order(123,"a001");
Order o2 = new Order(123,"a001");
System.out.println(o1.equals(o2));
}
}
public class MyDate
{
public MyDate(int year,int month,int day)
{
this.year = year;
this.month = month;
this.day = day;
}
int year;
int month;
int day;
@Override
public boolean equals(Object obj)
{
int flag = 1;
if(obj instanceof MyDate)
{
MyDate md = (MyDate) obj;
flag = 0;
if (this.year != md.year) {
flag += 1;
}
if (this.month != md.month) {
flag += 1;
}
if (this.day != md.day) {
flag += 1;
}
}
if (flag == 0) {
return true;
}else {
return false;
}
}
}
public static void main(String[] args)
{
// Order o1 = new Order(123,"a001");
// Order o2 = new Order(123,"a001");
// System.out.println(o1.equals(o2));
MyDate m1 = new MyDate(2019, 11, 23);
MyDate m2 = new MyDate(2019, 11, 23);
System.out.println(m1.equals(m2));
}
3.包装类和toString:
Integer i0 = 112; //自动装箱
int i1 = i0; //自动拆箱
System.out.println(i0);
//字符串转基本类型
int i = Integer.parseInt("123");
float f = Float.parseFloat("1.00");
Boolean b = Boolean.parseBoolean("false");
//基本类型转字符串
String istr = String.valueOf(i);
String ifloat = String.valueOf(f);
String ib = String.valueOf(b);
4.关键字static:
让一个类的所有实例共享数据,就用类变量。
类变量不用实例化,直接类名.属性名可以使用
Chinese.country = "中国";
public class Chinese {
//想知道new了几个对象
public Chinese() {
Chinese.count += 1;
}
static String country;
String name;
public static int count;
int age;
public static void showCount() {
System.out.println(Chinese.count);
}
}
public class Test
{
public static void main(String[] args) {
// Chinese.country = "中国";
// Chinese c = new Chinese();
c.country = "中国";
// c.name = "xx";
// c.age = 11;
// Chinese c1 = new Chinese();
c1.country = "中国";
// c1.name = "xx";
// c1.age = 11;
// Chinese c2 = new Chinese();
c2.country = "中国";
// c2.name = "xx";
// c2.age = 11;
Chinese c1 = new Chinese();
Chinese c2 = new Chinese();
Chinese c3 = new Chinese();
Chinese.showCount();
}
}
5.单例设计模式:
整个系统运行中,这个类只被实例化一次
节约资源和时间
饿汉式:
Single s = Single.getInstance();
Single s1 = Single.getInstance();
Single s2 = Single.getInstance();
Single s3 = Single.getInstance();
Single s4 = Single.getInstance();