简体   繁体   English

是否可以在Java中扩展泛型类参数?

[英]Is it possible to extend a generic class parameter in Java?

Does something like this make any sense at all in Java? 这样的事情在Java中根本没有意义吗?

class A<T extends B> extends T{ 
  int fun1() {
    ....
  }      
}

abstract class B extends X implements C {

}

interface C {
  int fun1();
}

I want class B to extend both X and C ideally. 我希望B类在理想情况下同时扩展X和C。 But since there is no multiple inheritance in Java, I'm trying to come up with a slicker workaround. 但是,由于Java中没有多重继承,因此我试图提出一种精巧的解决方法。

The trick is that the stuff that would be defined in C is defined in Super Class "A" instead. 诀窍在于,将在C中定义的内容改为在“ A”超级类中定义。 The problem is to get A let me use a generic type after "extends" 问题是要让A在“扩展”之后使用通用类型

Thoughts? 有什么想法吗?

No, you can't make a type extend a class specified as a type parameter 不,您不能使类型扩展指定为类型参数的类

Without knowing what A, B, C or X are, it's very hard to recommend an alternative design pattern I'm afraid. 不知道A,B,C或X是什么,恐怕很难推荐一种替代设计模式。

What you're trying to do is not going to work - Java generics have nothing to do with inheritance. 您试图做的事情行不通-Java泛型与继承无关。 There is no multiple inheritance, you'll have to make your peace with that :-) 没有多重继承,您必须与之保持和平:-)

You can declare multiple interfaces and use aggregation to "fake" multiple inheritance. 您可以声明多个接口并使用聚合来“伪造”多个继承。

As others have said, generics won't help you with multiple inheritance of implementations. 正如其他人所说,泛型将无法帮助您实现多个继承。

My workaround for not repeating myself due to multiple inheritance is usually to write the common implementation in a static method in the superclass, and delegate to that from the places where it is needed. 对于多重继承而不重复自己的解决方法,通常是在超类的静态方法中编写通用实现,然后从需要的地方委托该实现。

The simplest example would be like this: 最简单的示例如下所示:

class A
{
}

class B extends A 
{
}

interface C
{
    void foo();
}

class CBase extends A implements C
{
    public void foo()
    {
        sFoo(this);
    }

    static void sFoo(C c)
    {
        // Implement foo here
    }
}

class D extends B implements C
{
    public void foo()
    {
        CBase.sFoo(this);
    }
}

Note that to make this useful there would be more operations inherited by D from B and A, which are not shown in this example. 请注意,要使此功能有用,D会从B和A继承更多的操作,在此示例中未显示。

The type parameter is only a placeholder for the compile-time-only generics, it is an actual type. type参数只是“仅编译时”泛型的占位符,它是实际类型。

Trying to come up with a workaround solution for multiple inheritance in a language that does not support it is probably a good indicator that the flaw is in the design. 尝试提出一种不支持该语言的多重继承的变通方法,这很可能表明设计存在缺陷。 Saying A<T extends B> extends T will obviously fail as it doesn't even mean what you're hoping it to mean - <T extends B> means only that T is of a type that is a B . A<T extends B> extends T显然会失败,因为它甚至并不意味着您希望它代表什么- <T extends B>意味着仅T 是属于 B的类型。 Appending extends T does not mean anything because T is not defined in that class. 追加extends T并不意味着任何事情,因为T没有在该类中定义。 Again, this is a compile-time type - the actual type of T is not directly available during runtime. 同样,这是一个编译时类型-T的实际类型在运行时不直接可用。

Are you really sure that what is most properly achieved via multiple inheritance as opposed to composition? 您真的确定通过多重继承而不是组合最正确地实现了什么? It could also be possible that you're trying to get A to do too many things. 您也可能试图让A做太多事情。

A more concrete example might allow others to give more feedback on the design itself. 一个更具体的示例可以使其他人对设计本身提供更多反馈。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM