- Defining generic Classes
- The elements in a generic class should be generic types
- You can instantiate a generic object with a specific substitution type variable
- Generic methods
- Public static<t> t Getmiddle (t ... a), so form
- Call generic method: String middle = arrayalg.<string>getmiddle ()
- Most of the time, you can ignore <String>, ie arrayalg.getmiddle ()
- Qualification of type variables
- If you need to call the data type of a particular interface, you need to use the following form
- Public static<t extends comparable> T min (t[] a)
- It is important to note that T is a sub-type of something behind it, which can be an interface or a class
- Qualifiers are segmented using &, for example T extends comparable&serializable
- You can inherit multiple interfaces and a superclass, and if you have a superclass, put the superclass in the first place.
- Generic code and virtual machines
- Type parameter Erase
- If the variable is not qualified, then the original type is an object
- Place an interface with no methods at the end of the boundary list
- Translating generic expressions
- If the return type is erased, the compiler automatically inserts a strong
- Pair<employee> buddies= ...
- Employee Buddy = Buddies.getfirst
- The above call to Pair.getfirst is called in the virtual machine, casting the returned object class to the employee type
- Translating generic methods
- Bridge Method--is the compiler's tool for maintaining polymorphism
- There are no generics in the virtual machine, there are only ordinary classes and methods
- To preserve type safety, insert a forced type conversion if necessary
- Calling Legacy code
- Constraints and limitations
- Cannot instantiate with a basic data type
- Pair (Double) √pair (double) x
- Run-time type checking applies only to raw data types
- A instanceof pair<t> x
- A instanceof pair<string> x
- Pair<string> a = (pair<string) b x
- The GetClass method returns always the original data type
- Cannot create an array of parameterized types
- pair<string> table = new PAIR<STRING>[10] X
- There is only one way to arraylist<pair<string>>
- Cannot instantiate type variable
- To have a constructor expression provided by the caller
- Pair<string> p = Pair.makepair (string::new)
- Makepair (supplier<t> constr)
- return new Pair<> (Constr.get (), Constr.get ())
- Cannot create a generic array
- Cannot be done, however.
- Public static<t extends comparable> t[] Minmax (intfuction<t[] constr, T ... a)
- When called, it is string[] ss = Arrayalg.minmax (String[]::new, "Tom", "Jim", "Harry").
- Invalid type variable in the static context of a generic class
- That is, if something in a generic class is modified by static, it cannot have t
- Cannot throw or capture generic class instances
- Generic class extension Throwable is not legal
- Unable to capture generic Data sample T T is not captured
- Public static<t extends throwable> void DoWork (class<t> T)
- catch (T E) x
- The use of type variables is legal
- Public static<t extends throwable> void doWork (T t) throws T
- catch (Throwabe realcause) √
- The conflict after wiping out
- To support the erase conversion, you need to force the restriction that a class or type variable cannot be a subclass of two interface types at the same time, and these two interfaces are different parameterization of the same interface!
- Class Employee implements Comparable<employee>
- Class Manager extends Employee implements Comparable<manager>
- At this point, the Manager will implement comparable<employee> and comparable<manager>
- Inheritance rules for generic types
- Pair<employee> and pair<manager> don't matter.
- Wild-Letter wildcard type
- Concept: <? Extends employee> means any pair type
- Pair<manager> is pair< Extends subtypes of employee>
- Super-type qualification of wildcard characters
- Unqualified wildcard characters
- Pair<?> the Set method cannot be called
Simple Java Generics summary