Java BASICS (II: generics and enumeration)
1,Generic
Similar to object-oriented polymorphism, application generics can improve program reusability. Different from polymorphism, application generics can reduce data type conversion and improve code running efficiency. Generics are actually implemented by adding type parameters to classes or interfaces. The Definition Format of classes without generics is:
[Type modifier list] class name [extends parent class name] [implements Interface Name List]
{
Class body
}
If you want to change the above class Definition Format to a class definition format with the characteristics of generics, you only need to change the class name following the keyword class:
Class Name <type parameter>
Or
Class Name <type parameter 1, type parameter 2, type parameter 3,..., type parameter n>
The former applies to only one type parameter, and the latter applies to multiple type parameters, separated by commas (,) between adjacent parameters. The method above applies to interfaces, so that the Interface Definition Format without generics can be modified to the interface definition format with the characteristics of generics.
The Definition Format of type parameters can be any of the following three forms:
(1) type parameter identifier
(2) Type parameter identifier extends parent type
(3) type parameter identifier extends parent type 1 & parent type 2 & parent Type 3 &... & parent type n
Here is an example of the third form:
// Define the class J_C1class J_C1 {public void mb_methodA () {System. out. print ("A") ;}/// defines the J_C2interface J_C2 {public void mb_methodB () ;}// defines the J_C3 class, which inherits the J_C1 class, implementation interface J_C2class J_C3 extends J_C1 implements J_C2 {public void mb_methodB () {System. out. print ("B") ;}}// defines the J_T class, which inherits J_C1 and J_C2class J_T <T extends J_C1 & J_C2> {public void mb_method (T t T) {t. mb_methodA (); t. mb_methodB () ;}}// call the J_T class public class J_Genericity {public static void main (String args []) {J_T <J_T3> a = new J_T <J_T3> ();. mb_methodT (new J_C3 ());}}
The final execution result of the above Code is AB.
In the J_T class, if the type of a variable is T, you can call the member method of J_C1 or the member method of J_C2 through this variable. In the above example, because J_C3 implements the interface J_C2 and is a subclass of J_C1, J_C3 has all the capabilities of J_C1 and J_C2. Therefore, J_C3 can be used as the actual type of the type variable T of J_T, for example, J_T <J_C3>.
2,
Enumeration
The main purpose of creating an enumeration type is to define some enumeration constants. The basic Definition Format of enumeration is:
[Enumeration type modifier list] enum Enumeration type identifier
{
Enumeration constant 1, enumeration constant 2,..., enumeration constant n
}
"[]" Indicates that the enumerated type modifier list is optional. The enumerated type modifier List describes the attributes of the enumerated type, it can contain 0, 1, or more Enumeration type modifiers. If there are no enumerated type modifiers, separate them by spaces. The enumerated type modifier can be public, but not protected, private, and abstract. If the enumerated type modifier contains public, the file name prefix of the enumerated definition must be the same as the name specified by the enumerated type identifier, and ". java" is used as the suffix. In addition, this file cannot contain classes, interfaces, or enumerations with other public attributes. Each Java source program file contains multiple classes, interfaces, or enumerations, but only 0 or 1 of them can be set to public. The public modifier of Enumeration type indicates that this enumeration can be accessed by all classes or interfaces of each software package. If the enumerated type modifier does not include public, it indicates that the enumerated type is encapsulated by default and can only be used within the same package. Enumeration type identifier and enumeration constant 1, enumeration constant 2, ......, enumeration constant n defines some enumeration constants.
Enumeration type variables are abbreviated as enumeration variables. They are defined in two formats:
Enumeration type identifier enumeration variable;
Enumeration type identifier enumeration variable 1, enumeration variable 2,..., enumeration Variable n;
For example, the following defines four enumerated constants:
Enum E_SEASON
{
Spring, summer, autumn, winter
} // End of enumeration E_SEASON
How can we define enumeration variables? You can refer to the two definition formats of the enumerated variables as follows:
E_SEASON s;
E_SEASON s1, s2, s3,..., sn;
In addition to the preceding two methods, you can also define enumerated array variables in the same format as other types of array variables. For example:
E_SEASON [] s;
In addition, you must note that instance objects cannot be created using the new operator for enumeration types. You can directly access enumeration constants using Enumeration type identifiers. For example:
E_SEASON s = E_SEASON. Spring;
In addition, the defined enumerated variables can also access enumerated constants, for example:
S. Summer = E_SEASON. Summer;
Finally, you must note that two Member methods are used:
(1) public String name ()
(2) Public String toString ()
The functions of these two methods are the same and both return the strings corresponding to the enumerated constants. For example, if the s variable has been assigned "Spring", the call result is as follows:
S. name () and E_SEASON. Spring. toString () both return the string "Spring"