有关Java泛型的一些问题 [英] Some questions about Java Generics

查看:106
本文介绍了有关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泛型的一些问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆