Wildcard -------- wildcard

Source: Internet
Author: User

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 }};

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.