Summary of Simple Java interfaces

Source: Internet
Author: User

  1. Interface
    1. Interface concept: An interface is a set of requirements descriptions for a class rather than a class
    2. Interface Example: comparable interface
    3. 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
    4. 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!
    5. 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
    6. 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 ();}
    7. 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
    8. 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.
    9. 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]
    10. 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.
  2. lambda expression
    1. definition: is a block of code, meaning--for the convenience of code delivery
    2. If you cannot complete a table literal in an expression, you need {}, and the lambda expression must have a return value on each branch
    3. Functional interface--?? (number of letters), lambda expressions can only be passed to the function interface
    4. method reference:
      • :: For three cases
        • Object::instancemethod
        • Class::staticmethod
        • Class::instancemethod
        • Super::instancemethod (another)
      • The
    5. constructor Reference
      • method is named new, and the other and method references are not bad, i.e. (int[]::new), (person::new).
    6. 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
    7. handles a lambda expression
      • p240-p241, feels very complex Miscellaneous, but take a good look at
  3. Inner class
    1. 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
        1. internal classes can access themselves and private data for external classes
        2. 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 ()
        3. local inner class The
          • is defined in a specific method, cannot have modifiers, and can be completely hidden from the outside.
        4. Accessing a variable by an external method
          • Local class method can only be a final decorated local variable
        5. 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
        6. 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
  4. Agent
    1. When to use
      • The Invoke method must be called whenever it is used
    2. Create a proxy object
      • class loader, array of class objects, calling processor
      • The method used is newproxyinstance ().
  5. Interfaces and abstract classes
    1. The latter can be implemented in specific ways
    2. The latter can implement an interface, and the interface itself cannot implement an interface
    3. The former can be implemented more, while the latter is only single-inheritance

Summary of Simple Java interfaces

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.