本帖最后由 caijunsong 于 2014-3-28 12:13 编辑
- <BLOCKQUOTE>
- <DIV class=blockcode>
- <BLOCKQUOTE>import java.lang.reflect.*;
- class Person
- {
- private String name;
- private int age;
- double height;
- String[]body=new String[5];
- int []tel=new int[11];
- Person(String name,int age)
- {
- this.name=name;
- this.age=age;
- }
- Person()
- {
- this.name="noname";
- this.age=0;
- height=0.0;
- }
- public String getName()
- {
- return name;
- }
- public int getAge()
- {
- return age;
- }
- private void setAge(int age)
- {
- this.age=age;
- }
- public String toString()
- {
- return "名字:"+name+"年龄:"+age+"身高"+height+"米";
- }
- }
- public class Test
- {
- public static void main(String[]agrs)throws Exception
- {
- Class<Person> person=Person.class;
- Person p=(Person)person.newInstance();
- System.out.println(p);
- Constructor cr=Person.class.getConstructor(String.class,Integer.class);
- Object l=cr.newInstance(new String("xiaocai"),new Integer(22));//这里出现错误
- //运行错误提示如下:
- //Exception in thread "main" java.lang.NoSuchMethodException: cn.itcast.swing.Person.<init>(java.lang.String, java.lang.Integer)
- System.out.println(l);
- }
- }
复制代码
|