道歉,但很难尝试搜索<?>.
Java泛型有什么意义?我的理解<A extends B>和<A super B>,但在此之前我从来没有见过自己的这个问号.
我无法解释/理解Java List的泛型类型:
List<? extends Command> myVar = client.performAction(actionParams);
Run Code Online (Sandbox Code Playgroud)
如何? extends Command调用泛型类型,就像它有一个名称一样?究竟是什么类型的?它是一个Command对象吗?或者这是否意味着它只接受扩展的类Command?使用这种结构有什么优势?在什么Java版本中集成了这种类型的构造?
我想知道使用的?在java泛型中.通过研究占位符T和通配符?,我想知道?,经历了几个网站/页面和书籍,但没有理解它.所以我创建了下面的课来研究差异.
import java.util.List;
public class Generics2 {
public <T> void method1(List<T> list){
System.out.println(list);
}
public <T extends Number> void method2(List<T> list){
System.out.println(list);
}
/*public <T super Integer> void method3(List<T> list){
}*///super does not work.
public void method4(List<?> list){
System.out.println(list);
}
public void method5(List<? extends Number> list){
System.out.println(list);
}
public void method6(List<? super Integer> list){
System.out.println(list);
}
public <T> void copy1(List<T> list1, List<T> list2){
//copy elements from list1 to list2
}//It does not span well with copy of one type of …Run Code Online (Sandbox Code Playgroud) 鉴于以下设置:
public class TestType {
public static void main(String[] args) {
List<Constants> list = new ArrayList<>();
accept(list); //Does not compile
}
static void accept(Iterable<MyInterface> values) {
for (MyInterface value : values) {
value.doStuff();
}
}
}
interface MyInterface<T> {
T doStuff();
}
enum Constants implements MyInterface<Integer> {
ONE, TWO, THREE;
@Override
public Integer doStuff() {
return ordinal();
}
}
Run Code Online (Sandbox Code Playgroud)
为什么编译器不接受列表作为accept()的参数?
List延伸Iterable通过Collection,这不是问题.
另一方面,编译器告诉我
incompatible types: java.util.List<enums.Constants> cannot be converted to java.lang.Iterable<enums.MyInterface>
但常量是一个MyInterface ......不是吗?
interface Shape { }
class Circle implements Shape { }
class ShapeContainer<T extends Shape> {
T sh;
public ShapeContainer(T newInstance) {
sh = newInstance;
}
...
}
class Main {
public static void main(String[] a) {
ShapeContainer<Shape> A = new ShapeContainer(new Circle());
ShapeContainer<? extends Shape> B = new ShapeContainer(new Circle());
}
}
Run Code Online (Sandbox Code Playgroud)
ShapeContainer<Shape> A将变量声明为vs的优点和缺点是什么 ShapeContainer<? extends Shape> B
每一项应优先考虑什么条件?
我正在浏览代码并找到以下方法声明.
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
Run Code Online (Sandbox Code Playgroud)
<?>这里的意思是什么?
谢谢.
我正在尝试使用charts4japi,其中一个构造函数具有以下功能:
public static Data newData(List<? extends Number> data)
在我看来,它似乎是某种形式的泛型,但我以前从未见过这种表示法,而且我也不了解。
有时候我会看到看起来像的java代码
Class<?>
Run Code Online (Sandbox Code Playgroud)
我不明白它的正确含义.
如果我看到类似的东西:
ComboBox<Order> box = new ComboBox<>();
Run Code Online (Sandbox Code Playgroud)
然后它明确表示该框只能包含类订单中的对象,但是呢<?>?是等于还是完全不同?