黑马程序员技术交流社区

标题: JavaBean问题(请各位帮我看看为什么会这样,如何修正) [打印本页]

作者: 为梦而战    时间: 2012-1-8 12:47
标题: JavaBean问题(请各位帮我看看为什么会这样,如何修正)
本帖最后由 为梦而战 于 2012-1-8 23:52 编辑

//自己写了个JavaBean Person,可是运行的时候老是出现上面的提示,不解,请个位帮我看一
//看为什么找不到Person?如何修正?
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.Class;
import java.lang.String;
import java.lang.Integer;

public class IntroSpectorTest {
       
        public static void main(String[] args)throws Exception
        {
               
                Class clazz=Class.forName("Person",true,null);
                Constructor constructor=clazz.getConstructor(String.class,Integer.class);
                Person p=(Person)constructor.newInstance(new String("张科勇"),25);
                String propertyName="name";
                Object value="黑马-张科勇";
                setProperty(p, propertyName ,value);
                System.out.println(value);
               
        }

        private static void setProperty(Object p, String propertyName,Object value)
                        throws IntrospectionException, IllegalAccessException,
                        InvocationTargetException {
                PropertyDescriptor pd=new PropertyDescriptor(propertyName,p.getClass());
                Method methodSetName =pd.getWriteMethod();
                methodSetName.invoke(p, value);
        }
}

//下面的Person写在了和上面代码同一个包的不同.java文件中
import java.lang.String;
public class Person {

       
       
        private int age;
        private String name;
        public int getAge() {
                return age;
        }
        public void setAge(int age) {
                this.age = age;
        }
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        Person(String name,int age)
        {
                this.name=name;
                this.age=age;
               
               
        }
       
        public String toString()
        {
               
                return "name:"+getName()+":"+"age:"+getAge();
               
        }
}
错误提示:
Exception in thread "main" java.lang.ClassNotFoundException: Person
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:264)
        at com.itheima_2.IntroSpectorTest.main(IntroSpectorTest.java:26)
作者: ak47    时间: 2012-1-8 20:48
ClassNotFoundException: Person 没找到这个Person类
(IntroSpectorTest.java:26)
Class clazz=Class.forName("Person",true,null);是这么写的么?你再改改看看
作者: 朱泽宇    时间: 2012-1-8 21:25
改成Class clazz=Person.class;
还有你的Person里面我没有发现默认的构造函数、这个你自己定义构造函数后一定要加上默认的构造函数、
还有 你改完以后会报
java.lang.NoSuchMethodException异常
那是因为你的
Constructor constructor=clazz.getConstructor(String.class,Integer.class);
应该改为
Constructor constructor = clazz.getConstructor(String.class,int.class);
然后就可以完美运行了。
以后记得加默认构造函数~
作者: 为梦而战    时间: 2012-1-8 22:35
朱泽宇 发表于 2012-1-8 21:25
改成Class clazz=Person.class;
还有你的Person里面我没有发现默认的构造函数、这个你自己定义构造函数后 ...

public class IntroSpectorTest {
       
        public static void main(String[] args)throws Exception
        {
               
                Class clazz=Person.class;
                Constructor constructor = clazz.getConstructor(String.class,int.class);

                Person p=(Person)constructor.newInstance(new String("张科勇"),25);
                String propertyName="name";
                Object value="黑马-张科勇";
                setProperty(p, propertyName ,value);
                System.out.println(value);
               
        }
我按着你的改了一下:还是出现下面异常:这是怎么回事,是我哪还不对?请指点一下!谢谢!
Exception in thread "main" java.lang.NoSuchMethodException: com.itheima_2.Person.<init>(java.lang.String, int)
        at java.lang.Class.getConstructor0(Class.java:2721)
        at java.lang.Class.getConstructor(Class.java:1674)
        at com.itheima_2.IntroSpectorTest.main(IntroSpectorTest.java:26)
作者: 为梦而战    时间: 2012-1-8 22:40
ak47 发表于 2012-1-8 20:48
ClassNotFoundException: Person 没找到这个Person类
(IntroSpectorTest.java:26)
Class clazz=Class.forNa ...

哦,这个说是觉的没有找到Person类,是不是类加载器的问题,所以用了Class的静态方法forName(),这个方法有个重载,有三个参数,一个是类,一个是boolean型参数,一个是指定一个类加载器。我写成null,是想用默认加载器,可是还是行不通!
作者: ak47    时间: 2012-1-8 23:12
为梦而战 发表于 2012-1-8 22:35
public class IntroSpectorTest {
       
        public static void main(String[] args)throws Exception

java.lang.NoSuchMethodException: com.itheima_2.Person.<init>(java.lang.String, int)
没有这个方法, 你有Person(String name ,int age){} 构造函数么?
作者: 朱泽宇    时间: 2012-1-8 23:28

因为有参构造函数没有写访问级别所以默认为friendly 只有本包中的类可以调用、
所以其他类中的包无法调用只能调用其默认构造函数,所以会报 java.lang.NoSuchMethodException


作者: 为梦而战    时间: 2012-1-8 23:48
朱泽宇 发表于 2012-1-8 21:25
改成Class clazz=Person.class;
还有你的Person里面我没有发现默认的构造函数、这个你自己定义构造函数后 ...

非常感谢朱泽宇 同学,在他的帮助下,这个程序终于测试成功。真是三人行必有我师也!

作者: 为梦而战    时间: 2012-1-8 23:51
ak47 发表于 2012-1-8 23:12
java.lang.NoSuchMethodException: com.itheima_2.Person.(java.lang.String, int)
没有这个方法, 你有P ...

这个方法有呢。就是不能调用进来!朱泽宇 同学说这是访问级别的原因,觉的有道理!




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2