This is the 17th day of my participation in Gwen Challenge


encapsulation

performance
  1. Method is a basic package.
  2. A class is also a wrapper.
role
  1. Improved code reuse.
  2. Hiding implementation details and providing external access. Easy for the caller to use. This is one of the core, or you can think of it as the concept of encapsulation.
  3. Improved security.
conclusion
  1. Objects are accessed by the outside world through their methods
  2. Summary: Privatize everything in a class that does not need to be provided externally, including properties and methods. When things are described later, attributes are privatized and set and GET methods are provided to access them. Note: Private is just the embodiment of encapsulation.

inheritance

  1. Inheritance is divided into single inheritance and multiple inheritance
  2. Java only supports single inheritance between classes. Multiple inheritance is implemented through interfaces

polymorphism

meaning

External one interface, internal multiple implementations

classification
  1. Runtime polymorphism, compile-time polymorphism
implementation

Compile-time polymorphism is realized through method overloading, and runtime polymorphism is realized through class inheritance, method rewriting, and late linking techniques

The definition of a class

The basic structure

  1. Member variables
  2. Members of the method

Full format

[public] [abstract | final] class ClassName [extends SuperClassName]
										[implements TnterfaceNameList]{... }The // modifiers public, abstract, or final specify class attributes
/ / extends class inheritance
/ / implements implementation class
Copy the code

Note: Variables and methods are called members of the class, but constructor methods are not called members of the class

Member variables

The basic format
[public] [protected] [private] [static] [final] [transient][volative] type varibleName;

//[public][protected][private] or no modifier (default default) access permission
//static to restrict member variables to class variables, or instance variables if not
//final to declare a constant. A constant modified by final cannot be modified
//transient to declare a temporary variable, which is permanent by default
Copy the code

Members of the method

The basic format
[public] [protected] [private] [static] [final] [abstract] [native] [synchronized] <return_type> <name> (<argument_list>)[throws<exception_list>]{
    ...
};

//[public][protected][private] or no modifier (default or package) access
//static to restrict member methods to class methods
//abstract indicates an abstract method with no implementation body
// Final methods cannot be overridden
//native indicates that methods are implemented in other languages
Synchronized controls access to shared data by multiple concurrent threads.
Copy the code
Variable argument list Varargs
// Format: type... Parameter names
// Type after... Indicates that the parameter is passed either an array or a sequence of parameters when the method is called


public float avg(int. nums){
    int sum=0;
    for(int x:nums){
        sum+=x;
    }
    return (float)sum/nums.length;
}
public static void main(String args[]){
    avg(10.20.30);
} 
// You can change it by yourself
Copy the code

Method overloading

Follow the principle of
  1. The parameter list of a method must be different, including the type and number of parameters, so as to distinguish different method bodies.
  2. Methods can have the same or different return types and modifiers

this

This is a meaningful reference used by Java to the current object itself

A constructor

All classes in Java have constructors that are used to initialize class objects

Define the format
[public|protected|private] <class_name>([<argument_list>]){
    [<statements>]
    
    /** 1. Public Any class can create instance objects of this class. 2. The default (package | default) only with the class to instantiate 4. Private cannot be other class instantiation, this class might have a method with public access method (factory), only they can construct object and returns the * /

}

Copy the code
Pay attention to
  1. Constructor names must be the same as class names

  2. The constructor cannot have a return value

  3. The constructor cannot be invoked directly by the user, only automatically when the object is instantiated with new.

  4. Java provides a default constructor when the user does not write one

    1. Numeric – 0
    2. Boolean, false
    3. Char – ‘\ 0’
    4. Object – null
  5. This ([< Argument_list >]) is the constructor of the class on which the call is made

Access control

  1. Public – and open
  2. Protected — Protected
  3. Private – private
  4. Default — Default, also called default or package;
Modifier \ permission The same class The same package A subclass global
private Square root
default Square root Square root
protected Square root Square root Square root
public Square root Square root Square root Square root
conclusion
  1. The above four access levels can be defined for class member variables and member methods, and for classes (except inner classes) there can be public and default
  2. Objects of the same class can access each other’s private member variables and member methods. This indicates that access control is at the class level, not the object level.

The inner class

The inner class is used as a member of the outsourcing class

An inner class is used as a member of an outsource class. It can access all members of the outsource class, including static members and private members

Represents variables of the same name in the outsourced and inner classes and inner class methods
public class Outer{
    private int size;
    public class Inner{
        private int size;
        public void doStuff(int size){
            size++;// Local variables
            this.size++;// Inner class member variables
            Outer.this.size++;// Outsource class member variables}}}/** When an inner class method accesses a variable of the same name of the outsourcing class, the format is: the outsourcing class name.this. The variable name is */
Copy the code
The inner class is defined in the block of the outsourced class

When an inner class is defined ina block ina method body, its life cycle is limited to a block change run, where local variables need to be final (that is, constant local variables) to survive the end of the method.

Access the inner class in a class other than the outsourced class
  1. Inner classes have four different access rights than ordinary classes and interfaces: public, protected, default, and private.
  2. Internal classes that can be accessed outside of the outsourcing class are represented as full: the outsourcing class name. The internal name of the class
features
  1. The type of an inner class is only used within the class or block in which it is defined. When referenced externally, it must be given a full name with the name of the outer class, and the inner class name must not be the same as the name of the outer class.
  2. * Inner classes can access **Outsourcing class **Static or instance member variable of *
  3. * Inner classes can be defined in member methods whose local variables or arguments must be final before they can be used by inner classes; *
  4. An inner class can be an abstract class or an interface, and if it is an interface, it can be implemented by another inner class.
  5. The inner class can be used to public, protected, the default, four private access control;
  6. The inner class can be declared static(ordinary classes are not wide), so that the inner class becomes the top-level class, which is equivalent to putting it outside the nested inner class, and its objects will not contain Pointers to the outsource object, so it can no longer refer to the outsource object.
  7. Only top-level classes can declare static members. If an inner class requires a static member, the inner class must be declared static. Otherwise, members of an inner class cannot be declared static.

An anonymous class

Local classes (local classes) and anonymous classes are two types of inner classes

Local classes: Inner classes are declared in a method body, which is a local class (inner class);

Anonymous class: A local class without a name.

Local classes use regular class declarations, while anonymous classes are defined in an expression and are part of an expression. Anonymous classes inherit from a parent class or implement an interface. Anonymous classes defined grammar including new operator, inherited the name of the parent class or implement interface, brackets enclose a constructor parameter (if it is implementing an interface, because there is no constructor interface, the parentheses content is empty), the anonymous class declaration block, the block, allowing method statement, is not allowed.

The benefit of anonymous classes is that you can make your code more concise by defining a local class as anonymous when it is only used once.

Lambda expressions

grammar
Lambda expressions The list of parameters The arrow symbol Expression body
The instance (int, int y) -> x+y
    (int x,int y)->x+y // Return the value of x+y with the integer x and y as arguments() - >42// Returns 42 with no arguments(String s) ->{System.out.println(s); }/* Returns no information */
    
Copy the code

Lambda replaces anonymous classes

If anonymous classes are simple and contain only one method interface,