Home  >  Article  >  Java  >  Java's generics in depth

Java's generics in depth

黄舟
黄舟Original
2017-02-24 09:52:341321browse


Continued from the above java generics

1. Restricted generics

In the above we use the class class GenDemo8742468051c85b06f0a0af9e3e506b5c, 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;
    }
}

ˆ class GenDemo2a9303dff3eed1b561b75d0dce9e5a56 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:2a9303dff3eed1b561b75d0dce9e5a56The 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所拥有的特性了}

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>

3. Wildcard generics

Continued from the above java generics

1. Restricted generics

  In the above we use the class class GenDemo8742468051c85b06f0a0af9e3e506b5c, 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;
    }
}

ˆ class GenDemo2a9303dff3eed1b561b75d0dce9e5a56 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:2a9303dff3eed1b561b75d0dce9e5a56The 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所拥有的特性了}

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>

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)!


Statement:
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
Previous article:Reflection basics of javaNext article:Reflection basics of java