object oriented programming

Object-Oriented Programming

1. The three main lines of Java object-oriented learning

  1. Java类及类的成员: properties, methods, constructors; code blocks, inner classes
  2. 面向对象的三大特征: encapsulation, inheritance, polymorphism, (abstract)
  3. Other keywords: this, super, static, final, abstract, interference, package, import

"Think big, start small"

1.1, process-oriented and object-oriented

面向过程(POP, Procedure Oriented Programming): The emphasis is on functional behavior, taking functions as the smallest unit, and considering how to do it.

面向对象(OOP, Object Oriented Programming): Encapsulating functions into objects, emphasizing objects with functions, taking class/object as the smallest unit, and considering who will do it.

Example:

Problem description: "People put elephants in refrigerators"

1.面向过程:强调的是功能行为,以函数为最小单位,考虑怎么做。
① 把冰箱门打开
② 抬起大象,塞进冰箱
③ 把冰箱门关闭
2.面向对象:强调具备了功能的对象,以类/对象为最小单位,考虑谁来做。
人{
    打开(冰箱){
    冰箱.开开();
    }

    抬起(大象){
    大象.进入(冰箱);
    }

    关闭(冰箱){
    冰箱.闭合();
    }
}

冰箱{
    开开(){}
    闭合(){}
}

大象{
    进入(冰箱){}
}

1.2. Two elements of object-oriented

: The description of a class of things is an abstract, conceptual definition

对象: It is every individual of this kind of thing that actually exists, so it is also called实例(instance)

  • The focus of object-oriented programming is the design of classes
  • The design class is the member of the design class. The common members of the class are:
    • 属性: field (can be translated as "domain" or "field"), corresponding to the class成员变量
    • 行为: method, corresponding to the member method in the class

How to use the class after it is created? ----Instantiation of a class, that is, creating an object of the class

image.png

1.3, the use of classes and objects

(The implementation of object-oriented thinking):

step:

1. Create a class and design the members of the class

2. Create an object of the class

3. Call the structure of the object through "object.property" or "object.method"

public class PersonTest {
    
    
    public static void main(String[] args) {
    
    
        //创建Person类的对象
        Person p1 = new Person();
//        Scanner scanner = new Scanner(System.in);

        //调用对象的结构:属性、方法
        //调用属性:对象.属性
        p1.age = 1;
        p1.isMale = true;
        p1.name = "Cute";
        System.out.println(p1.age);

        //调用方法:对象.方法
        p1.eat();
        p1.sleep();
        p1.talk(Chinese);

    }
    public class Person{
    
    
        //属性
        String name;
        int age;
        boolean isMale;

        //方法
        public void eat(){
    
    
            System.out.println("人可以吃饭");
        }
        public void sleep(){
    
    
            System.out.println("人可以睡觉");
        }
        public void talk(String language){
    
    
            System.out.println("人可以说话,使用的是:" + language);
        }
    }
}

If multiple objects of a class are created, each object independently has a set of class attributes (non-static)

1.4, object memory analysis

  1. Memory structure: stack (local variable), heap (new structure: object (non-static member variable), array)
  2. Variables: member variables vs local variables (in method, method parameter, constructor, constructor parameter, code block)
  • The variables in the method are all local variables
  • Variables of reference type can only store two types of values: null or address value (including the type of variable)

important! ! !

虚拟机栈: It is the usual stack structure. We store local variables in the stack structure

: We load the new structure (for example: array, object) into the heap space.

​ Supplement: Object properties (non-static) are loaded into the heap space

方法区: Class loading information, constant pool, static domain

Local method stack: generally store C and C++ things

image.png

1.5, memory analysis of object array

Variables of reference type can only store two types of values: null or address value (including the type of variable)

image.png

1.5, the use of attributes in the class

Use of attributes in classes

​ Attributes (member variables) vs local variables

1. The same points:

​ 1.1 Format of defining variables: data type variable name = variable value

1.2 Declare first, then use

​ 1.3 Variables have their corresponding scope

2. Differences

​ 2.1 The positions declared in the class are different

属性​: directly defined within a pair of {} of the class

局部变量​: Variables declared in methods, method parameters, code blocks, constructor parameters, and constructors

​ 2.2 Differences about permission modifiers

​ Attribute: You can specify its permissions when declaring attributes, using permission modifiers

​ Commonly used permission modifiers: private, public, default, protected---->encapsulation

​ Local variables: permission modifiers are not allowed

​ 2.3 The case of default initialization value

​Properties : Attributes of a class, according to its type. have default initialization values

​ Integer (byte, short, int, long): 0

​ Floating point type (float, double): 0.0

​ Character (char): 0 (or '\u0000')

​ Boolean: false

​ Reference data type (class, array, interface): null

​local variable: no default initialization value

​It means that we must explicitly assign a value before calling a local variable

​ In particular, when the formal parameters are called, we can assign them.

​ 2.4 The location loaded in memory

​Attribute : loaded into the heap space (non-static)

​Local variables: loaded into stack space

See the table below for details:

difference attribute (member variable) local variable
Same point 1.1 Format of defining variables: data type variable name = variable value 1.2 Declare first, use later
1.3 Variables have their corresponding scope
2.1 The position of the declaration in the class is different Defined directly within a pair of {} in a class Variables declared in methods, method parameters, code blocks, constructor parameters, and constructors
2.2 Differences about permission modifiers You can specify its permissions when declaring attributes, using permission modifiers permission modifiers are not allowed
2.3 The case of default initialization value Attributes of a class, according to its type. have default initialization values There is no default initialization value. It means that we must explicitly assign a value before calling a local variable. In particular, when the formal parameters are called, we can assign values.
2.4 Location loaded in memory Loaded into the heap space (non-static) load into stack space

1.6. Declaration and use of methods in classes

Method: Describes the functionality a class should have

for example:

  • Math class: sqrt()\random()...
  • Scanner class: nextXxx()...
  • Arrays类:sort()\binarySearch()…

1.6.1. Examples

   public void eat()//无返回值,无形参
   public void sleep(int hour)//无返回值,有形参
   public String getName()//有返回值,无形参
   public String getNation(String getNation//有返回值,有形参

1.6.2. Method declaration

权限修饰符 返回值类型 方法名(形参列表){
    
    
           方法体
       }

static final abstract can also modify methods

1.6.2.1, member variables and local variables

  • Outside the method body, the variables declared in the class body are called member variables ;
  • Variables declared inside the method body become local variables .

image.png

1.6.3. Specific instructions

1.6.3.1, permission modifier

The modifiers for the default permissions arepublic

The four modifiers specified by Java: private, public, protected, default

1.6.3.2, return value type

Can be divided into return value vs no return value

  • If there is a return value, the type of the return value must be specified when the method is declared . At the same time, the method needs to use the return keyword to return a variable or constant of the specified type. Format:return 数据;

  • If the method does not return a value , void is used when the method is declared. In a method without a return value, return is not used, but if it is used, it can only use " return;" to indicate the end of this method.

  • Should the definition method have a return value?

    ​ ① Topic Requirements

    ② Based on experience: specific analysis of specific issues

1.6.3.3, method name

The method name belongs to 标识符and should conform to the rules and specifications of the identifier, "see the name and know the meaning"

1.6.3.4, formal parameter list

A method can declare 0, 1, or more formal parameters,

Format: data type 1 formal parameter 1 , data type 2 formal parameter 2,…

1.6.3.5, method body

The embodiment of the function of the method

1.6.3.6, return keyword

  1. Scope of use: used in the method body

  2. effect:

    ① End method

    ② For methods with return value types, use the "return data" method to return the desired data.

    ③ Note: The execution statement cannot be declared after the return keyword

1.6.3.7, how to call

In the use of the method, the method can call the properties or methods of the current class

Special: method A is used again in method A: recursive method

In a method, methods cannot be defined.

Guess you like

Origin blog.csdn.net/Miss_croal/article/details/131611938