//class反射编程
//定义一个学生类
public class test124 {public String name;protected int age;double height;private double money;public test124(String name,Integer age){this.name=name;this.age=age;}public test124(String name,int age,double money){this.name=name;this.age=age;this.money=money;}//看书public void learn(BookRead book){System.out.println( "我的名字"+name+ "我正在学习"+book.getName());}//奔跑public void run(){System.out.println( "我们喜欢奔跑" );}
}
定义bookread类
public class BookRead {private String name;public void setName(String name) {this.name = name;}public String getName() {return name;}
}
定义测试类
public class test125 {/*使用class实现反射编程,创建对象,获取类的相关信息* */public static void main(String[] args){String packageName=test124.class.getPackage().getName();System.out.println( "包名"+packageName);try {Class<?> stuClass=Class.forName( "test124" );System.out.println( stuClass.getName() );//使用字节码对象获取这个对象的实例test124 stuObj=(test124)stuClass.newInstance();System.out.println(stuObj!=null );}catch (Exception e){}}
}
运行结果