黑马程序员技术交流社区
标题:
反射——构造器
[打印本页]
作者:
DOOR
时间:
2014-1-9 23:31
标题:
反射——构造器
本帖最后由 DOOR 于 2014-1-15 02:05 编辑
import java.lang.reflect.Field;
import java.lang.reflect.Constructor;
class ReflectPoint
{
public int x;
public int y;
private String str1 = "ball";
private String str2 = "basketball";
private String str3 = "itcast";
ReflectPoint(int x,int y)
{
this.x=x;
this.y=y;
}
public String toString()
{
return str1+":"+str2+":"+str3;
}
}
class Demo0707
{
public static void main(String[] args)throws Exception
{
//ReflectPoint rp = new ReflectPoint(3,5);
Constructor constructor1 = ReflectPoint.class.getConstructor(int.class,int.class);
ReflectPoint rp = (ReflectPoint)constructor1.newInstance(3,5);
//changeStringValue(rp);
System.out.println(rp);
}
}
复制代码
NoSuchMethodException异常,求解
作者:
daoyua
时间:
2014-1-9 23:34
NoSuchMethodException,我汗,你直接英文翻译赛,就是没有找个这个方法异常
作者:
DOOR
时间:
2014-1-9 23:39
daoyua 发表于 2014-1-9 23:34
NoSuchMethodException,我汗,你直接英文翻译赛,就是没有找个这个方法异常
不是这个意思,我想问为什么会抛这个异常
作者:
wodenhaowzg
时间:
2014-1-10 04:34
NoSuchMethodException这个异常指:找不到该方法。
该方法指: constructor1.newInstance(3,5);
改成constructor1.newInstance();即可
newInstance()方法不能带参数,即没有带参数的newInstance()方法
作者:
松毛
时间:
2014-1-10 08:55
我试了一下,可能是你的 ReflectPoint(int x,int y)这个构造函数的权限的问题,你在构造函数前加上public修饰符再试试,应该可以的。
作者:
IT人
时间:
2014-1-10 10:36
请问一下构造方法的默认是啥????还有
newInstance()可以有参数!!!应该是权限的问题!!!
作者:
山治0712
时间:
2014-1-14 23:01
把
ReflectPoint.class.getConstructor(int.class,int.class);
改成
ReflectPoint.class.getDeclaredConstructor(int.class,int.class);
就可以了。
作者:
张志明
时间:
2014-1-14 23:24
编译:
Exception in thread "main" java.lang.NoSuchMethodException: ReflectPoint.<init>(int, int)
复制代码
修改为:
import java.lang.reflect.Field;
import java.lang.reflect.Constructor;
class ReflectPoint
{
public int x;
public int y;
private String str1 = "ball";
private String str2 = "basketball";
private String str3 = "itcast";
public ReflectPoint(int x,int y)
{
this.x=x;
this.y=y;
}
public String toString()
{
return str1+":"+str2+":"+str3;
}
}
public class InvokeRun
{
public static void main(String[] args)throws Exception
{
//ReflectPoint rp = new ReflectPoint(3,5);
Constructor constructor1 = ReflectPoint.class.getConstructor(int.class,int.class);
ReflectPoint rp = (ReflectPoint)constructor1.newInstance(3,5);
//changeStringValue(rp);
System.out.println(rp);
}
}
复制代码
构造函数权限不可以被反射获取到,把构造方法公开暴露后,编译通过
输出:ball:basketball:itcast
作者:
工善器
时间:
2014-1-16 11:28
权限问题,下面的都不能访问到上面的reflectPoint方法:
package first;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
class ReflectPoint{
public int x;
public int y;
private String str1="123";
private String str2="zhangsan";
private String str3="lisi";
public ReflectPoint(int x, int y)
{
this.x=x;
this.y=y;
}
// 公有一个转字符串的方法
public String toString()
{
return str1+":"+str2+":"+str3;
}
}
public class Test{
public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Constructor constructor1=ReflectPoint.class.getConstructor(int.class,int.class);
ReflectPoint rp=(ReflectPoint) constructor1.newInstance(3,5);
System.out.println(rp);
}
}
复制代码
改成这样就可以了
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2