Java Object Orientation Summary

One, java object-oriented


2. Packaging

The operations and access to hidden information are realized through the methods provided by this class.

Information about hidden objects

set aside access interface

Modify the visibility of the property (private) → create a getter/setter method (set to public for reading and writing properties) → you can make some logical settings for data acquisition and assignment in the getter/setter method

(1) Package

(二)static

static+property

    static properties, class properties

static+method

    static method, class method

static+class

    no static class

static + local variables within a method

    static local variable in method does not exist

static + code block

    Normal code block: exists inside a method

    Constructing Code Blocks: Exists in Classes

    Static code block: static + construction code block

(3) Pay attention to the problem:

1, the life cycle of static members

    Generated with the loading of the class, not recycled until the class dies, and has a long life cycle

2. Member calls in static methods

    A static method can only directly call static members in the same class, and cannot directly access non-static members in the same class

    Non-static members can only be accessed by means of object.member methods after the object is instantiated

3. The execution order of various code blocks

    No matter how many objects you instantiate, static code blocks are executed only once, construction code blocks are called each time an object is constructed, and normal code blocks in methods are called sequentially each time the method is called


3. Inheritance (Part 1)

concept:

1. The relationship between a class and a class

2. Create a new class based on the definition of an existing class

3. The definition of a new class can add new definitions or new functions, or use the functions of the parent class, but cannot selectively inherit the parent class

4. Satisfy the relationship of "A is a B"

Features:

1. Conducive to code taking

2. Shorten the development cycle

grammar:

1. Implement using extends

2. Single inheritance, there can only be one parent class

Initialization order after inheritance

Parent class static member → child class static member → parent class object construction → child class object construction

(1) super

You can access the members of the parent class through the super keyword

1. Access the parent class member method (super. member method)

2. Access the parent class attribute (super. attribute)

3. Access the parent class constructor (super())


1. The constructor of the parent class must be called during the construction of the subclass, and the constructor without parameters is called by default.

2. If there is no explicit annotation in the constructor of the subclass, and there is no parameterless constructor in the parent class, a compilation error occurs

3. Use super to call the specified constructor of the parent class, which must be in the first line of the constructor of the subclass


(二)this pk super

1. this: a reference to the current class object

Access member methods of the current class

Access member properties of the current class

access the constructor of the current class

cannot be used in static methods

2, super: a reference to the parent class object

Access member methods of parent class

Access the member properties of the parent class

Access the constructor of the parent class

cannot be used in static methods

When a constructor is called, super and this cannot appear at the same time


(3) Method overriding pk method overloading

1. Method rewrite:

In subclasses that satisfy the inheritance relationship

The method name, parameters (number, order) and return value are the same as the parent class, compatible with the parent class type

The limited scope of the access modifier is greater than or equal to the parent class method

2. Method overloading:

in the same class

same method name

The number, order, and type of parameters are different

Return value type, access modifier arbitrary


(4) Access modifiers



4. Inheritance (below)

(1) Object class

The parent class of all classes when the Object class

Every class in Java can use methods defined in Object

(2) final

1. The modified class indicates that inheritance is not allowed

2. The modified method means that it is not allowed to be overridden, but can be inherited

3. Modified variables indicate that they are not allowed to be modified

4. Modified variables of reference type cannot point to another object after initialization, but the content of the pointed object can be changed

5. Use with static to indicate static information that is not allowed to be modified, such as some configuration information


5. Polymorphism





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326273913&siteId=291194637