本文介绍了泛型,方法签名,分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有这样的方法签名:



void doSomething(List< TypeA> typeAs){...}



列表与LT;类型A<&的TypeB GT;> getTypeBTypeAs(){...}



但是如果我尝试并调用

doSomething(getTypeBTypeAs());



我得到一个编译错误:方法doSomething(List)in类型...不适用于参数(List>)



但是,如果我更改 doSomething to



void doSomething(List< TypeA<>> typeAs){...}



它仍然不起作用,但

void doSomething(List typeAs) {...}



很明显,它可以作为我绕过泛型。



这似乎很奇怪。



有人可以填我吗?



另外,在这种情况下, code> doSomething 来处理任何包含任何泛型类型的TypeAs的List; undefined,TypeB,TypeC等。



谢谢。

解决方案

泛型类 TypeA< TypeB> 是与 TypeA 不同的类型。您不能传入类型 TypeA< TypeB> 的参数,其中方法需要 TypeA 。 TypeA< TypeB> 是与 TypeA< TypeC> 不同的类型,所以应用了相同的约束。



经典示例(来自 Effective Java,2nd ( Container< Animal> )以及 Animal 我们有狮子和蝴蝶。现在,如果你有一个方法

  void func(Animal animal); 

它会接受狮子和蝴蝶。然而,这个函数

  void func(Container&Animal AnimalContainer); 

不会接受 Container< Lion> ,既不是容器< Butterfly> 。意识到一个用于保护狮子的强大笼子不会阻止蝴蝶飞走,反之亦然,一个厚而薄的蝴蝶网不会阻挡狮子。

如果您确定任何种类的动物容器都适合您,请声明您的功能如下:

  void func (Container<?extends Animal> animalContainer); 

回到您的案例,我猜测只有接受 List< TypeA> 和 List< TypeA< TypeB>> 会是这样的:

  void doSomething(List<?> list); 


I thought I understood this but obviously not...

I have a method signature like so:

void doSomething(List<TypeA> typeAs){...}

List<TypeA<TypeB>> getTypeBTypeAs(){...}

but if I try and call

doSomething(getTypeBTypeAs());

I get a compile error: "the method doSomething(List) in the type ... is not applicable for the arguments (List>)"

however if i change the sig of doSomething to

void doSomething(List<TypeA<?>> typeAs){...}

it still doesn't work, but

void doSomething(List typeAs){...}

obviously it works as i bypass generics.

which seems odd.

Can someone fill me in?

Also, in this case I'd like doSomething to work with any List containing TypeAs of any generic type; undefined, TypeB, TypeC etc.

thanks.

A generic class TypeA<TypeB> is a different type from TypeA. You can't pass in a parameter of type TypeA<TypeB> where the method expects a TypeA. Also TypeA<TypeB> is a different type from TypeA<TypeC>, so the same constraints apply.

The classic example (from Effective Java, 2nd Ed. AFAIR) is: we have containers for animals (Container<Animal>) and as subclasses of Animal we have Lion and Butterfly. Now, if you have a method

void func(Animal animal);

it will accept both lions and butterflies. However, this function

void func(Container<Animal> animalContainer);

will not accept a Container<Lion>, neither a Container<Butterfly>. Do realize that a strong cage useful for keeping lions safely would not stop butterflies from flying away, and vice versa a thick but light net to hold butterflies would not stand a chance against a lion.

If you really are sure that any kind of animal container suits you, declare your function like this:

void func(Container<? extends Animal> animalContainer);

Back to your case, I guess the only method to accept both List<TypeA> and List<TypeA<TypeB>> would be something like this:

void doSomething(List<?> list);

这篇关于泛型,方法签名,分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-09 16:20