简体   繁体   English

为什么通用类可以实现具有不同类型参数的接口?

[英]Why is it possible for a generic class to implement interface with different type parameters?

For example like in this section 例如在本节中

interface MyGenericsI<A, B, C>{
    public A getA();
    public B getB();
    public C getC();
}

abstract class MyGenerics<D> implements MyGenericsI{
}

Because the fields or methods that make use of the class' generic type(s) might be unrelated to the methods of the interface. 因为使用类的泛型类型的字段或方法可能与接口的方法无关。 Contrived example: 人为的例子:

public class Company<P> implements List<Employee> {
    public P produceProduct();
    public Employee get(int index) { ... }
    ...
}

Here, a Company<P> is capable of producing products of the generic type P , so that you could eg create a Company<Computer> that makes computers. 在这里, Company<P>能够生产通用类型P ,因此您可以例如创建制造Company<Computer>Company<Computer> However, a company also has employees, so you could make it implement the methods of List<Employee> in order to access the employees of the company (composition would be better in this example, though). 但是,公司也有员工,因此您可以使其实现List<Employee>的方法以便访问公司的员工(尽管在此示例中,组合会更好)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么不可能使用不同的类型参数多次实现通用接口? - Why is it impossible to implement a generic interface multiple times with different type parameters? 抽象类实现了通用的接口类型 - Abstract class implement a generic Interface Type 是否可以在接口类中访问通用类型的方法? - Is it possible to access methods of generic type in an interface class? 具有用于实例化其他通用接口的通用类型参数的Java类 - Java class with a generic type parameter that instantiates a different generic interface 如何在具有相同数据类型参数的另一个泛型类(或接口)中使用泛型类(或接口)而无需强制转换 - How to use generic class (or interface) in another generic class (or interface) with the same data type parameters without casting 如何从实现回调接口的类中隐藏通用类型参数 - How to hide Generic type parameters from implementing class of a Callback interface 获取包含类型参数的 Generic 接口的 Class 实例 - Obtain Class instance of Generic interface including type parameters 在Java中使用通用类型实现通用接口 - Implement generic interface with generic type in Java 在泛型抽象类中实现泛型接口 - Implement generic interface in a generic abstract class 是否可以使用参数类型实现类 <Generic extends X> ? - Is it possible to implement a class with a parameter of type <Generic extends X>?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM