public class Fu {
public Fu() {
syso(this.getClass);
}
}
public class Zi {
public Zi() {
syso(this.getClass);
}
}
public main() {
Fu fu = new Fu(); //输出 class com.feicui.test.Fu
Zi zi = new Zi(); //输出 class com.feicui.test.Zi
}
public class Fu {
public Fu() {
syso(this.getClass);
}
}
public class Zi extends Fu{
public Zi() {
syso(this.getClass);
}
}
public main() {
Zi zi = new Zi(); //输出 class com.feicui.test.Zi
// class com.feicui.test.Zi
Fu fu = new Zi(); //输出 class com.feicui.test.Zi
// class com.feicui.test.Zi
}
底层是动态绑定 this是指运行时那个对象,new谁,就用谁的构造器
public class Fu {
public Fu() {
// 获取当前对象所属的类的字节码
syso(this.getClass());
// 获取没有泛型信息的父类字节码
syso(this.getClass().getSuperclass);
// 获取有泛型信息的父类全类名的字符串
String str = this.getClass().getGenericSuperclass().toString();
syso(str);
// 进一步截取出泛型的字符串
str = str.substring(str.indexOf("<") + 1, str.length() - 1);
// 进一步就可以获取泛型类的字节码
try{
Class clazz = Class.forName(str);
syso(clazz);
}catch (Exception e){
e.printStackTrace();
}
}
}
public class Zi extends Fu{
public Zi() {
}
}
public main() {
Zi zi = new Zi(); //输出 class com.feicui.test.Zi
// class com.feicui.test.Fu
//com.feicui.test.Fu<java.lang.String>
//class java.lang.string
}