有关Java泛型的一些问题

编程入门 行业动态 更新时间:2024-10-25 00:29:41
本文介绍了有关Java泛型的一些问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

interface Foo< FooType extends Foo< FooType>>假设我有以下接口并实现类: { FooType copy(); } class Bar实现Foo< Bar> { public bar copy(){ return new Bar();

如果我尝试这样做:

public< FooType extends Foo< FooType>> FooType getFoo(){ return new Bar(); }

我得到编译错误类型不匹配:无法从Bar转换为FooType 。 为什么?

我可以通过像下面这样重写函数来修复:

@SuppressWarnings(unchecked) public< FooType extends Foo< FooType>> FooType getFoo(){ return(FooType)new Bar(); }

但是让我们说我有这样的功能:

public< FooType extends Foo< FooType>> void printFoo(FooType foo){ System.out.println(foo.toString()); }

如果我尝试这样做:

printFoo(getFoo());

我收到以下编译错误:

绑定不匹配:对于参数(Foo< Foo< FooType>>),通用方法printFoo(FooType)不适用。推断的类型Foo< Foo< FooType>> 不是有界参数的有效替代< FooType extends Foo< FooType>>

WAT? getFoo()的返回类型与参数类型的printFoo()!

是这些错误还是我错过了什么?

解决方案

知道了!感谢meriton回答了这个问题的这个版本:

我需要通过 printFoo(getFoo()) -part对编译器进行宝贝步骤:

public< FooType extends Foo< FooType>> void doPrint(){ FooType foo = getFoo(); printFoo(foo); }

,而是调用 doPrint()然后,一切正常。

所以,我猜即使为什么? 在中间可能是我缺乏理解(感谢Bhesh清理那部分),WAT?在最后是有道理的,因为这真的应该只是工作......例如,由于某种原因,在上面的代码中为foo的变量定义内联并不正确。

Let's say I have the following interface and implementing class:

interface Foo<FooType extends Foo<FooType>> { FooType copy(); } class Bar implements Foo<Bar> { public Bar copy() { return new Bar(); } }

If I try to do this:

public <FooType extends Foo<FooType>> FooType getFoo() { return new Bar(); }

I get the compile error "Type mismatch: cannot convert from Bar to FooType". Why?

I can "fix" this by rewriting the function like this:

@SuppressWarnings("unchecked") public <FooType extends Foo<FooType>> FooType getFoo() { return (FooType) new Bar(); }

But let's say I have a function like this:

public <FooType extends Foo<FooType>> void printFoo(FooType foo) { System.out.println(foo.toString()); }

If I try to do this:

printFoo(getFoo());

I get the following compile error:

Bound mismatch: The generic method printFoo(FooType) is not applicable for the arguments (Foo<Foo<FooType>>). The inferred type Foo<Foo<FooType>> is not a valid substitute for the bounded parameter <FooType extends Foo<FooType>>

WAT? The return type of getFoo() is literally identical to the argument type of printFoo()!

Are these bugs or am I missing something?

解决方案

Got it! Thanks to meriton who answered this version of the question:

How to replace run-time instanceof check with compile-time generics validation

I need to baby-step the compiler through the printFoo(getFoo())-part by doing this:

public <FooType extends Foo<FooType>> void doPrint() { FooType foo = getFoo(); printFoo(foo); }

and instead call doPrint() then.

Then everything works fine.

So, I guess even though the "Why?" in the middle may have been my lack of understanding (thanks to Bhesh for clearing up that part), the "WAT?" at the end was justified since this really should just work... For example, it's not OK, for some reason, to inline the variable definition for "foo" in the code above...

更多推荐

有关Java泛型的一些问题

本文发布于:2023-10-28 17:57:20,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!