百知教育 - 孙帅 - 12_Object类与包装类
01_Object_toString方法
-
Object类
- Object类是所有Java类的父类
- Object o = 任何对象
- Object类中的方法是所有对象都具有的方法
-
Object_getClass方法
- 返回对象的实际类型 即Class对象(反射)
-
Object_toString方法
- 语法: public String toString()
- 返回值: 返回对象的字符串形式
- 用法: 打印对象,等同于打印对象的toString()方法的返回值
-
代码:
package day14; public class TestObject{ public static void main(String[] args){ Animal a1 = new Dog(); Animal a2 = new Cat(); /* if((a1 instanceof Dog && a2 instanceof Dog) || (a1 instanceof Cat && a2 instanceof Cat)) System.out.println("是同类对象"); else System.out.println("不是同类对象"); if(a1.getClass() == a2.getClass()) System.out.println("是同类对象"); else System.out.println("不是同类对象"); */ Student s = new Student("yky", 40); System.out.println(s); } } class Animal{} class Dog extends Animal{} class Cat extends Animal{} class Student{ private String name; private int age; public Student(String name, int age){ super(); this.name = name; this.age = age; } public String toString(){ return "Student name = "+name+" age = "+age; } }
-
运行结果:
02_Object_equals方法
-
equals()
- 作用: 判断两个 对象 的内容是否相同
- 声明: public boolean equals(Object o)
- 调用: s1.equals(s2) 判断s1和s2内容是否相同
-
==
- 作用: 判断两个引用是否指向同一对象
-
代码:
package day14; public class TestObject{ public static void main(String[] args){ Animal a1 = new Dog(); Animal a2 = new Cat(); /* if((a1 instanceof Dog && a2 instanceof Dog) || (a1 instanceof Cat && a2 instanceof Cat)) System.out.println("是同类对象"); else System.out.println("不是同类对象"); if(a1.getClass() == a2.getClass()) System.out.println("是同类对象"); else System.out.println("不是同类对象"); */ /* int a = 10; int b = 10; System.out.println(a == b); */ Student s1 = new Student("yky", 20); Student s2 = new Student("yky", 20); System.out.println(s1 == s2); System.out.println(s1.equals(s2)); /* String s1 = new String("abc"); String s2 = new String("abc"); System.out.println(s1 == s2); System.out.println(s1.equals(s2)); */ } } class Animal{} class Dog extends Animal{} class Cat extends Animal{} class Student{ private String name; private int age; public Student(String name, int age){ super(); this.name = name; this.age = age; } public String toString(){ return "Student name = "+name+" age = "+age; } //判断this 和 o 对象内容是否相同 public boolean equals(Object o){ if(this == o) return true; if(o == null) return false; if(this.getClass() != o.getClass()) return false; Student s = (Student)o; if(this.name.equals(s.name) && this.age == s.age) return true; else return false; } }
-
运行结果:
03_Object_finalize
-
垃圾回收
- 定义: JVM自动回收 垃圾对象,释放相应的内存空间
-
垃圾对象的认定
- 零引用算法(当没有引用指向该对象时,被认定为垃圾对象)
-
垃圾对象的回收时机
- 延迟回收策略
-
finalize()
- 调用时机: 对象被垃圾回收的时候,由垃圾回收器自动调用
-
代码:
Student s = new Student("YKY" ,20); XXXX s = null; // 不用的时候把引用指向空指针,从而把该对象标记为垃圾
04_利用标注实现覆盖语法检查
-
覆盖标注:
- 语法: @Override
- 作用: 编译器自动检查覆盖是否有误
-
代码:
package day14; public class TestOverride{ public static void main(String[] args){ Worker w = new Worker(); System.out.println(w); } } class Worker{ //我要覆盖 @Override public String toString(){ return "hehe"; } //我要覆盖 @Override public boolean equals(Object w){ return false; } }
-
运行结果:
05_包装类
-
概念:
8种基本类型的对象形式 -
基本类型对应的包装类
-
int Integer
-
byte Byte
-
short Short
-
long Long
-
float Float
-
double Double (前六种包装类的 直接父类 为Number)
-
char Character
-
boolean Boolean
-
-
类型转换
- int与Integer
int i = 10 Integer a = new Integer(i); Integer b = Integer.valueOf(i); Integer a1 = new Integer(12); Integer a2 = new Integer(12); System.out.println(a1 == a2); //打印false Integer a3 = Integer.valueOf(12); Integer a4 = Integer.valueOf(12); System.out.println(a3 == a4); //打印true Integer a5 = Integer.valueOf(128); Integer a6 = Integer.valueOf(128); System.out.println(a5 == a6); //打印false i = a.intValue(); //valueOf()方法:传递的整数在-128~127之间时,使用的是创建好的对象;之外时,创建新对象。
- 自动封箱(拆箱): 由编译器完成基本类型和包装类之间的转换
- int与String
int i = 10; String s = String.valueOf(i); String s = i+""; i = Integer.parseInt(s);
- Integer与String
Integer a = Integer.valueOf(10); String s = a.toString(); a = new Integer(s); a = Integer.valueOf(s);
- int与Integer
06_接口引用和类引用
-
Java语言规范
- 编译器编译时要对引用进行检查,来确定是否能调用该方法,检查的步骤 为:1. 本类 2. 父类 3. 接口 4. Object类,如果存在该方法就能正常调用。
-
注意:
- 接口不是Object类的子类,但是 接口引用 可以被 当做 类引用 来使用。
-
代码(输出 false):
package day18; public class TestInterface4{ public static void main(String[] args){ A a1 = new B(); A a2 = new B(); //System.out.println(a.toString()); Object o = a1; //接口引用当做类引用来使用 System.out.println(a1.equals(a2)); //接口对象a2赋值给了Object引用 } } interface A{} class B implements A{}