1. Matching any wildcard in development is the most common way for Object Reference transfer, the generic type must be matched before it can be passed during reference transfer. Otherwise, it cannot be passed.
Class Info <T> {private T var; // defines the generic variable public void setVar (T var) {this. var = var;} public T getVar () {return this. var;} public String toString () {// print return this directly. var. toString () ;}}; public class GenericsDemo12 {public static void main (String args []) {Info <String> I = new Info <String> (); // use String as the generic type I. setVar ("MLDN"); // sets the content fun (I);} public static void fun (Info <Object> temp) {// receive the Info Object System of the Object generic type. out. println ("content:" + temp );}};
Compilation error:
Class Info <T> {private T var; // defines the generic variable public void setVar (T var) {this. var = var;} public T getVar () {return this. var;} public String toString () {// print return this directly. var. toString () ;}}; public class GenericsDemo13 {public static void main (String args []) {Info <String> I = new Info <String> (); // use String as the generic type I. setVar ("MLDN"); // sets the fun (I);} public static void fun (Info temp) {// receives the Info Object System of the Object generic type. out. println ("content:" + temp );}};
The above has indeed completed the improved functions, but some of the Code does not seem to be too appropriate. After all, we have already specified generics.
Class Info <T> {private T var; // defines the generic variable public void setVar (T var) {this. var = var;} public T getVar () {return this. var;} public String toString () {// print return this directly. var. toString () ;}}; public class GenericsDemo14 {public static void main (String args []) {Info <String> I = new Info <String> (); // use String as the generic type I. setVar ("MLDN"); // sets the fun (I);} public static void fun (Info <?> Temp) {// you can receive any generic object System. out. println ("content:" + temp );}};
If you use? This means that any content is acceptable, but this content cannot be directly used <?> Modify the modified generic object.
Class Info <T> {private T var; // defines the generic variable public void setVar (T var) {this. var = var;} public T getVar () {return this. var;} public String toString () {// print return this directly. var. toString () ;}}; public class GenericsDemo15 {public static void main (String args []) {Info <?> I = new Info <String> (); // use String as the generic type I. setVar ("MLDN"); // set content }};