This week, we learned about generic programming and learned about the benefits of generic methods, which are that generic classes improve the type safety of Java programs, eliminate forced type conversions, and increase the reuse rate of code.
A generic parameter type can use the extends statement to define the upper bounds of a generic variable, and the upper bound declared by the extends keyword can be either a class or an interface. Classes handled by T in the public numbergeneric<t extends number> must have an inheritance relationship with number. The reason for choosing extends is closer to the concept of subclasses. Of course, a generic parameter type can also use the Super statement to define the lower bound of a generic variable, such as list< Super Cashcard>cards=new arrayslist<t> (), the Super keyword can pin the type of a generic parameter to a type or its superclass. Wildcard characters, such as public static<t> void sort (t[] a,compartor< super t> C), are commonly used when qualifying types for a method's parameters
Arrays are covariant, but covariance cannot be used in generic types, which destroys the type-safe generics that will be provided. There is also a confusing distinction in this chapter: T denotes an unknown type, and a wildcard? Represents any one type
During the experiment, it was found that the generic method would not be used. You can understand the meaning when reading a textbook, but it will not be used in a specific program.
201671010116.2016-2017-10 "Java Programming" 10th Week study Summary