package set;
public class Person {
private String name;
private int age;// 创建构造方法,在new时就给属性传值
public Person(){
}
public Person(String name,int age){
this.name = name;
this.age = age;}
@Override
public String toString(){
return this.name +"\t"+ this.age;}}
package test0812;//使用java反射获取类的对象(调用无参构造方法创建实例对象)
class B {
public String name;
private int age;
public voidsay(){
}
public intjia(int num1,int num2){
return num1 + num2;}}
public class Test03 {
public voidcreateObj(String className){
// 通过java反射获取B类的对象
try {
Class clazz = Class.forName(className);// 使用java反射获取类的对象
Object obj = clazz.newInstance();//调用的是无参构造方法
System.out.println(obj);//test0812.B@15db9742} catch (Exception e){
e.printStackTrace();}}
public staticvoidmain(String[] args){
Test03 t = new Test03();
t.createObj("test0812.B");}}