由于不期望的匹配类型,无法覆盖子类中的方法 [英] Unable to overwrite method in the subclass because of the undesirably matched type

查看:49
本文介绍了由于不期望的匹配类型,无法覆盖子类中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个班级.我想输出"want MyClass"对于 myObjStr myObjInt .

因此,我只需要子类的方法,并且我想完全替换超类的方法.

但目前,我只获得了"want MyClass"用于 myObjStr .

对于 myObjInt ,我错误地得到了"hate MySuperClass"作为输出(由于 K 不幸地与 int 匹配).

如果不允许我修改 MySuperClass MyMethod ,是否可以进行修复? MySuperClass 来自标准库,因此我不应该对其进行修改.

Main.java

 公共类Main {公共静态void main(String [] args){MyClass< String,String>myObjStr = new MyClass< String,String>();myObjStr.myMethod("a");MyClass< Integer,String>myObjInt = new MyClass< Integer,String>();myObjInt.myMethod(1);}} 

MySuperClass.java

 公共类MySuperClass {public boolean myMethod(int index){System.out.print(讨厌MySuperClass,");返回false;}} 

MyClass.java

 公共类MyClass< K,V>扩展MySuperClass {公共布尔myMethod(K键){System.out.print("want MyClass,");返回false;}} 

解决方案

还应使用泛型对超类进行参数化设置:

 公共类MySuperClass< K>{公共布尔MyMethod(K索引){System.out.print(讨厌MySuperClass,");返回false;}} 

并且子类应使用相同的泛型扩展超类:

 公共类MyClass< K,V>扩展MySuperClass< K>{@Override公共布尔MyMethod(K键){System.out.print("want MyClass,");返回false;}} 

编译器看到您正在调用名为 MyMethod 的方法,该方法传递了 int 类型的参数,并期望返回 boolean .

如果超类的签名不完全是 boolean MyMethod(int)(例如,如果将其重命名为 MyMethod2 ),则编译器不会感到困惑,并且将按照您的期望去实现子类.

然而,在这种情况下,签名是模棱两可的,并且编译器采用超类而不是子类来获得更好"的签名.签名匹配.

NIT:方法名称应以小写字母开头,因此,遵循Java标准约定,使用 myMethod 而不是 MyMethod .


如果无法修改 MySuperClass 进行参数化,则必须在派生类中显式重写super方法:

 公共类MyClass< K,V>扩展MySuperClass {公共布尔MyMethod(K键){System.out.print("want MyClass,");返回false;}@Overridepublic boolean MyMethod(int index){//您的派生类逻辑在这里.}} 

I have 3 classes. I want to have output "want MyClass" for both myObjStr and myObjInt.

Therefore, I only want the method of the subclass, and I would like to replace the superclass method completely.

But currently, I only got "want MyClass" for myObjStr.

For myObjInt, I mistakenly got "hate MySuperClass" as output (since K unluckily matched int in that case).

Is it possible to do the fix if I am not allowed to modify MyMethod of MySuperClass? MySuperClass is from a standard library so I shouldn't modify it.

Main.java

public class Main {

    public static void main(String[] args) {

        MyClass<String, String> myObjStr = new MyClass<String, String>();
        myObjStr.myMethod("a");
    
        MyClass<Integer, String> myObjInt = new MyClass<Integer, String>();
        myObjInt.myMethod(1);
    
    
    }

}

MySuperClass.java

public class MySuperClass {

    public boolean myMethod(int index) {
        System.out.print("hate MySuperClass, ");
        return false;
    }

}

MyClass.java

public class MyClass<K,V> extends MySuperClass {

    public boolean myMethod(K key) {
        System.out.print("want MyClass, ");
        return false;
    }
}

解决方案

Super class should also be parametrized with generics:

public class MySuperClass<K> {

    public boolean MyMethod(K index) {
        System.out.print("hate MySuperClass, ");
        return false;
    }

}

And subclass should extend the super class with the same generic:

public class MyClass<K,V> extends MySuperClass<K> {

    @Override
    public boolean MyMethod(K key) {
        System.out.print("want MyClass, ");
        return false;
    }
}

The compiler sees you are calling a method called MyMethod passing a parameter of type int and expecting a boolean in return.

If the signature of the superclass was not exactly boolean MyMethod(int) (for example if you rename it MyMethod2), then the compiler wouldn't get confused and would go to the implementation of the subclass as you are expecting.

However, in this case the signatures are ambiguous and the compiler takes the superclass instead of the subclass for a "better" match of signature.

NIT: the methods names should start with small letter, hence myMethod instead of MyMethod, following the Java standard conventions.


If you cannot modify MySuperClass to be parameterized, then you will have to explicitly override the super method in your derived class:

public class MyClass<K,V> extends MySuperClass {

    public boolean MyMethod(K key) {
        System.out.print("want MyClass, ");
        return false;
    }

    @Override
    public boolean MyMethod(int index) {
        //Your derived class logic here.
    }
}

这篇关于由于不期望的匹配类型,无法覆盖子类中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆