1.class类
2.class的常用方法
3.获取class类的实例
测试得到Class类有几种方式小案例
//测试class类的方式有哪些
public class Demo1 {
public static void main(String[] args) throws ClassNotFoundException {
Petion petion = new stent ();
System.out.println ("这个人是"+petion.name);
//方式1:通过对象获得
Class c1 = petion.getClass ();
System.out.println (c1.hashCode ()); //输出
//方式2.forname获得
Class c2 = Class.forName ("注解和反射.反射.得到Class类的几种方法.stent");
System.out.println (c2.hashCode ());
//方式3.通过类名.Class获得
Class c3 = stent.class;
System.out.println (c3.hashCode ());
//方式4.基本内置类型的包装类都有一个Type属性
Class c4 = Integer.TYPE; //输出是int,int是默认基本类型
System.out.println (c4);
//获得父类基本类型
Class c5 = c1.getSuperclass ();
System.out.println (c5);
}
}
//类
class Petion {
public String name;
//无参
public Petion() {
}
//有参
public Petion(String name) {
this.name = name;
}
//toString
@Override
public String toString() {
return "Petion{" +
"name='" + name + '\'' +
'}';
}
}
//定义个子类
class stent extends Petion{
public stent(){
this.name = "学生";
}
}
class atent extends Petion{
public atent(){
this.name = "老师";
}
}