1. Object类
package com.fy.t2;
public class TestObject {
public static void main(String[] args) {
method(new Student());
System.out.println();
}
public static void method(Object obj) {
}
public static void goHome(Object obj) {
}
public static Object buyVehicle(int n) {
return null;
}
}
class Dog {}
class Cat {}
class Student {}
2. getClass()方法
package com.fy.t2;
public class TestGetClass {
public static void main(String[] args) {
Student1 s = new Student1();
System.out.println(s.getClass());
Object o = new Object();
System.out.println(o.getClass());
Animal a = new Dog1();
System.out.println(a.getClass());
Animal a1 = new Dog1();
Animal a2 = new Cat1();
Animal a3 = new Bird();
Animal a4 = new Cat1();
if(a1.getClass() == a2.getClass()) {
System.out.println("sametype");
}else {
System.out.println("noSame");
}
}
}
class Student1{}
class Animal{}
class Dog1 extends Animal{}
class Cat1 extends Animal{}
class Bird extends Animal{}
3. hashCode()方法
package com.fy.t2;
public class TestHashCode {
public static void main(String[] args) {
for(int i = 0; i < 5; i++) {
Object o = new Object();
System.out.println(o.hashCode() + "\t" + o);
System.out.println(o.hashCode() + "\t" + o.toString());
System.out.println(o.hashCode() + "\t" + Integer.toHexString(o.hashCode()));
}
}
}
class Teacher2{
public String toString() {
return null;
}
}
toString()方法