猿问

Java 不能用不同的参数继承

我在实际继承接口时遇到了麻烦。我一直以错误结束


错误:NameableContainer 不能用不同的参数继承:<Friend> 和 <>


我有以下接口:


public interface Nameable


public interface Name


public interface Friend extends Nameable


public interface NameableContainer<T extends Nameable> {

    void add(Name name, Name prevName);


    void remove(Nameable nameable);


    T findByName(Name name);

}


public interface FriendContainer extends NameableContainer<Friend>

我还有一个继承 NameableContainer 的抽象类。


public abstract class NameableMap implements NameableContainer {

    public void add(Name name, Name prevName) { /* do stuff*/ }


    public void remove(Nameable nameable) { /* do stuff*/ }


    public Nameable findByName(Name name) { /* do stuff*/ }

}

最后,试图把这些放在一起


public class Friends extends NameableMap implements FriendContainer

我错过了什么?


温温酱
浏览 347回答 2
2回答

狐的传说

当您说时,您正在使用原始类型public abstract class NameableMap implements NameableContainer这会在编译器中抛出所有东西;除非您的代码最初是为 java 1.4 或更早版本编写的,否则原始类型是错误的。当你实现一个指定类型参数的接口时,你要么需要提供一个具体类型,要么如果还不知道,你可以引入另一个类型参数并将它传递给超类型。请参阅:什么是原始类型,为什么我们不应该使用它?因此,您在实现时指定了一个可变的泛型类型参数NameableContainer(因为您还不知道具体类型,因为它NameableMap也应该适用于所有 Nameables。public abstract class NameableMap<T extends Nameable> implements NameableContainer<T>//&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^^^^^^^^^^^^^^^^^^^^&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^^^

摇曳的蔷薇

将整个程序中的所有代码移到一个类中并放在一行
随时随地看视频慕课网APP

相关分类

Java
我要回答