Tôi muốn làm điều này:Required loại là giống như tìm thấy loại
Foo<String> foo = new Foo<>();
Foo<String>.Bar fooBar = foo.new Bar();
fooBar.doSomething("this works!");
Nhưng sau đó 2 dòng đầu tiên trong một dòng duy nhất như:
Foo<String>.Bar fooBar2 = new Foo<>().new Bar();
fooBar2.doSomething("The above line gives: incompatible types. Required: Foo.Bar Found: Foo.Bar");
Nhưng dòng đầu tiên thất bại. Tôi nhận được:
các loại không tương thích.
Yêu cầu: Foo.Bar
Tìm thấy: Foo.Bar
Tại sao vậy?
public class Foo<T> {
public class Bar extends SomeOtherClass<T> {
public void doSomething(T t) {
}
}
}
Và lớp cuối cùng:
public class SomeOtherClass<T> {
}
Một khởi đầu tốt được đọc về suy luận kiểu, xem doc http://docs.oracle.com/javase/7/docs/technotes/guides/language/type-inference-generic-instance-creation.html và https : //docs.oracle.com/javase/tutorial/java/generics/genTypeInference.html – MrSimpleMind
thông báo lỗi xấu. trình biên dịch gì? – ZhongYu