标签:hashcode struct java tostring 获得 lang div 方式 field
public class Person{
private String name;
private Integer age;
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public Integer getAge(){
return age;
}
public void setAge(Integer age){
this.age = age;
}
public Person(){
}
public Person(String name, Integer age){
this.name = name;
this.age = age;
}
public Person(String name){
this.name = name;
}
public Person(Integer age){
this.age = age;
}
}
Person p = new Person();
Person p2 = new Person();
Class c1 = p.getClass();
Class c2 = p2.getClass();
System.out.print(c1 == c2); // 输出为true,说明创建的两个对象使用的是同一份字节码文件对象
import java.lang.reflect.Constructor;
Class c3 = Person.class; //得到字节码文件对象
Constructor [] constructors = c3.getConstructors(); // 获得构造器对象
for(Constructor constructor:constructors){
System.out.println(constructor);
}
Constructor constr = c3.getConstructor(String.class); // 得到指定的构造器对象
Object obj = constr.newInstance("tom"); // 用得到的构造器对象创建该类的实例
Person cp = (Person)obj;
System.out.println(cp.getName()); //输出为tom
public class Student{
private Student(){
}
public String toString(){
return "Student [getClass()=" + getClass() + ",hashCode()=" +hashCode()+ ",toString()=" + super.toString() +"]";
}
}
标签:hashcode struct java tostring 获得 lang div 方式 field
原文地址:https://www.cnblogs.com/baiyifengyun/p/14398736.html