本帖最后由 为梦而战 于 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) |