- Interface
- Interface concept: An interface is a set of requirements descriptions for a class rather than a class
- Interface Example: comparable interface
- Features of the interface:
- All methods of the interface are automatically public and do not require explicit instructions
- The interface can contain constants, but cannot contain instance fields
- Steps to implement the interface:
- Declaring a class to implement a given interface
- Define all the methods in an interface
- The key word for implementing the interface is implement
- When implementing an interface, you must define the method as public
- Java is a strongly typed language, and when a method is called, the compiler checks to see if the method exists
- Comparisons can only exist between the parent class and the subclass, and not the child class compared to the parent class!
- Features of the interface
- You cannot instantiate an interface with new, but you can declare a variable of an interface
- An interface object must refer to an object that implements the interface's class
- Instance can check if an interface is implemented
- Interface can also be extended, the keyword is extends
- Method can directly use the defined constants in the interface
- The number of interfaces is not unique and can be represented as a right method interfance 1, Interface 2
- Static methods
- A getinstance-like approach can be defined in the interface to improve the simplicity of the API
- For example, static Calculator getinstance () {return new Basiccalculator ();}
- Default method:
- Add default for markup, such as default void MouseListener (MouseEvent event) {}
- Plus default, you can override the method you want to use without having to modify all of the methods
- Resolving default method conflicts
- Super class first
- Interface conflict: A hyper-interface provides a default method, and the other interface provides a method with the same name and with the same parameters (whether or not default), which must be overridden to resolve interface conflicts
- It is not valid to call one of the default methods in two interfaces alone, such as Person.supergetname (), which is more emphasized by Java to resolve ambiguity
- Superclass > interfaces, which inherit more than the latter, while the class precedence rule makes it less likely to affect previously normal code even if the default method is implemented on the interface.
- Comparator interface
- Implementing a custom Comparator requires that the instance be constructed when it is used, as follows: comparator<string> comp = new Lengthcomparator () if (Comp.compare (word[i), WORD[J]) >0) ...
- When used, you need to pass in two parameters for ArraySort, Arrays.sort (friends,new lengthcomparator ())
- This is something about the difference between comparable and comparator []http://www.cnblogs.com/skywang12345/p/3324788.html]
- Object Cloning-->cloneable
- In the default action, clone is a shallow copy, and if the object contains a reference to the child object, the same object reference will still be obtained
- Cloneable interface
- Even if a shallow clone satisfies the requirement, the default interface needs to be implemented, the clone is defined as public, and then the Super.clone is called.
- If you are creating a deep copy, you need to make a call to the Clone method for each child object that is included.
- lambda expression
- definition: is a block of code, meaning--for the convenience of code delivery
- If you cannot complete a table literal in an expression, you need {}, and the lambda expression must have a return value on each branch
- Functional interface--?? (number of letters), lambda expressions can only be passed to the function interface
- method reference:
- :: For three cases
- Object::instancemethod
- Class::staticmethod
- Class::instancemethod
- Super::instancemethod (another)
The
- constructor Reference
- method is named new, and the other and method references are not bad, i.e. (int[]::new), (person::new).
- Variable scope
- There are three parts in a lambda expression: a code block, a parameter, a free variable value
- lambda expression can only reference a variable that the value does not change, whether inside the expression or in the An external
- lambda expression that declares a parameter with the same name as a local variable, or a local variable that is illegal
- handles a lambda expression
- p240-p241, feels very complex Miscellaneous, but take a good look at
- Inner class
- use reason
- can access data defined by this class
- can hide
- anonymous inner classes easily for other classes in the same package (when defining a callback function)
/ul>
- using internal classes to access object state
- internal classes can access themselves and private data for external classes
- special syntax for internal classes
- outerclass.this table The external class object is referenced by the new and this
- in the external class, using This.new xxx () to complete the initialization, and if it is external, use Xx.new yy ()
- local inner class The
- is defined in a specific method, cannot have modifiers, and can be completely hidden from the outside.
- Accessing a variable by an external method
- Local class method can only be a final decorated local variable
- Anonymous inner class
- Form New supertype (construction parameters) {Inner class method and data} Supertype can be either an interface or a class
- You cannot have a constructor
- to use GetClass () on an anonymous word class! =other.getclass () gg
- static inner class
cannot reference the data of an external class, but if you do not need to refer to an external class, define the inner class directly as static.
- static methods must be static internal classes
- static internal classes can have static fields and methods
- Agent
- When to use
- The Invoke method must be called whenever it is used
- Create a proxy object
- class loader, array of class objects, calling processor
- The method used is newproxyinstance ().
- Interfaces and abstract classes
- The latter can be implemented in specific ways
- The latter can implement an interface, and the interface itself cannot implement an interface
- The former can be implemented more, while the latter is only single-inheritance
Summary of Simple Java interfaces