代码
public class Test1 {
public static void main(String[] args) throws ClassNotFoundException {
Person student = new Student();
System.out.println("这个人是 : "+student.name);
Class<? extends Person> method1 = student.getClass();
System.out.println(method1.hashCode());
Class<?> method2 = Class.forName("Student");
System.out.println(method2.hashCode());
Class<Student> method3 = Student.class;
System.out.println(method3.hashCode());
Class<Integer> method4 = Integer.class;
System.out.println(method4);
Class fatherClass = method1.getSuperclass();
System.out.println(fatherClass);
}
}
class Person{
String name;
public Person(String name) {
this.name = name;
}
public Person() {
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
'}';
}
}
class Student extends Person{
public Student() {
this.name = "学生";
}
}
class Teacher extends Person{
public Teacher() {
this.name = "老师";
}
}
输出结果
这个人是 : 学生
1057941451
1057941451
1057941451
class java.lang.Integer
class Person
Process finished with exit code 0