java - Why can't I pass an object of type T to a method on an object of type <? extends T>? -
public class container & lt; T & gt; {Private list & amp; Lt; Item & lt;? T & gt; & Gt; item; Private T-value; Public Container (T Value) {this.value = value; } Public Zero addItem (item increases & lt;? T & gt; items) {items.add (item); } Public Zero doActions (Item (lt; items: item increases for items) {item.doAction (value);}}} Public abstract class items {public essence Deleting zero (T item);}
returns the Eclipse error: the method doesAction (Capture # 1-Expands?) In Type Item Argument Argument (T) Is not applicable for
I am reading generic examples and various postings, but I still can not understand why this permission Eclipse does not give any useful suggestions in its proposed reforms, either the variable Take a look at the following programs As you said, (A) line does not compile. If this line was legal then, at runtime, the program will call the When the performance comes later (B), value
is type T, why will not it apply to ? .
Public class cell & lt; T & gt; {Private T-value; Public zero set (tt) {value = t;} Public T find () {return value;}} cell & lt; integer> ci = New cell & lt; integer & Lt;? Number & gt; Extends cn = ci; Cn.set (new double (5.0)); // (A) & lt; - Compilation error here n = ci.get (); // (B) Runtime Error!
double
object to cn.set ()
where cn
is the dynamic type < Code> cell & lt; Integer & gt; . ci.get ()
will return a twice
- which passed in it The declaration of (a) --- ci
says that its get ()
method is guaranteed to return integer
. To prevent this puzzle (which actually breaks JWM's strong typing philosophy), compiler T
to .