2013-02-28 60 views
0

我的主要问题是围绕何时使用,是什么样的区别以下时类,抽象,接口组合:使用的类,抽象,接口组合扩展

  1. <E>

  2. <E extends Interface>

  3. <? extends Interface>

下面显示的是带有一些代码签名的详细问题:

此代码使用Guava Forwarding Decorators来定义特定集合。

基本接口:

public interface AnimalSetInterface<E extends AnimalI> extends Set<E> 

这工作:

public interface AsiaI<E extends AnimalI> extends AnimalSetInterface<E> 

下面给出了一个错误:

public interface AsiaI<E> extends AnimalSetInterface<E> 

Bound mismatch: The type E is not a valid substitute for the bounded parameter of the type AnimalSetInterface

什么我想了解的是,如果我有在基本界面上指定我只想要<E extends AnimalI>那么为什么我必须再次指定AsiaI

我想了解泛型,同时最小化代码。

此外,如果两个类有这样的代码是否有合并/最小化的好办法(删除/泛型化样板代码)它:
亚洲:

public Asia(final ImmutableSet<E> animalSet){ 
    super(animalSet); 
} 

public static <E extends AnimalI> AsiaI<E> of(final ImmutableSet<E> animalSet){ 
    return new Asia(animalSet); 
} 

非洲:

public Africa(final ImmutableSet<E> animalSet){ 
    super(animalSet); 
} 

public static <E extends AnimalI> AfricaI<E> of(final ImmutableSet<E> animalSet){ 
    return new Africa(animalSet); 
} 

回答

3
public class Africa<E extends AnimalI> extends AnimalSetAbstract implements AfricaI 

public class Asia<E> extends AnimalSetAbstract implements AsiaI 

区别在于,在第一种情况下,您的ge neric类型必须扩展AnimalI 在第二种情况下,您的泛型类型可以是任何类。

+0

这就是我的想法,但下面的用法给出了错误(这是我想要的): 'final AsiaI a = new Asia(ImmutableSet.of()); \t // 不应该工作# – 2013-02-28 21:56:49

+0

@PranavShah你能否请你用你想要的行为更新你的问题。到目前为止我还不清楚你的问题是什么。 – psabbate 2013-02-28 22:03:08

+0

进一步检查,我看到发生了什么事。在我的测试代码中,我使用接口而不是类,因此我得到了错误,但现在我想我明白发生了什么 – 2013-02-28 22:03:19