If you don't talk nonsense, go to the topic.
Two conceptual upper limit and lower limit wildcards
Let's look at a program:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1525015628-0.jpg "title =" CZJ6) DR % GSBG9PNPMN1A ~ 78. jpg "alt =" 215256386.jpg"/>
Upper Limit wildcard <? Extends Object>)
Lower limit wildcard <? Super Integer>)
To be honest, these concepts are really a headache. If they are not the ones that need to take the exam, I don't think it is necessary to remember them. I just want to know how to use them, next, I will help you analyze the cause of the above error to better understand it.
First, we can see that the 8th rows are generic <? Extends Object>, how do you understand this? I read the online materials and thought it was hard to understand. Well, it may be that my IQ was suppressed. Next I will use what I understand as a simple example. Actually, you can understand this.
It is equivalent to <?>? Represents any class) But he gives? A condition is added, that is, this class must inherit the Object,
At this time, someone will ask, isn't all classes inheriting the Object class? Isn't it possible to add instances of all classes? Why, will there be an error in line 11? Well, that's what I thought at the time, so I got bored for a long time and thought about it again.
For example? Can I replace it with an Integer? Extends Object, so can we add (new String ("asd"); add a String to list1? Obviously, this is not acceptable. So is String an extends Object? Yes.
So we don't know about it? So we can replace "?" with different types. Therefore, instances of any class cannot be added to list1. So Lines 9, 10, and 11 failed.
Next we can think about what can be passed in? Yes, it is null. No matter what type you are, you must be a type. If I add a null value to list1, no error will occur in any situation.
Next we can talk about the result, that is, Lines 13, 14, and 15.
Because we cannot judge? So we cannot accurately give it a type. But we know that its parent class is an Object, so defining a variable with the Object can certainly store the retrieved Object.
As for the lower limit wildcard, It is similar.
<? Super Integer>
You can use the above method to determine, first use <?> To replace <? Super Integer>, and then think ,? In fact, a restriction is added. The condition is that it is the parent class of the Integer class.
If you are skilled, explain it directly <? Super Integer>, the input parameter must be of the Integer character type, but there are many Integer parent types, so we cannot determine which one is, but what we can determine is, it is the parent class of Integer, so you can add an Integer instance to list2.
When receiving the message, we still cannot determine accurately? Which class is used, but what we know is that it must be a subclass of the Object. Therefore, it is okay to define an Object reference variable pointing to the retrieved class.
Another key issue is the new ArrayList. <?> (); Medium? What is entered.
First, you cannot directly use "?", Or <? Extends Object> this type of tag with wildcards will report an error.
Then you should fill in a specific class name, but your class name must meet the preceding constraints.
For example, if the value of String in line 1 is true, <? Extends Object> This is because String inherits the Object.
In addition, if you are a specific class, you must be followed by the same specific class as the previous one, not its subclass, not other classes.
I have mentioned a lot. Although I try to use a very popular language, please leave a message if you have any questions.