7, object-oriented

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_45536396/article/details/102736070

Object-Oriented

First, the program

Simulate real-world, problem-solving in real life

Second, the object

1. All things are objects of objective reality, things are objects.

2. Any objects have characteristics and behaviors

a) wherein: instead of the term generally used, what represents the object. Also known as attributes.

b) behavior: instead of the general verb, represents the object can do. Also known methods.

3. Class

a) extraction class

Extracting from the same or similar objects in a group and the behavior characteristics in common, concern retention portion.

Dog class: Properties (age, sex, breed coat ...) method (eat sleep and drink ...)

Defined b) class

Syntax: class {} class name

Properties: defined by the variable defined in the class, the outer methods. Member variables | instance variables

Property name data type;

Method: Define the same way as static function without later explained in detail. Member method | instance method

public return type method name (parameter list) {// declare a method of representing an object can do

// method implementation should be able to fulfill the functions of how to do it

}

4. Objects

Creating a) objects

Based on the class name, by means of the new keyword

Syntax: class name = new class name of the object name ();

Access b) objects

.. A assignment: object name attribute name = value;

b values: Object name attribute name

. C invoke methods: Method name object name (arguments);.

The relationship between classes and objects

Class: An object: a plurality of

Class defines the properties and methods should have, the object class is a template.

The object is an entity having a plurality of characteristics and behavior, an object is an instance of the class.

Third, the difference between the member variables and local variables [important]

Local variables Member variables
Defined position The internal (function) defined in the process variable Defined outside the class, method
Scope Defining the start to the end of the code block defined In this class, at least an effective
Defaults No default value must be assigned to use There are default values ​​for the same array
Naming conflicts In the overlap range, the same name does not allow two local variables Member variables and local variables can be the same name, but a local variable priority

Note: The default value

Integer type: decimal type 0: 0.0 Boolean: false

Character types: null character | '\ u0000' String | Other: null

Heavy-duty four methods

1. Concept: there may be multiple processes to achieve a certain kind of behavior of the object, the specific choice of what kind of implementation, depending on the parameters passed.

2. Requirements: the same method name, a list of different parameters (number, type, sequence), irrespective of the type of the return value.

Note: The same method name, type parameter list, the number of the order are the same, only the names of the different parameters does not constitute overloaded methods.

3. Execution: The method will be realized to match the incoming parameters corresponding thereto.

4. Benefits: The difference between the flexible shield user.

Fifth, the constructor

1. The concept: to call a special method when creating the object.

2. Features

a) The method must be identical names and class names

b) No Return Type

c) can not be manually invoked way to call the constructor will be automatically called by the JVM triggered constructor when creating the object.

3. The process of object creation

a) allocation of space (to save open space in the heap objects created) at the same time given a default value

b) initialization properties (properties to impart an initial value)

c) in code execution constructor

D) [object type stored in the reference itself (object name) of]

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-WXV0ZwHm-1571962767619) (E: \ Corejava \ lecture notes, and code \ day7_ object-oriented \ notes \ 1. png)]

[Image dump the chain fails, the source station may have security chain mechanism, it is recommended to save the picture down uploaded directly (img-DkNMxl2N-1571962767620) (E: \ Corejava \ lecture notes, and code \ day7_ object-oriented \ notes \ objects creation .png)]

4. Note:

a) the constructor still support the heavy load, corresponding method will match the passed parameters.

Provide a default constructor with no arguments while b) if a class does not provide any method of construction; provided when the class constructor parameters when, constructor with no arguments will not be provided.

c) method can be configured as a property (member variables) assignment, but to distinguish the parameters (local variables) and member variables (attributes)

5. Assignment opportunity object attributes

a) allocation of space, did not attribute given a default value (first assignment opportunities attributes)

b) initialization properties, given the initial value (second attribute is the attribute assignment chance)

c) performing a code configuration method, the method is configured to assign attribute (attribute assignment third opportunity)

After you have created object, the object name. Property to property assignment.

Sixth, references

Variable: Basic variable | object variable.

Basic variables: variable substantially corresponding to the basic data types is stored in the variable value assignment: value is passed.

Object variable: object data corresponding to the type of the variable. Call reference

References are stored in the address of the object, the object can be operated by reference.

Reference assignment is passed between address.

Seven, this keyword

.. 1. this attribute this method name (arguments);

this represents the current objects, like 'I', this class for use in the present representative call this class attributes, methods. Under normal circumstances may be omitted.

Note: When the local variables and member variables of the same name, you can use this to differentiate.

2. this () | this (argument) representative of this class of call, no arguments | arg constructor has

note

a) this () appears only in the first row of the construction method of the present class

b) can not appear recursively

It can be omitted.

Note: When the local variables and member variables of the same name, you can use this to differentiate.

2. this () | this (argument) representative of this class of call, no arguments | arg constructor has

note

a) this () appears only in the first row of the construction method of the present class

b) can not appear recursively

Guess you like

Origin blog.csdn.net/weixin_45536396/article/details/102736070