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

解决方案 »

  1.   

    c(x,y)是b中特有的方法,用父类对象怎么可以调用子类中的特有方法呢?
      

  2.   

    其它类都不能修改,只能修改
    a_Obj a = null;
    a = new a_Obj();
    附近的代码
      

  3.   

    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);
    }
    }
      

  4.   

    父类写一个c(x,y)
    让子类去重写c(x,y)
    然后父类.c的时候执行的就是子类的c(x,y)方法了
      

  5.   

    LZ你想写的应该是
    a_Obj a = null;
    a = new b_Obj();
    这样吧..
      

  6.   

    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
      

  7.   

    别说我赖皮 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();
    }
    }