Continued from the above java 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; } }
ˆ 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所拥有的特性了}
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>
Continued from the above java 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; } }
ˆ 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所拥有的特性了}
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>
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)!