Home > Java > javaTutorial > body text

Java's generics in depth

黄舟
Release: 2017-02-24 09:52:34
Original
1324 people have browsed it


Continued from the above java generics

1. Restricted generics

In the above we use the class class GenDemo<T>, there is no limit to the scope of the holder T, which is actually equivalent to Object.
But sometimes we need to pass in the parameter type to be the implementation or subclass of a certain interface or class, rather than the unlimited Object
So Use restricted generics. Once again, we let T be the excuse for the implementation of Collection:

import java.util.collection;public class GenDemo<T extends Collection>  {
    private T t;    public GenDemo(T t) {        this.t = t;
    }    public void setT(T t) {        this.t = t;
    }    public T getT(){        return T;
    }
}
Copy after login
Copy after login

ˆ class GenDemo<T extends Collection> Using restricted generics determines that the type of the holder T can only be the implementation class of Collection , if a non-Collection class is passed in, an error will be reported during translation.

Note:<T extends Collection>The keyword extends is used, but after extends, you can also use interfaces or classes, here The extends is not inheritance. It should be understood here that the type of T is an implementation class that implements the xx interface, or a subclass that inherits the xx class.
The example here only demonstrates the type limitation of the generic method. The exact same rules are used for the limitation of type parameters in the generic class, but it is added to the head of the class declaration, such as:

public class Demo<T extends Comparable & Serializable> {
    // T类型就可以用Comparable声明的方法和Seriablizable所拥有的特性了}
Copy after login
Copy after login

2. Multiple interface restrictions

The extends keyword is mainly used. The extends keyword here unifies the original concepts of extends and implements, that is, extends is used to implement interfaces and inherited classes. But,Still follows the application system, java can only inherit one class and can implement multiple interfaces. That is:

<T extends SomeClass & interface1 & interface2 & interface3>
Copy after login
Copy after login

3. Wildcard generics

Continued from the above java generics

1. Restricted generics

  In the above we use the class class GenDemo<T>, there is no limit to the scope of the holder T, and it is actually equivalent to Object.
But sometimes the parameter type we need to pass in is the implementation or subclass of a certain interface or class, and It is not an unrestricted Object
so use restricted generics. Once again, we let T be the excuse for the implementation of Collection:

import java.util.collection;public class GenDemo<T extends Collection>  {
    private T t;    public GenDemo(T t) {        this.t = t;
    }    public void setT(T t) {        this.t = t;
    }    public T getT(){        return T;
    }
}
Copy after login
Copy after login

ˆ class GenDemo<T extends Collection> Using restricted generics determines that the type of the holder T can only be the implementation class of Collection , if a non-Collection class is passed in, an error will be reported during translation.

Note:<T extends Collection>The keyword extends is used, but after extends, you can also use interfaces or classes, here The extends is not inheritance. It should be understood here that the type of T is an implementation class that implements the xx interface, or a subclass that inherits the xx class.
The example here only demonstrates the type limitation of the generic method. The exact same rules are used for the limitation of type parameters in the generic class, but it is added to the head of the class declaration, such as:

public class Demo<T extends Comparable & Serializable> {
    // T类型就可以用Comparable声明的方法和Seriablizable所拥有的特性了}
Copy after login
Copy after login

2. Multiple interface restrictions

The extends keyword is mainly used. The extends keyword here unifies the original concepts of extends and implements, that is, extends is used to implement interfaces and inherited classes. But,Still follows the application system, java can only inherit one class and can implement multiple interfaces. That is:

<T extends SomeClass & interface1 & interface2 & interface3>
Copy after login
Copy after login

3. Wildcard generics

The above is the in-depth content of generics in java. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!