本帖最后由 刘兆华 于 2013-5-27 02:48 编辑
我一共有两个类: Person 和ReflectTest- public class Person { //Person类
- private String name;
- private int age;
- Person(String name,int age){
- this.name=name;
- this.age=age;
- }
- public void setName(String name){
- this.name=name;
- }
- public void setAge(int age){
- this.age=age;
- }
- public String getName(){
- return name;
- }
- public int getAge(){
- return age;
- }
- Person(String name)
- {
- this.name=name;
- this.age=0;
- }
- Person(int age)
- {
- this.age=age;
- this.name="";
- }
-
复制代码- package cn.itheima.bbs;
- import java.lang.reflect.*; //ReflectTest类
- public class ReflectTest {
- /**
- * @param args
- */
- public static void main(String[] args) throws Exception {
- // TODO Auto-generated method stub
-
- Person p1=(Person)Class.forName("cn.itheima.bbs.Person").getConstructor(String.class).newInstance(new String("abc"));
- System.out.println(p1.getName());
- }
- }
复制代码 运行时发生NoSuchMethodException 异常 求教 |