2012-08-08 68 views
2

我可以使用java中的子类对象调用父类重写方法吗?如何在不使用超级关键字的情况下调用超类重写方法和子类对象?

我试过例如

class First1 
{ 
    void show() 
    { 
    String msg="You are in first class"; 
    System.out.println(msg); 
    } 
} 
class second extends First1 
{ 
    void show() 
    { 
    String msg="You are in second class"; 
    System.out.println(msg);   } 
    } 
} 
class CallingMethod extends second 
{ 
    void show() 
    { 
    String msg="You are in the third class"; 
    System.out.println(msg); 
    } 
    public static void main(String[] args) 
    { 
    CallingMethod cm=new CallingMethod(); 
    cm.show(); 
    } 

}

现在告诉我,如果可以打印下面的 “我在第二类人。”通过在示例中使用cm的CallingMethod类的对象,并且不使用超级关键字。

+3

你试过一个例子吗?如果是这样,张贴并让我们知道你有问题。 – kosa 2012-08-08 14:51:50

+6

为什么你不想使用super关键字?你们让我们玩这个游戏的目标是什么? – 2012-08-08 14:53:44

+0

是的,我做了,我有三个类叫做第一,第二和第三,第二扩展第一,第三扩展第二。并且每个类都有一个方法void show(),我创建了第三个类的对象t,并且我想调用并使用对象t打印第二个类的show()方法。我该怎么做@thinksteep – P3M 2012-08-08 14:55:52

回答

4

我假设你的意思是从子类之外调用该方法。

然后不,在java中不可能,因为重写方法意味着改变了对新类有意义的行为。

在课堂内部,您可以在任何情况下使用super关键字。

注意:使用反射,您可以使用对象进行操作,即语言本身不允许。

注:我测试了这个使用反射,它不起作用。但是当你用C使用JNI你也许可以做到这一点...

//does not work 
class YourClass 
{ 
    public static void main(String[] args) throws SecurityException, 
      NoSuchMethodException, IllegalArgumentException, 
      IllegalAccessException, InvocationTargetException 
    { 
     CallingMethod cm = new CallingMethod(); 
     First1 f = new First1(); 
     // Method m = First1.class.getDeclaredMethod("show"); 
     Method m = First1.class.getMethod("show"); 
     m.invoke(f); 
        //output: You are in first class 
     m.invoke(cm); 
        //output: You are in the third class 
    } 

} 
+0

啊,所以*那是他在说什么! +1 – erickson 2012-08-08 15:00:55

+0

好的。你怎么能这么说呢? – P3M 2012-08-08 15:34:17

+0

@ P3M这是一个很难回答的问题。这里是[super]的文档(http://docs.oracle.com/javase/tutorial/java/IandI/super.html)。这就是说我处于这样的位置,我知道(从阅读)语言可以做什么(不仅仅是超级),但我永远无法确定它不能做那些没有记录的事情。也许你的老师想听听Reflection。反思允许你访问私有方法和属性,它可能允许你做你的老师想要的。从这个角度来看,你可以告诉他,如果你使用C,你几乎可以用你的java类来做任何事情。 – 2012-08-08 16:02:02

0

是的,这里有一个例子重写方法:

http://www.cs.umd.edu/~clin/MoreJava/Objects/overriding.html

+0

这样你只能调用方法一级。我想问题是关于不止一个级别。像super.super.foo()一样。我相信这是不可能的。 – 2012-08-08 15:00:17

+0

我没有觉得它有用..因为它使用超级关键字。 – P3M 2012-08-08 15:32:53

0

可以使用super.overriddenMethod()只要你在孩子课堂内部调用它。

+0

是的,我知道,但问题是我不允许使用super关键字,现在呢? – P3M 2012-08-08 15:33:37

0

也许你想是这样的:

class A 
    method() 
class B extends A 
    method() 
class C extends B 
    method() 
    { 
     //call A.method() 
    } 

这在Java中是不可能的为好。你只能调用你的直接超类的方法。你永诺需要使用

super 

编辑:这就是为什么:

class A 
{ 
    private int positionA; 
    void move() 
    { 
    positionA++; 
    } 
    int getPosition() 
    { 
    return positionA; 
    } 
} 
class B 
{ 
    private int positionB; 

    void move() 
    { 
    positionB++; 
    } 
    int getPosition() 
    { 
    return positionB; 
    } 
} 
A a = new A() 
B b = new B() 

如果运行

b.move() 

然后positionB递增。你可以从getPosition()的调用中得到你所期望的。

如果你能 b

它会增加positionA运行

A.move() 

。因此,对b.getPosition()的调用不会返回正确的位置。

如果你有

Class C extends B 

你会绕过B的举动(),如果你能在

this. 

这是同样的问题,因为外部类调用

A.move() 

。你的类会表现得很奇怪,这就是Java开发人员不允许它的原因。

+0

你怎么能这么说? – P3M 2012-08-08 15:32:24

+0

@ P3M ansered通过编辑我的答案的问题... – 2012-08-08 16:53:49

相关问题