Java's support for generics (2)-advanced syntax

Source: Internet
Author: User

There are roughly three basic generic syntax elements in Java:Restrict generic availability,Make the type wildcard, AndGeneric inheritance. The three syntaxes are described below.

1. Restrict generic availability

When defining a generic class, we can use any type to instantiate the type owner in the generic class by default. We can also specify a type so that this generic class can only instantiate the type owner through this type or its subclass, or the class that implements this interface.

When defining type holders, we use the extends keyword to restrict them. For example, we can define generic classes as follows:

public class LimitDemo<T extends List>{}

This indicates the type represented by type holder T.Must be a type that implements the list Interface. Therefore, we need to generate an instance of the limitdemo class as follows:

LimitDemo<ArrayList> demo = new LimitDemo<ArrayList>();

Because arraylist is a class that implements the list interface, this write is legal. If it is not a class that implements the list interface:

Limitdemo <string> demo = new limitdemo <string> (); // string does not implement the list Interface

The compiler will report the error "the string type is not in the T range:

In fact, when we don't use the extends keyword to restrict generic class objects, the compilerBy default, all subclasses of the object class can be instantiated.. That is:

public class LimitDemo<T>{}

It is equivalent:

public class LimitDemo<T extends Object>{}

2. Type wildcard

If there is a demo with a reference name, we hope that the demo can reference both the wilddemo <string> type and the wilddemo <integer> type. How can this be implemented?

If no wildcard is used, we can only define two variables: wilddemo <string> demo1 and wilddemo <integer> demo2 to save references to these two types of instances. If a wildcard is used, you can write as follows:

// Use the wildcard '? 'Wilddemo <?> Demo; demo = new wilddemo <integer> (); demo = new wilddemo <string> ();

However, note that,An object referenced by a name declared by a type wildcard cannot be added to this object. Information of this object can only be obtained or removed.. For example:

Import Java. util. *; public class wilddemo <t> {private t x; // assign public void setx (t x) {This. X = x;} // read the value of X public t getx () {return X;} public static void main (string [] ARGs) {wilddemo <integer> OBJ = new wilddemo <integer> (); obj. setx (100); // use the wildcard '? 'Wilddemo <?> Demo = OBJ; demo. getx (); // read information, OK demo. setx (null); // remove information, OK demo. setx (200); // set new information to X, error }}

3. Inheritance of generic classes

Define the parent generic class:

class Parent<T1,T2>{}

Define sub-generic classes inherited from parent:

class Child<T1,T2,T3> extends Parent<T1,T2>{}

If the subclass wants to retain the type holders t1 and t2 of the parent class, the number of Type holders declared on the parent class must be fully written when inherited, that is, class child <t1, t2)>. If the subclass does not retain the type owner, the inherited t1 and t2 types are automatically changed to the object type.

In practical applications, we should define generic classes based on the simple and easy-to-read principle. We should try to avoid defining complex generics with multiple definitions.

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.