Wildcard characters in Java

Source: Internet
Author: User

A wildcard is represented by <. Extends T>,<? Super T>,<?>.

<? Extends t> means that you can refer to the subclasses of T and T

<? Super t> means that you can refer to the parent class of T and T

<?> represents an unqualified reference.

<? Extends t> looks similar to declaring a type-qualified <t extends a> in a generic class or function, but there is a big difference between the two

First intuitive look at the "T", one in front of the extends, one in the extends behind.

Type qualification is when declaring a class or function, such as:

Public <t extends a>t func (T) {};

Wildcard characters are used when referencing as variables such as:

<? Extends t> aobj = new OBJ ();

The following combination of code description:

Import java.lang.reflect.*;
Class testclass1<t>{
TestClass1 (T t) {
this.at=t;
}
Public T Dispandret () {
Class<?> Ademo=null;
Ademo=at.getclass ();
Method Amethod=null;
Try
{
Amethod=ademo.getmethod ("Dispandret");
}
catch (Exception e)
{
System.out.println (At.getclass () + ":" +at.tostring ());
return at;
}
Try
{
Amethod.invoke (at);
}
catch (Exception e)
{
SYSTEM.OUT.PRINTLN ("Invoke Error");
}
return at;
}
Private T at;
}

Class Testclass2<t> extends testclass1<t>{
TestClass2 (T t) {
Super (T);
}
}

Class Testclass3<t> extends testclass1<t>{
TESTCLASS3 (T t) {
Super (T);
}
}

public class test2{
public static void Main (string[] args) {
testclass1<string> atest=new testclass1<string> ("This is Test");
Atest.dispandret ();

Testclass1<testclass2> btest=new testclass1<testclass2> (New testclass2<string> ("BTest ref TestClass2 "));
Btest.dispandret ();

Btest=new testclass1<testclass3> (New testclass3<string> ("Btest ref TESTCLASS3"));
Btest.dispandret ();

testclass1<? Extends Testclass1> ctest=new testclass1<testclass3> (New testclass3<string> ("CTest ref TestClass2");
Ctest.dispandret ();

Ctest=new testclass1<testclass2> (New testclass2<string> ("CTest ref TESTCLASS3"));
Ctest.dispandret ();

Ctest=new testclass1<testclass1> (New testclass1<string> ("CTest ref TestClass1"));
Ctest.dispandret ();
}
}

//////////////////

Output

Class Java.lang.String:this is test
Class Java.lang.String:bTest ref TESTCLASS2
Class Java.lang.String:cTest ref TESTCLASS2
Class Java.lang.String:cTest ref TESTCLASS3
Class Java.lang.String:cTest ref TESTCLASS1

///////////////////////////////////////////////

First, a generic class TestClass1 is declared, and then two subclasses are inherited TESTCLASS2,TESTCLASS3

The main function uses testclass1<string> atest to define a variable to refer to the String type.

With testclass1<testclass2> btest reference a TESTCLASS2 type, red code part of the code want to Btest reference TESTCLASS3, resulting compilation error

Tips are as follows:

"Required:testclass1<testclass2>

Found:testclass1<testclass3>"

This is because btest is defined with testclass1<testclass2> and can only refer to TestClass2. Reference TESTCLASS3 will compile an error.

Next testclass1<? Extends testclass1> CTest uses wildcards to define CTest, which means ctest can refer to TestClass1 and its subclasses.

TestClass2 and TESTCLASS3 are their subclasses, the next sentence ctest=new testclass1<testclass2> (new testclass2<string> ("CTest ref TESTCLASS3 "));

When you refer to TESTCLASS3 ctest, there is no compile error.

Wildcard characters in Java

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.