复制代码
- <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);
- }
- }
郭黎明 发表于 2014-3-27 20:52
public static void main(String[] args) throws Exception {
Constructor cons=person.class.getDec ...
郭黎明 发表于 2014-3-27 23:04
public class Test
{
public static void main(String[]agrs)throws Exception
caijunsong 发表于 2014-3-28 01:10
怎么结束提问啊!!!!!
无标题.png (18.91 KB, 下载次数: 28)
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |