Java object-oriented basic grammar 08- - Interface

enumerate

Enumeration is a special class whose objects are limited fixed few constants objects.

Before JDK1.5

  • Plus private constructor privatization

  • This class creates a set of constant internal objects, and add public static modifier, external exposure to these constant objects

JDK1.5 after format

[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.

  • After JDK1.5 Switch , providing support for enumeration types, the latter case you can write the name of the enumeration constants.

  • 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.

Common methods:

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

Enum class has only a single constructor

protected Enum(String name, int ordinal)

Wrapper class

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;

Note: Automatic boxing and unboxing can be achieved only between types with their corresponding.

Some API wrapper class

There cache object wrapper class

Byte -128~127
Short -128~127
Integer -128~127
Long -128~127
Float No
Double No
Character 0~127
Boolean true and false

No cache object class or out of class caches, one for each of the new new

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

interface

Use keyword. It will also be compiled into .class files, but it must be clear that it is not a class, but another reference data types.interface

[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

Implement an interface

Use interface, it can not create objects , but can be implemented ( similar to inherited).implements

A class can implement multiple interfaces simultaneously

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

How to call the corresponding method

  • For static methods interface directly using the "Interface name." Called to

    • 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

Multi implementation of the interface

A class can implement multiple interfaces, this interface is called multi realized . And, a class can inherit from a parent class, multiple interfaces simultaneously.

When the interface, a plurality of abstract methods, implement all abstract methods class must be rewritten . If the abstract method has the same name, only it needs to be rewritten once .

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:

A: The default Qindie follow the principle that the retention of the parent class

B: can also be completely rewritten

Multiple inheritance interface

An interface can inherit other or a plurality of interfaces, the interface inheritance use keyword, the parent child inherited interfaces to interfaces.extends

[Modifier] interface extends the interface Interface 1 and Interface 2 {

}

Tips:

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

Implementation class implements the interface, similar to the subclass inherits the parent class, and therefore, the interface between the object type and implementation of the class variable may be referenced polymorphic configuration. Calling a method through an interface type variable, the final execution of the method body is your new implementation class object implementation.

Classic Interface

 

 

Guess you like

Origin www.cnblogs.com/Open-ing/p/11885242.html