吐血 悬赏 class.forname(....);获得一个构造是private的类的实例! 怎么用 class.forName(),来获得一个构造函数是private的类的实例? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 构造函数是private的类是不能被实例化的,一般来说这种类都会提供静态的工厂方法 你的意思相当于说怎么直接调用private Method. 不能获得,既然它定义为private,就是不希望别人通过构造函数来创建实例. 不过你可以看看它有没有静态工厂方法,通过工厂方法得到一个实例 在类里面 定义一个自己的实例化 要是static 的 外面的类要用就调用一个方法 来返回这个对象在 这个singleton类必须实现一个接口过来的.我需要根据这个类的名字和路径用class.forName()获得class.再获得一个接口类型的实例. 我采用class.forName的时候用什么方式获得singleton的实例. package tony.designpattern.singleton;import java.lang.reflect.*;public class Test { public static void main(String[] args) { try { Class c = Class .forName("tony.designpattern.singleton.LazySingleton"); Method[] methods = c.getMethods(); for (int i = 0; i < methods.length; i++) { String className = methods[i].getReturnType().toString(); if (className.indexOf(".") > 0) { className = className .substring(className.lastIndexOf(".") + 1); System.out.println("className:" + className); if (className.equals("LazySingleton")) {//返回类型是这个的类 // if (methods[i].getModifiers() == Method.) {//原先想判断是否是static的 LazySingleton singleton = (LazySingleton) methods[i] .invoke(null, null);//静态方法可以不用实例,所以可以用null // System.out.println(singleton instanceof // LazySingleton); singleton.print(); // } } } } } catch (Exception e) { } }}------------------------------------package tony.designpattern.singleton;/** * Description: * * @author Tony.Fang * @version 0.1 * */public class LazySingleton { private static LazySingleton lazySingleton = null; private int i = 0; private LazySingleton() { } synchronized public static LazySingleton getInstance() { if (lazySingleton == null) { lazySingleton = new LazySingleton(); } return lazySingleton; } public void print() { System.out.println("lazySingleton:"+i++); }} U can call private methods , r/w private fields by JNI . 把另一帖的回复 copy 过来,大家帮忙检查一下回复人: haha1903(中国人) ( ) 信誉:100 2005-07-13 09:30:00 得分: 0 要用反射,做个例子给你//test.Test1package test;public class Test1 { private int i; private Test1(int i) { this.i = i; } public void test() { System.out.println(i); }}//test.Test2package test;import java.lang.reflect.Constructor;import java.lang.reflect.Method;public class Test2 { public static void main(String[] args) throws Exception { Class c = Class.forName("test.Test1"); Constructor constructor = c.getDeclaredConstructor(new Class[] { int.class }); constructor.setAccessible(true); Object o = constructor.newInstance(new Object[] { new Integer(10) }); Method testMethod = c.getMethod("test", null); testMethod.invoke(o, null); }}你试一下就明白了。但构造函数一定要存在。 Top 回复人: haha1903(中国人) ( ) 信誉:100 2005-07-13 09:33:00 得分: 0 私有方法不等于不能调用,虽然这样调用是不好的,很容易引入问题。但有时做 UT 的时候,不得不用这样的办法。 Top 回复人: haha1903(中国人) ( ) 信誉:100 2005-07-13 09:35:00 得分: 0 spring 中的 bean 默认就是 singleton 的,但也不一定有静态的工厂方法啊。 Top 把另一帖的回复转过来回复人: haha1903(中国人) ( ) 信誉:100 2005-07-13 09:30:00 得分: 0 要用反射,做个例子给你//test.Test1package test;public class Test1 { private int i; private Test1(int i) { this.i = i; } public void test() { System.out.println(i); }}//test.Test2package test;import java.lang.reflect.Constructor;import java.lang.reflect.Method;public class Test2 { public static void main(String[] args) throws Exception { Class c = Class.forName("test.Test1"); Constructor constructor = c.getDeclaredConstructor(new Class[] { int.class }); constructor.setAccessible(true); Object o = constructor.newInstance(new Object[] { new Integer(10) }); Method testMethod = c.getMethod("test", null); testMethod.invoke(o, null); }}你试一下就明白了。但构造函数一定要存在。 Top 回复人: haha1903(中国人) ( ) 信誉:100 2005-07-13 09:33:00 得分: 0 私有方法不等于不能调用,虽然这样调用是不好的,很容易引入问题。但有时做 UT 的时候,不得不用这样的办法。 Top 回复人: haha1903(中国人) ( ) 信誉:100 2005-07-13 09:35:00 得分: 0 spring 中的 bean 默认就是 singleton 的,但也不一定有静态的工厂方法啊。 Top 请求帮助,查询出错,不知道错在哪? 求助:java如何监视Websphere MQ的通道状态 小弟java新手,有一问题一直不明:为什么java中有的地方必须抛出异常而有的地方却不必? 请教,eclipse的插件实现原理 字符集,不懂 J2SE做办公自动化的毕业设计问题 JAVA程序如何使用多个代理服务器 数据库执行sql文的原子性问题 请问我也下载了一个J2SDK,在WINDOWS里如何设置呢? 菜鸟求教,感谢各位大佬 一直争论不休的话题 求助!
不过你可以看看它有没有静态工厂方法,通过工厂方法得到一个实例
外面的类要用就调用一个方法 来返回这个对象在
try {
Class c = Class
.forName("tony.designpattern.singleton.LazySingleton");
Method[] methods = c.getMethods();
for (int i = 0; i < methods.length; i++) {
String className = methods[i].getReturnType().toString();
if (className.indexOf(".") > 0) {
className = className
.substring(className.lastIndexOf(".") + 1);
System.out.println("className:" + className);
if (className.equals("LazySingleton")) {//返回类型是这个的类
// if (methods[i].getModifiers() == Method.) {//原先想判断是否是static的
LazySingleton singleton = (LazySingleton) methods[i]
.invoke(null, null);//静态方法可以不用实例,所以可以用null
// System.out.println(singleton instanceof
// LazySingleton);
singleton.print();
// }
}
}
}
} catch (Exception e) {
} }}
------------------------------------
package tony.designpattern.singleton;/**
* Description:
*
* @author Tony.Fang
* @version 0.1
*
*/
public class LazySingleton { private static LazySingleton lazySingleton = null;
private int i = 0;
private LazySingleton() {
} synchronized public static LazySingleton getInstance() {
if (lazySingleton == null) {
lazySingleton = new LazySingleton();
}
return lazySingleton;
} public void print() {
System.out.println("lazySingleton:"+i++);
}
}
回复人: haha1903(中国人) ( ) 信誉:100 2005-07-13 09:30:00 得分: 0
要用反射,做个例子给你
//test.Test1
package test;public class Test1 {
private int i; private Test1(int i) {
this.i = i;
}
public void test() {
System.out.println(i);
}
}
//test.Test2
package test;import java.lang.reflect.Constructor;
import java.lang.reflect.Method;public class Test2 {
public static void main(String[] args) throws Exception {
Class c = Class.forName("test.Test1");
Constructor constructor = c.getDeclaredConstructor(new Class[] { int.class });
constructor.setAccessible(true);
Object o = constructor.newInstance(new Object[] { new Integer(10) });
Method testMethod = c.getMethod("test", null);
testMethod.invoke(o, null);
}
}
你试一下就明白了。
但构造函数一定要存在。
Top
回复人: haha1903(中国人) ( ) 信誉:100 2005-07-13 09:33:00 得分: 0
私有方法不等于不能调用,虽然这样调用是不好的,很容易引入问题。
但有时做 UT 的时候,不得不用这样的办法。
Top
回复人: haha1903(中国人) ( ) 信誉:100 2005-07-13 09:35:00 得分: 0
spring 中的 bean 默认就是 singleton 的,但也不一定有静态的工厂方法啊。
Top
回复人: haha1903(中国人) ( ) 信誉:100 2005-07-13 09:30:00 得分: 0
要用反射,做个例子给你
//test.Test1
package test;public class Test1 {
private int i; private Test1(int i) {
this.i = i;
}
public void test() {
System.out.println(i);
}
}
//test.Test2
package test;import java.lang.reflect.Constructor;
import java.lang.reflect.Method;public class Test2 {
public static void main(String[] args) throws Exception {
Class c = Class.forName("test.Test1");
Constructor constructor = c.getDeclaredConstructor(new Class[] { int.class });
constructor.setAccessible(true);
Object o = constructor.newInstance(new Object[] { new Integer(10) });
Method testMethod = c.getMethod("test", null);
testMethod.invoke(o, null);
}
}
你试一下就明白了。
但构造函数一定要存在。
Top
回复人: haha1903(中国人) ( ) 信誉:100 2005-07-13 09:33:00 得分: 0
私有方法不等于不能调用,虽然这样调用是不好的,很容易引入问题。
但有时做 UT 的时候,不得不用这样的办法。
Top
回复人: haha1903(中国人) ( ) 信誉:100 2005-07-13 09:35:00 得分: 0
spring 中的 bean 默认就是 singleton 的,但也不一定有静态的工厂方法啊。
Top