请高手帮忙:用class.forname弄出来的类中函数怎么调用?有办法吗? 如果parent是interface,那就可以因为你实际用到是的那个method嘛,而不是parent里的field methodname 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我是想用一个在父类或者接口中没有的函数,而这个函数的名字,参数是由子类通过父类的变量传给接口或父类,然后在仅有父类存在和知道子类的uri时调用, 我也Up一下我也Up两下!!!^@^ fishbob21(fishbob)请问怎么反射? import java.lang.reflect.*;public class TestMothed { public TestMothed() { } public void test() { System.out.println("ok."); } public static void main(String[] args) throws Exception { TestMothed testMothed = new TestMothed(); Class c = Class.forName("xmldoom.TestMothed"); Method method = c.getMethod("test", null); method.invoke(testMothed, null); }}这个可以 import java.lang.reflect.*;public class TestMothed { public TestMothed() { } public void test() { System.out.println("ok."); } public static void main(String[] args) throws Exception { TestMothed testMothed = new TestMothed(); Class c = Class.forName("TestMothed"); Method method = c.getMethod("test", null); method.invoke(testMothed, null); }}这样 不知道这样行不行:getParameterTypes()取得参数类型,然后public Object invoke(Object obj, Object[] args)调用函数。 楼上说的当然行了,jokerjava(冷血) 写的例子过于简单了,没涉及到方法有参数的形式,如果要调用的方法有参数,就必须用public Object invoke(Object obj, Object[] args)将参数传进去,而且利用反射效率极底,不到万不得以,最好不要用. import java.lang.reflect.*;public class TestMothed { public TestMothed() { } public void test() { System.out.println("ok."); } public void test(String str) { System.out.println(str); } public static void main(String[] args) throws Exception { TestMothed testMothed = new TestMothed(); Class c = Class.forName("TestMothed"); Class[] cs = new Class[1]; cs[0] = String.class; Method method = c.getMethod("test", cs); Object param[] = new Object[1]; param[0] = "asd"; method.invoke(testMothed, param); }}这是带参数的 都让我写了 搂主没得作了 呵 Number小问题,在线等 关于Runtime的东东。请教。 JAVA子类对象如何调用父类被overridden的函数? 问一个mysql移植数据库的问题。 请老鸟过来帮帮忙 关于jsp 关于java 如何实现java在后台监听键盘 初学JAVA遇到的一个问题!谢谢! 在Structs的XML里面是否可以写注释? 中兴面试题之一 求教:对于内网的应用程序,有没办法获得自己公网的IP地址。? javascript的问题,急问!! JAVA如何类似C的格式输出?急,高分
^@^
请问怎么反射?
public TestMothed() {
}
public void test() {
System.out.println("ok.");
}
public static void main(String[] args) throws Exception {
TestMothed testMothed = new TestMothed();
Class c = Class.forName("xmldoom.TestMothed");
Method method = c.getMethod("test", null);
method.invoke(testMothed, null);
}}
这个可以
public TestMothed() {
}
public void test() {
System.out.println("ok.");
}
public static void main(String[] args) throws Exception {
TestMothed testMothed = new TestMothed();
Class c = Class.forName("TestMothed");
Method method = c.getMethod("test", null);
method.invoke(testMothed, null);
}}
这样
getParameterTypes()取得参数类型,然后
public Object invoke(Object obj, Object[] args)调用函数。
public TestMothed() {
} public void test() {
System.out.println("ok.");
} public void test(String str) {
System.out.println(str);
} public static void main(String[] args) throws Exception {
TestMothed testMothed = new TestMothed();
Class c = Class.forName("TestMothed");
Class[] cs = new Class[1];
cs[0] = String.class;
Method method = c.getMethod("test", cs);
Object param[] = new Object[1];
param[0] = "asd";
method.invoke(testMothed, param);
}}这是带参数的 都让我写了 搂主没得作了 呵