[英]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.