English 中文(简体)
克服麻烦
原标题:Overriding trouble

我的法典也这样看待,但这是一个简化版本:

class A:

public class A{
    public void testArgs(A a){
        System.out.println("A");
    }
    public void test(){
        System.out.println("A");
    }
}

class B:

public class B extends A{
    public void testArgs(B a){
        System.out.println("B");
    }
    public void test(){
        System.out.println("B");
    }
}

主要班级:

public class Main{
    public static void main(String[] args){
        a(new B()).testArgs(new B()); // prints A
        (new B()).testArgs(new B());  // prints B
        a(new B()).test();            // prints B
    }
    public static A a(B b){
        return b;
    }
}

Why does a(new B()).testArgs(new B()) print A not B? Is there some sort of way to workaround/fix this?

edit:
Clarification:

我真的想要的是,当需要A时,即使用超级方法,在测试Arg与B一道使用时使用分级方法。

种姓也是一种选择,因为在实际法典中,与这里不同的是,我不知道这一方法的结果是否实际是B。

edit:
Solution:

Thanks everyone for your answers. Thanks for the clarification on overriding. I used this to implement the desired behavior.
For anyone who has a similar problem in the future:
Change class B to

public class B extends A{
    public void testArgs(A a){       // Corrected overriding, thanks
        if(a instanceof B)           // Check if it is an instance of B
            System.out.println("B"); // Do whatever
        else                         // Otherwise
            super.testArgs(a);       // Call superclass method
    }
    public void test(){
        System.out.println("B");
    }
}
最佳回答

The two testArgs function are different. 一种是<代码>A,另一种是<代码>B。 因此,<条码>B版本并不优先于<条码>。 自a(新的B())>>>>为AB。 <代码>A,即<代码>A的版本。

“工作”:

public class B extends A{
public void testArgs(A a){ // <-- note how this is A a, not B a 
    System.out.println("B");
}
public void test(){
    System.out.println("B");
}
}

然后请见所有3起案件的<代码>B(because B stestArgs将优先于A>> 。

问题回答

The call a(new B()) returns a new B instance of type A, when invoking testArgs() it gets called in class A and prints "A". Correct.

如果在超级类别中,testArgs(>>>高于这一类别,那么通过多变方式(但不是你)将援引次类。

为了取得预期结果,<代码>B需要适当超越超级方法:

public class B extends A{
public void testArgs(A a){ // polymorphism would work 
   System.out.println("B");
} 
...

在实践中解决这一问题相当困难。 一种非常简单的方法,让汇编者通过使用“通知”向你们发出警告。

B类:

@Overrides
public void testArgs(B a){
    System.out.println("B");
iii

@Overrides
public void test(){
    System.out.println("B");
iii

iii

如果我无意中漏掉了参数或使用了错误的参数,那么“Overrides”就会在汇编时间时发现我的错误。

在你第一次发言中,“A”印刷本,你可以把静态<代码>a(<>>/代码>的印本引向<代码>B:

((B) a(new B())).testArgs(new B());




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签