| Author |
Generics - wildcard?
|
Alexsandra Carvalho
Ranch Hand
Joined: Jul 13, 2007
Posts: 75
|
|
Hello, I don't understand that... Can I use wildcard on type of class definition???
public class Teste<T,X extends Number>{ public static void main ( String args) { Teste<String, ? super Integer> teste = new Teste<String, Number>(); } }
|
 |
Alexsandra Carvalho
Ranch Hand
Joined: Jul 13, 2007
Posts: 75
|
|
I'm so cofused. What's the meaning of to define a class like public class Teste<T,X extends Number>{ and later use like this: Teste<String, ? super Integer> teste = new Teste<String, Number>(); What I can and I can't to do with the second argument?
|
 |
Peter Ricke
Greenhorn
Joined: Dec 07, 2007
Posts: 24
|
|
Try to think about the two thinks completly separated one from another: One is defining a class Tester, the other defining a variable of type Tester When defining a class like you declair that every instance of this class will have to have 2 parameters (of cause you can leave the parameters at all, but if paramters are used...). The first has to be whatever the implementer wants. The second has to extend Number , so could be Integer or Long or... This guarantees that Methods of tester are acting only on Number-Instances, not e.g. on Strings.. If than someone instantiates your class she is doing 3 things: a) declair a variable named teste. This can hold every Teste-object which has parameters String ans Something superior to number. b) create a new Teste-Object with Parameters String and Number c) assign the object to the variable Due to your class-definition the object could also be a new Teste<Integer,Integer>() but you would not be able to assign to the variable. On the other hand, later inb the program, you could instantiate another tester-object eg (new Tester<String,Integer>()) and assign to your variabe tester. This is ok cause Integer is "super" to Integer. Other Instance-types are not possibel to assign to tester,because var 1 has to be a String and var 2 has to be super to Integer and extending Number and this is true only for Number and Integer Hope this helps
|
========<br />class a{<br /> a a(a a){return (a)a;}<br />}
|
 |
 |
|
|
subject: Generics - wildcard?
|
|
|