public class refaclt {
public static void main(String[] args) throws Exception, Exception {
//Student stu=new Student();
//Class cla=stu.getClass();//通过对象的getClass()方法获取这个类的Class对象;
//Class cla=Student.class;//通过类的class属性获取这个类的class对象;
Class cla=Class.forName("ceshidemo.ceshidemo.Student");//通过Class 类的forname()方法获取这个类的class对象;
try {
Constructor co=cla.getConstructor(String.class,String.class,String.class);//获取指定的构造方法;
Student stu1=(Student)co.newInstance("张三","29","男");//新建student对象stu1;
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
Student stu2=(Student)cla.newInstance();
Field namefileld=cla.getField("name");
namefileld.set(stu2, "李四");
Method met1=cla.getMethod("setname", String.class);
met1.invoke( stu1,"王二麻");//stu1提示找不到;
}
}
public static void main(String[] args) throws Exception, Exception {
//Student stu=new Student();
//Class cla=stu.getClass();//通过对象的getClass()方法获取这个类的Class对象;
//Class cla=Student.class;//通过类的class属性获取这个类的class对象;
Class cla=Class.forName("ceshidemo.ceshidemo.Student");//通过Class 类的forname()方法获取这个类的class对象;
try {
Constructor co=cla.getConstructor(String.class,String.class,String.class);//获取指定的构造方法;
Student stu1=(Student)co.newInstance("张三","29","男");//新建student对象stu1;
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
}
Student stu2=(Student)cla.newInstance();
Field namefileld=cla.getField("name");
namefileld.set(stu2, "李四");
Method met1=cla.getMethod("setname", String.class);
met1.invoke( stu1,"王二麻");//stu1提示找不到;
}
}