[英]Role of Type Inference over Type Erasure - Wildcard bounding can be possible if Type Inference works well over Type Erasure?
Working on a specific need. 针对特定需求进行工作。 Most of online tutorial talks about applying wildcard implementation with Collections.
大部分在线教程都讨论了将通配符实现与“集合”一起应用。 In below example, extends works OK but when I apply super with wildcard bounding getting error.
在下面的示例中,extends可以正常工作,但是当我在通配符边界上应用super时出现错误。 I wish to restrict a method with it super type like said in the below example.
我希望用以下示例中所述的方法来限制其超类型的方法。 Is there any limitation with super that I supposed to know.
我应该知道的超级有什么限制吗?
class SuperClass3 {
public void display() {
System.out.println("This is display3 method");
}
}
class SuperClass2 extends SuperClass3 {
public void display() {
System.out.println("This is display2 method");
}
}
class SuperClass1 extends SuperClass2 {
public void display() {
System.out.println("This is display1 method");
}
}
Extends works well (with Type bounding NOT with wildcard bounding)... 扩展效果很好(类型限制不带通配符限制)...
public <T extends SuperClass2> void displayOutput(T obj) {
obj.display();
}
Try to do the same with Super not working. 尝试对Super无法正常工作。 Throw compile error on method signature.
在方法签名上引发编译错误。
public <T super SuperClass2> void displayOutputWithSuper(T obj) {
//obj.display();
}
Complete Example ...
package com.tutorial.generic.bounds.wildcard;
import java.util.List;
public class UpperBoundWildcardExample {
class SuperClass3 {
public void display() {
System.out.println("This is display3 method");
}
}
class SuperClass2 extends SuperClass3 {
public void display() {
System.out.println("This is display2 method");
}
}
class SuperClass1 extends SuperClass2 {
public void display() {
System.out.println("This is display1 method");
}
}
public <T extends SuperClass2> void displayOutput(T obj) {
obj.display();
}
public void addData(List<? extends SuperClass2> data) {
}
public <T super SuperClass1> void displayOutputWithSuper(T obj) {
obj.toString();
}
/*
* This wont work
*
* public void addData(<? extends SuperClass2> data){
*
* }
*/
public static void main(String[] args) {
UpperBoundWildcardExample obj = new UpperBoundWildcardExample();
// Oops!!! Error
// obj.displayOutput(obj.new SuperClass3());
// It suppports SuperClass2 & which extends SuperClass2
obj.displayOutput(obj.new SuperClass2());
obj.displayOutput(obj.new SuperClass1());
}
}
@Shaan This might be helpful @Shaan这可能会有帮助
Bounding generics with 'super' keyword 用'super'关键字绑定泛型
let's say that you have this generic method declaration: 假设您有以下通用方法声明:
<T super Integer> void add(T number) // hypothetical! currently illegal in Java
And you have these variable declarations: 并且您具有以下变量声明:
Integer anInteger
Number aNumber
Object anObject
String aString
Your intention with (if it's legal) is that it should allow add(anInteger), and add(aNumber), and of course add(anObject), but NOT add(aString). 您的意图(如果合法)是它应该允许add(anInteger)和add(aNumber),当然还允许add(anObject),但不允许add(aString)。 Well, String is an Object, so add(aString) would still compile anyway.
好吧,字符串是一个对象,所以add(aString)仍然可以编译。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.