Java Generics restrict to interface

后端 未结 3 2456
时光取名叫无心
时光取名叫无心 2021-02-20 05:44

I\'m not sure if this is possible or not, but what I want to accomplish is this:

public static  B makeB(A thing) {...}
         


        
3条回答
  •  隐瞒了意图╮
    2021-02-20 06:23

    I think the problem here is that you want to return a B from this method.

    You specify B as a type parameter, but it never appears anywhere else in the method signature.

    How is the compiler supposed to infer the return type from the arguments????

    There is no opportunity for the client code to specify what B is.

    It seems like you should return either a SomeClass or an A.

    Either one can be a B under the hood, but should appear as a SomeClass or an A to the client code.

提交回复
热议问题