Object-oriented programming (3)

Disclaimer: All my articles are the compilation of online teaching videos, including Mad God Talk, Shang Silicon Valley, Dark Horse Programmer, etc., used as reference materials, without any commercial use, please refer to the majority of netizens, please do not spray ,Thank you. (Note that due to the website, some code characters may have problems. It is recommended that when reading the code, it is best to correspond with the picture below)

One of the members of the class: attributes

1. Syntax format:

修饰符  数据类型  属性名 = 初始化值;

Note 1: Modifiers, commonly used permission modifiers are private, default, protected, public, other modifiers are static, final (not considered for the time being)
Note 2: Data type, any basic data type (such as int, boolean) or Any reference data type.
Note 3: The attribute name is an identifier, and only needs to comply with the naming rules and specifications.
Object-oriented programming (3)

2. Classification of variables: member variables and local variables

Outside the method, the variables declared inside the class body are called member variables; the variables declared inside the method body are called local variables.
Object-oriented programming (3)
Note: The similarities and differences between the two in terms of initialization values. The same point is that both have a life cycle. The difference is that local variables need to be explicitly initialized except for formal parameters.

The difference between member variables (attributes) and local variables

Object-oriented programming (3)

Memory location of member variables vs local variables

Object-oriented programming (3)
Object-oriented programming (3)

Default initialization assignment of object properties

When an object is created, it will automatically initialize and assign various types of member variables. In addition to the basic data types, the variable types are all reference types, such as the Person above and the array mentioned above.
Object-oriented programming (3)

Class member two: method

1. What is a method (method, function):

 A method is an abstraction of the behavior characteristics of a class or object, used to complete a certain functional operation, and is also called a function or a procedure in some languages.
 The purpose of encapsulating functions as methods is to realize code reuse and simplify code.
 Methods in Java cannot exist independently, all methods must be defined in the class.
example:
Object-oriented programming (3)

2. Method declaration format:

Modifier return value type method name (parameter type parameter 1, parameter type parameter 2,...) {
Method body program code
return return value;
}
Description:
Modifier: public, default, private, protected Equal
return value type: no return value is void; there is a return value, declare the type of the return value, and use it with the "return return value" in the method body.
Method name: belongs to the identifier, follow the identifier naming rules and specifications when naming, "see the name to know the meaning".
Formal parameter list: It can contain zero, one or more parameters. For multiple parameters, separate them with ",".
Return value: The data that the method returns to the program that called it after execution.

3. Method call

The method is called by the method name, and only when it is called, the calling process is analyzed as shown in the figure:
Object-oriented programming (3)
Note:
 The method is called once, it will be executed once
 If there is no specific return value, the return value type is represented by the keyword void , Then the return statement is not necessary in the method body. If it is used, it is only used to end the method. When the method is
called, the result of the method should be returned to the caller, and the caller
can only call the method or attribute in the method. Define the method inside the method

4. Practice

1) Create a Person class, which is defined as follows:
Object-oriented programming (3)
Requirements: (1) Create an object of the Person class, set the name, age and sex properties of the object, call the study method, output the string "studying", call the showAge() method to display For the age value, call the addAge() method to increase the age attribute value of the object by 2 years. (2) Create a second object, perform the above operations, and experience the relationship between different objects of the same class.
2) Using object-oriented programming methods, design class Circle to calculate the area of ​​a circle.
3) Write a program, declare a method method, print a 10 8 rectangle in the method, and call the method in the main method.
4) Declare a date type MyDate: There are attributes: year year, month month, day day. Create 2 date objects, assign values ​​respectively: your date of birth, the date of birth of your object, and display information.

Guess you like

Origin blog.51cto.com/12859164/2545541