同时覆盖具有泛型异常和泛型类型的方法

时间:2016-05-08 16:58:39

标签: java generics

我在覆盖接口中声明的方法时遇到了一些麻烦,该方法使用泛型参数作为其异常,将泛型作为其参数之一。为了说明,我编写了以下代码接口&类

package test;

import java.lang.IllegalArgumentException;
import java.util.List;

interface Interfaz {

    <ARGUMENT_EXCEPTION extends IllegalArgumentException> String metodoA(String[] params) throws ARGUMENT_EXCEPTION;

    <ARGUMENT_EXCEPTION extends IllegalArgumentException> String metodoB(List<String> params) throws ARGUMENT_EXCEPTION;

    <ARGUMENT_EXCEPTION extends IllegalArgumentException> String metodoC(List<String> params) throws ARGUMENT_EXCEPTION;

}

class Implementadora implements Interfaz {
        //This overrides metodoA correctly
        @Override
    public String metodoA(String[] params) throws IllegalArgumentException {
                throw new IllegalArgumentException("ex");
        }

    //This one won't override metodoB, however
    @Override
    public String metodoB(List<String> params) throws IllegalArgumentException {
                throw new IllegalArgumentException("ex");
        }

    //Ugly workaround (with unchecked cast)
    @Override
    public <ARGUMENT_EXCEPTION extends IllegalArgumentException> String metodoC(List<String> params) throws ARGUMENT_EXCEPTION {
                throw (ARGUMENT_EXCEPTION)new IllegalArgumentException("ex");
        }
}

通过javac运行,我得到了

Line: 14
Implementadora is not abstract and does not override abstract method metodoB(java.util.List) in Interfaz

Line: 23
name clash: metodoB(java.util.List) in Implementadora and metodoB(java.util.List) in Interfaz have the same erasure, yet neither overrides the other

Line: 22
method does not override or implement a method from a supertype

请注意,在界面中,metodoAmetodoB之间的唯一区别在于它们的参数。

为什么Implementadora metodoB()不会覆盖Interfaz metodoB(),但覆盖metodoA() <?xml version="1.0"?> <config> <modules> <Amanweb_Catattr> <version>0.1.0</version> </Amanweb_Catattr> </modules> <global> <helpers> <catattr> <class>Amanweb_Catattr_Helper</class> </catattr> </helpers> <models> <catattr> <class>Amanweb_Catattr_Model</class> <resourceModel>catattr_mysql4</resourceModel> </catattr> </models> <resources> <categoryaddattr_setup> <setup> <module>Amanweb_Catattr</module> <class>Mage_Catalog_Model_Resource_Eav_Mysql4_Setup</class> </setup> <connection> <use>core_setup</use> </connection> </categoryaddattr_setup> <categoryaddattr_write> <connection> <use>core_write</use> </connection> </categoryaddattr_write> <categoryaddattr_read> <connection> <use>core_read</use> </connection> </categoryaddattr_read> </resources> </global> </config> 方式有效吗?

1 个答案:

答案 0 :(得分:0)

你不能指望在methodA和methodB中返回字符串,看看你的界面这里是一个编译的版本

class Implementadora implements Interfaz {

@Override
public <ARGUMENT_EXCEPTION extends IllegalArgumentException> String metodoA(String[] params)
        throws ARGUMENT_EXCEPTION {
    // TODO Auto-generated method stub
    return null;
}

@Override
public <ARGUMENT_EXCEPTION extends IllegalArgumentException> String metodoB(List<String> params)
        throws ARGUMENT_EXCEPTION {
    // TODO Auto-generated method stub
    return null;
}

@Override
public <ARGUMENT_EXCEPTION extends IllegalArgumentException> String metodoC(List<String> params)
        throws ARGUMENT_EXCEPTION {
    // TODO Auto-generated method stub
    return null;
}
    //This overrides metodoA correctly

}