有什么区别?和T在类和方法签名? [英] what is the difference between ? and T in class and method signatures?

查看:87
本文介绍了有什么区别?和T在类和方法签名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么

  public interface ArrayOfONEITEMInterface< T extends ONEITEMInterface> {
public List< T> getONEITEM();
}

编译但不是

  public interface ArrayOfONEITEMInterface <?扩展ONEITEMInterface> {
public List<?> getONEITEM();
}

有什么区别?和T中的类和方法签名?

解决方案

是通配符并且意味着 ONEITEMInterface 的任何子类包括其自身。
$ b

T ONEITEMInterface 的特定 code>在这种情况下。

因为是通配符,所以在你的方法声明中的类声明和中,因此它不会被编译。只需列表<?>






第一种场景 >表示整个类可以正确处理一个类型的 Bar 每个实例

 界面Foo< T延伸栏> {
列表< T>得到();

第二种场景允许每个实例可在任何子类型 Bar

上进行操作

 接口Foo {
列表< ;?扩展Bar> get()
}


why does

public interface ArrayOfONEITEMInterface <T extends ONEITEMInterface>{
    public List<T> getONEITEM();
}

compile, but not

public interface ArrayOfONEITEMInterface <? extends ONEITEMInterface>{
    public List<?> getONEITEM();
}

what is the difference between ? and T in class and method signatures?

解决方案

? is a wildcard and means any sublass of ONEITEMInterface including itself.

T is a specific implementation of ONEITEMInterface in this case.

Since ? is a wildcard, there is no relation between your ? in the class declaration and the ? in your method declaration hence it won't compile. Just List<?> getONEITEM(); will compile though.


The first scenario means the entire class can handle exactly one type of Bar per instance.

interface Foo<T extends Bar> {
     List<T> get();
}

The second scenario allows each instance to operate on any subtype of Bar

interface Foo {
     List<? extends Bar> get()
}

这篇关于有什么区别?和T在类和方法签名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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