不用new在测试类里面父类能调用子类 b_Obj类继承了a_Obja_Obj a = null;a = new a_Obj();System.out.println("result = "+a.c(1, 1));这里a怎么调用b_Obj 类里的c(x,x)方法? 不能new b_Obj 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 c(x,y)是b中特有的方法,用父类对象怎么可以调用子类中的特有方法呢? 其它类都不能修改,只能修改a_Obj a = null;a = new a_Obj();附近的代码 import java.lang.reflect.Method;import org.junit.Test;public class Ref { public void T(String className, String methodName, Object[] args) throws Exception { Class ownerClass = Class.forName(className); Class[] argsClass = new Class[args.length]; for (int i = 0, j = args.length; i < j; i++) { argsClass[i] = args[i].getClass(); } Method method = ownerClass.getMethod(methodName, argsClass); method.invoke(ownerClass.newInstance(), args); } @Test public void testRe() throws Exception { String className = "b_Obj"; String methodName = "c"; String x = null, y = null; Object[] args = { new String("x"), new String("y") }; Ref r = new Ref(); r.T(className, methodName, args); }} 父类写一个c(x,y)让子类去重写c(x,y)然后父类.c的时候执行的就是子类的c(x,y)方法了 LZ你想写的应该是a_Obj a = null;a = new b_Obj();这样吧.. a_Obj代码public class a_Obj { public a_Obj() { } public int c(int a,int b) { return a+b; }}b_Obj代码public class b_Obj extends a_Obj{ public b_Obj() { } public int c(int a,int b) { return a*b; }}测试类里面代码a_Obj a = null; // Add 这里修改 a = new a_Obj(); // Add System.out.println("result = "+a.c(1, 1));a_Obj 和 b_Obj 类不能修改,测试类里面不能new b_Obj 别说我赖皮 public static void main(String[] args) { a_Obj a = null; try { a = (a_Obj) Class.forName("com.test.b_Obj").newInstance(); System.out.println("result = " + a.c(1, 1)); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } 基础题 如何把method.invoke后的返回值转成数组或集合类型? 请教FTP 服务器的一个问题 [求助]InputStream的available()方法是不是有BUG? 高分清高手——Java开发一个用来制作chm文件的工具 为什么BufferedReader只能读取第一行 des三重加密的问题?(在线等待) java 类型转换问题 大虾们帮忙(做JSP) tomcat配置sqlserver连接问题 一个关于NoClassDefFounderErr的问题 多线程问题的执行顺序 怎么获取JscrollPane中组件的位置
a_Obj a = null;
a = new a_Obj();
附近的代码
throws Exception {
Class ownerClass = Class.forName(className);
Class[] argsClass = new Class[args.length]; for (int i = 0, j = args.length; i < j; i++) {
argsClass[i] = args[i].getClass();
} Method method = ownerClass.getMethod(methodName, argsClass); method.invoke(ownerClass.newInstance(), args);
} @Test
public void testRe() throws Exception {
String className = "b_Obj";
String methodName = "c";
String x = null, y = null;
Object[] args = { new String("x"), new String("y") };
Ref r = new Ref();
r.T(className, methodName, args);
}
}
让子类去重写c(x,y)
然后父类.c的时候执行的就是子类的c(x,y)方法了
a_Obj a = null;
a = new b_Obj();
这样吧..
public class a_Obj {
public a_Obj()
{
}
public int c(int a,int b)
{
return a+b;
}
}
b_Obj代码
public class b_Obj extends a_Obj{
public b_Obj()
{
}
public int c(int a,int b)
{
return a*b;
}
}测试类里面代码
a_Obj a = null;
// Add 这里修改
a = new a_Obj();
// Add
System.out.println("result = "+a.c(1, 1));a_Obj 和 b_Obj 类不能修改,测试类里面不能new b_Obj
a_Obj a = null;
try {
a = (a_Obj) Class.forName("com.test.b_Obj").newInstance();
System.out.println("result = " + a.c(1, 1));
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}