java - Why can't I pass an object of type T to a method on an object of type <? extends T>? -


In Java, let me assume that I have a container that has a list of class items:

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 value is type T, why will not it apply to ? .

"post-text" itemprop = "text">

Take a look at the following programs

  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!  

As you said, (A) line does not compile. If this line was legal then, at runtime, the program will call the double object to cn.set () where cn is the dynamic type < Code> cell & lt; Integer & gt; .

When the performance comes later (B), 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 .

spreads

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -