java 定义一个标准的JavaBean,名叫Person,包含属性name、age。使用反射的方式创建一个实 5
个实例、调用构造函数初始化name、age,使用反射方式调用setName方法对名称进行设置,不使用setAge方法直接使用反射方式对age赋值。求完整代码,急急!!!!...
个实例、调用构造函数初始化name、age,使用反射方式调用setName方法对名称进行设置,不使用setAge方法直接使用反射方式对age赋值。求完整代码,急急!!!!!!!!!
展开
2个回答
展开全部
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class reflect_j
{
public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
{
Person p = new Person();
Class c = p.getClass();
Method m = c.getMethod("setAge",String.class);
m.invoke(p, "21");
System.out.println("age:"+p.getAge());
}
}
class Person
{
public String name;
public String age="10";
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAge()
{
return age;
}
public void setAge(String age)
{
this.age = age;
}
}
import java.lang.reflect.Method;
public class reflect_j
{
public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
{
Person p = new Person();
Class c = p.getClass();
Method m = c.getMethod("setAge",String.class);
m.invoke(p, "21");
System.out.println("age:"+p.getAge());
}
}
class Person
{
public String name;
public String age="10";
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getAge()
{
return age;
}
public void setAge(String age)
{
this.age = age;
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
自己也在学习ing...
import java.lang.reflect.*;
public class Test
{
public static void main(String []args) throws Exception{
Class c = Class.forName("Persion");
Persion p = (Persion)c.newInstance();
Method m = c.getMethod("setName",String.class);
m.invoke(p,"xiaoli");
Field f = c.getField("age");
f.setInt(p,10);
System.out.println(p.toString());
}
};
class Persion
{
public String name;
public int age;
public Persion(String name,int age){
this.name=name;
this.age=age;
}
public Persion(){
this.name="lili";
this.age=1;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return this.age;
}
public String toString(){
return "name="+this.name+",age="+this.age;
}
};
import java.lang.reflect.*;
public class Test
{
public static void main(String []args) throws Exception{
Class c = Class.forName("Persion");
Persion p = (Persion)c.newInstance();
Method m = c.getMethod("setName",String.class);
m.invoke(p,"xiaoli");
Field f = c.getField("age");
f.setInt(p,10);
System.out.println(p.toString());
}
};
class Persion
{
public String name;
public int age;
public Persion(String name,int age){
this.name=name;
this.age=age;
}
public Persion(){
this.name="lili";
this.age=1;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return this.age;
}
public String toString(){
return "name="+this.name+",age="+this.age;
}
};
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |