Mastering the wildcard character "? "For use.
Master settings for restricted generics.
Mastering the limitations of generics and subclass inheritance.
Specific content 1.1 matches any type of wildcard character
reference passing (up-down) of an object in development is the most common, but in a generic operation, the generic type must match to be passed when the reference is passed, otherwise it cannot be passed.
For example, there are no generic type matches, one string and one object type.
PackageThread1;classInfo<t>{ PrivateT var;//Defining generic Variables Public voidSetVar (T var) { This. var =var; } PublicT GetVar () {return This. var; } Public String toString () {//Direct printing return This. var.tostring (); }}; Public classdemo1{ Public Static voidMain (String args[]) { Info <String> i = new info<string> (); Using string as a generic typeI.setvar ("Mldn");//Set ContentFun (i); Passes the I object of the string generic type to the temp of the object generic type. } Public Static voidFun info<object> temp) {//receives an Info object for the object generic type System.out.println ("content:" + temp); }};
Compilation error occurred.
Exception in thread "main" java.lang.Error:Unresolved compilation Problem: The ' method fun ' (Infofor the arguments (info<string>) at Thread1.demo1.main (Demo1.java:18)
When a generic object is passed by reference, the type must be the same, and if it is not, you can cancel the generic of the info parameter in the fun method (into void fun (Info temp)). 、
The above does improve the functionality, but it does not seem to be appropriate, after all, the generic was previously specified.
The above procedure uses the "Info<?>" code form in the Fun () method, which means that any generic type object can be used, so the fun () method definition is reasonable, but there are also areas to be aware of using the above method.
That is, if you use the? "When you receive a generic object, you cannot set the content that is specified by the generic type .
classInfo<t>{ PrivateT var;//Defining generic Variables Public voidSetVar (T var) { This. var =var; } PublicT GetVar () {return This. var; } PublicString toString () {//Direct Printing return This. var.tostring (); }}; Public classgenericsdemo14{ Public Static voidMain (String args[]) {Info<String> i =NewInfo<string> ();//using string as a generic typeI.setvar ("Mldn");//Set ContentFun (i); } Public Static voidFun (info<?> temp) {//can receive arbitrary generic objectsSystem.out.println ("Content:" +temp); }};
If you use the? "means you can receive arbitrary content, but this content cannot be directly used to make use of"? Modify the modified generic object . There will be problems as follows:
PackageThread1;classInfo<t>{ PrivateT var;//Defining generic Variables Public voidSetVar (T var) { This. var =var; } PublicT GetVar () {return This. var; } PublicString toString () {//Direct Printing return This. var.tostring (); }}; Public classdemo1{ Public Static voidMain (String args[]) {Info<?> i =NewInfo<string> ();//using string as a generic type I.setvar ("mldn"); Setting the content, there will be an error, because "? "Wildcard-decorated objects can only be received, cannot be modified, that is, cannot be set. }};
Operation Result:
Exception in thread "main" java.lang.Error:Unresolved compilation Problem: The method SetVar (capture# for the arguments (String) at Thread1.demo1.main (demo1.java:17)
When using the? "Can only be received and cannot be modified.
1.2 Restricted generics
Before setting the generic, you can actually set it arbitrarily, as long as the class is set. In Java generics, however, you can specify the upper and lower bounds of a generic type.
Set upper Limit
classInfo<t>{ PrivateT var;//Defining generic Variables Public voidSetVar (T var) { This. var =var; } PublicT GetVar () {return This. var; } PublicString toString () {//Direct Printing return This. var.tostring (); }}; Public classgenericsdemo17{ Public Static voidMain (String args[]) {Info<Integer> I1 =NewInfo<integer> ();//generic Object declaring an integerinfo<Float> i2 =NewInfo<float> ();//generic Object declaring floatI1.setvar (30);//set integer, auto-boxingI2.setvar (30.1f);//set decimal, auto-boxingFun (I1); Fun (I2); } Public Static voidFuninfo< extends number> Temp) {//Can only receive subclasses of number and numberSystem.out.print (temp + ",") ; }};
Run successfully. However, if the descendant's generic type is string, it is not possible because string is not a number subclass.
Use the generic upper bound in the class.
PackageThread1;classinfo<T extends number>{//generics can only be numeric types here PrivateT var;//Defining generic Variables Public voidSetVar (T var) { This. var =var; } PublicT GetVar () {return This. var; } PublicString toString () {//Direct Printing return This. var.tostring (); }}; Public classdemo1{ Public Static voidMain (String args[]) {Info<Integer> I1 =NewInfo<integer> ();//generic Object declaring an integer }};
If you set the string type when using info, you will compile with an error (string is not a number subclass):
Set lower limit
classInfo<t>{ PrivateT var;//Defining generic Variables Public voidSetVar (T var) { This. var =var; } PublicT GetVar () {return This. var; } PublicString toString () {//Direct Printing return This. var.tostring (); }}; Public classgenericsdemo21{ Public Static voidMain (String args[]) {Info<String> I1 =NewInfo<string> ();//a generic object that declares a stringinfo<Object> i2 =NewInfo<object> ();//a generic object that declares an objectI1.setvar ("Hello") ; I2.setvar (NewObject ()); Fun (I1); Fun (I2); } Public Static voidFun(info< Super string> temp) {//can only receive generics of type string or object, the parent class of the string class is only the object classSystem.out.print (temp + ",") ; }};
Both the object class and the String class are the parent class of string, all run successfully, but if you use an integer at this point, an error occurs because the integer is not a string parent.
1.3 Explanation: Limitations on generics and subclass inheritance.
A subclass of a class can be instantiated for its parent class through object polymorphism, but in a generic operation, the generic type of a subclass cannot be received using the generic type of the parent class. For example:info<string> cannot use info<object>
Receive.
For example, the following is definitely an error.
classInfo<t>{ PrivateT var;//Defining generic Variables Public voidSetVar (T var) { This. var =var; } PublicT GetVar () {return This. var; } PublicString toString () {//Direct Printing return This. var.tostring (); }}; Public classgenericsdemo23{ Public Static voidMain (String args[]) {Info<String> I1 =NewInfo<string> ();//generic type is Stringinfo<Object> i2 =NULL ; i2 = I1; There is an error because the object generic type is different. }};
Object must be larger than string.
Summarize
1) use? can receive arbitrary generic objects.
2) The upper limit of the generic type:? The extends type.
3) The lower limit of the generic type:? Super type. A relative understanding of some can be.
4) understand why inheritance between generic subclasses cannot be converted directly.
Generics-wildcard, restricted generics (upper and lower limits)