java 泛型上下限,了解上限和下限?在Java泛型中

I am really having a tough time understanding the wild card parameter. I have a few questions regarding that.

? as a type parameter can only be used in methods. eg: printAll(MyList extends Serializable>) I cannot define classes with ? as type parameter.

I understand the upper bound on ?. printAll(MyList extends Serializable>) means: "printAll will print MyList if it has objects that implement the Serialzable interface."

I have a bit of an issue with the super. printAll(MyList super MyClass>) means: "printAll will print MyList if it has objects of MyClass or any class which extends MyClass (the descendants of MyClass)."

Correct me where I went wrong.

In short, only T or E or K or V or N can be used as type parameters for defining generic classes. ? can only be used in methods

Update 1:

public void printAll(MyList super MyClass>){

// code code code

}

Accordint to Ivor Horton's book, MyList super MyClass> means that I can print MyList if it has objects of MyClass or any of the interfaces or classes it implements. That is, MyClass is a lower bound. It is the last class in the inheritance hierarchy. This means my initial assumption was wrong.

So, say if MyClass looks like:

public class MyClass extends Thread implements ActionListener{

// whatever

}

then, printAll() will print if

1. There are objects of MyClass in the list

2. There are objects of Thread or ActionListener in the List

Update 2:

So, after having read the many answers to the question, here is my understanding:

? extends T means any class which extends T. Thus, we are referring to the children of T. Hence, T is the upper bound. The upper-most class in the inheritance hierarchy

? super T means any class / interface which is super of T. Thus we are referring to all the parents of T. T is thus the lower bound. The lower-most class in the inheritance hierarchy

解决方案

? as a type parameter can only be used in methods. eg: printAll(MyList extends Serializable>) I cannot define classes with ? as type parameter.

A wildcard (?) isn't a formal type parameter, but rather can be used as a type argument. In the example you give, ? extends Serializable is given as a type argument to the generic type MyList, of the printAll method's parameter.

Methods can also declare type parameters like classes, for example:

static void printAll(MyList myList)

I understand the upper bound on ?. printAll(MyList extends Serializable>) means printAll will print MyList if it has objects that implement the Serialzable interface

More accurately, it means a call to printAll will compile only if it is passed a MyList with some generic type that is or implements Serializable. In this case it would accept a MyList, MyList, etc.

I have a bit of an issue with the super. printAll(MyList super MyClass>) means printAll will print MyList if it has objects of MyClass or any class which extends MyClass (the descendants of MyClass)

A wildcard bounded with super is a lower bound. So we could say a call to printAll will compile only if it is passed a MyList with some generic type that is MyClass or some super-type of MyClass. So in this case it would accept MyList, e.g. MyList, or MyList.

So, say if MyClass looks like:

public class MyClass extends Thread implements ActionListener{

// whatever

}

then, printAll() will print if

There are objects of MyClass in the list

There are objects of Thread or ActionListener in the list

You're on the right track. But I think saying e.g. "it will print if there are objects of MyClass in the list" is problematic. That makes it sound like you're defining runtime behavior - generics are all about compile time checks. For example wouldn't be able to pass a MyList as an argument for MyList super MyClass>, even though it might contain instances of MyClass, by inheritance. I would reword it to:

A call to printAll(MyList super MyClass>) will compile only if it is passed a:

MyList

MyList

MyList

MyList

MyList

MyList

MyList super X> where X is MyClass, Thread, Runnable, ActionListener, EventListener, or Object.

So, after having read the many answers to the question, here is my

understanding:

? extends T means any class which extends T. Thus, we are referring to

the children of T. Hence, T is the upper bound. The upper-most class

in the inheritance hierarchy

? super T means any class / interface which is super of T. Thus we are

referring to all the parents of T. T is thus the lower bound. The

lower-most class in the inheritance hierarchy

Close, but I wouldn't say "children of T" or "parents of T", since these bounds are inclusive - it would be more accurate to say "T or its subtypes", and "T or its supertypes".

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值