Enumeration is a special class
-
-
This class creates a set of constant internal objects, and add public static modifier, external exposure to these constant objects
[Modifiers] enum enum class name {
const object list;
other members of the list;
}
Enum class requirements and characteristics:
-
Const object must be in the list of enumeration class enum class of the first line , because it is constant, it is proposed that capital .
-
If the constant list of objects no other code behind it ";" can be omitted, otherwise it can not be omitted ";."
-
The compiler defaults to the enumeration class is provided by the private constructor with no arguments, if enumeration class need is constructor with no arguments, you do not declare, write a list of objects constants do not add parameters,
-
If the enumeration class needs is to have a reference structure, there is need to manually define private participation structure, there is a method argument constructor call is in constant object name behind the plus (argument list) can be.
-
The default enum class inheritance is java.lang.Enum class, and therefore can not inherit other types.
-
-
For other properties enumerated type is recommended ( not required ) these properties are also declared as final, because the constant objects in a logical sense should not be changed.
1.toString (): returns the default constant name (object name), you can continue to manually override this method!
2.name (): returns the constant name (object name) [rarely used]
3. ORDINAL () : Returns the constant sequence number, starting from 0 by default
4. values (): Returns all of the enumeration class constant object, the return type is an array type of the current enumeration, is a static method
5. the valueOf (String name) : the enumeration constant enumeration object acquired object name
No. | Basic data types | Packaging (the java.lang package) |
---|---|---|
1 | byte | Byte |
2 | short | Short |
3 | int | Integer |
4 | long | Long |
5 | float | Float |
6 | double | Double |
7 | char | Character |
8 | boolean | Boolean |
9 | void | Void |
1, boxing and unboxing
Packing: The basic data types -> Packaging class object
Manual Packing: For example: new Integer (int value) or Integer.valueOf (int value)
Autoboxing: Integer i = int value;
Unpacking: the packaging of objects -> basic data types
Manually unpacking: example: Integer object .intValue ()
Automatic unboxing: int i = Integer object;
-128~127 | |
Short | -128~127 |
Integer | -128~127 |
Long | -128~127 |
Float | No |
Double | No |
Character | 0~127 |
Boolean | true and false |
Wrapper class object is immutable: once the modification is the new object. Such as: parameter passing method, it does not change the value of the original object
[Modifier] interface interface name {
static const //
// abstract method
// default method
// static method
// private method
}
1, public static constants: public static final
2. public abstract methods: public abstract
Non-abstract implementation class must override
3, a public default method: public default, after JDK1.8
"Object implementation classes." Call
Implementation class can choose to override
4, public static method: public static, after JDK1.8
Use only "Interface name." Called
Implementation class can not override
After private (private not be omitted) JDK1.9: 5, proprietary methods
Modifier class implementation class [] [extends superclass] the implements interfaces 1, 2 {Interface
// rewrite abstract methods interface [must]
// override the default interface method [optional] default, do not write the word
} // inheritance front, implemented after
When implementing class implements an interface, the interface must override all abstract methods, otherwise the implementation class must be abstract class.
You can not override static methods
-
-
Can only use the "Interface name." Call can not be achieved through the class object call
-
-
For abstract methods of the interface, the default method, can only be achieved by calling the class object
-
Interface can not directly create objects, can only create the object implementation class
-
The method may have a conflict :
(1) implementation class implements a plurality of interfaces, and a plurality of interfaces appearing in the same manner as when the default method signature:
Implementation class must make a choice:
A: One reservations: interface name .super method name.
B: can also be completely rewritten
(2) an implementation class only inherits the parent class, and implements the interface, when the parent class appears in the default interface method signature the same way:
B: can also be completely rewritten
Multiple inheritance interface
An interface can inherit other or a plurality of interfaces, the interface inheritance use extends
[Modifier] interface extends the interface Interface 1 and Interface 2 {
}
When the sub-interface to override the default method, default keyword can be retained.
When a subclass overrides the default method, default keyword can not be reserved.
Interface and implementation polymorphic class object reference