Process-oriented and object-oriented design ideas

Process-oriented and object-oriented design ideas

Process-oriented: procedure oriented programming abbreviation POP

The specific steps to solve the problem (direct attention to the process, simple and effective.)

Object-oriented: object oriented programming abbreviation OOP object-oriented programming language

Solve complex problems and classify things

The object-oriented thinking of macro-specific things conforms to human cognitive habits.

It is suitable for dealing with complex things. First, use an object-oriented way to classify the overall relationship, and then deal with in-depth details according to different classes.

Relationship between the two:

Object-oriented cannot replace process-oriented, they complement each other. Object-oriented focuses on grasping the relationship between things at a macro level, and still adopts a process-oriented way of thinking when it comes to how to achieve a certain detail.

java class

A class is a template that describes the properties and behavior of a class of objects.

Class is an abstraction of some basic characteristics of a certain type of group in the objective world.

Class structure

Member variable: the description of object properties;

Method: the behavior of the object; (things that can be done)

Construction method: used to create objects;

Inner class: the class declared in the class body.

Block: code block

Basic properties and methods

​ Member variables: a description of the characteristics of a certain thing (what does this class have)

​ Method: Regulations on certain behaviors (what can this class do)

Class definition

public class Car {
 /*
   声明类的成员变量
   格语法为
     [访问权限修饰符] [修饰符] 数据类型 变量名[= 值];
  */
    String name;   //名字
     String color;  //颜色
     float price;    //价格
    /*
      成员方法声明格式为
         [访问权限修饰符] [修饰符]/void  方法名([形参列表]){
                 方法体
                [return 返回值]
         }
     */
    public void start(){
        System.out.println("汽车启动");
    }
    public void  run(){
        System.out.println("汽车行驶");
    }
    public void stop(){
        System.out.println("刹车");
    }
}

Creation and use of java objects

Create an object using the new keyword

Use object. Member variables to refer to the member variables of the object.

Use the object. method to call the method of the object.

package day1;

public class TestCar {
    
    
    public static void main(String[] args) {
    
    
        /*
           创建汽车对象:
             以Car类为模板,
        Car   benchi =  new+Car();  new 关键字+默认构造方法();
                  在内存空间  创建一个对象
        Car benchi  以Car类作为类型   声明一个变量  指向内存空间中具体对象
                         使用benchi变量访问对象中的属性,方法


                  通过类可以创建无数个对象
         */
                  Car benchi= new Car();
                 /*
                 对象.成员变量=变量值
                 实例对象。属性=属性值
                  */
                  benchi.color="red";
                  benchi.name="SUV";
                  benchi.price= 1200000;
                   /*
                   实例对象调用成员方法
                   实例对象.成员方法([参数列表])
                   如果有返回值  返回值需要创建变量接收返回值
                    */
                    benchi.run();
                    benchi.start();
                    benchi.stop();
                   Car lsls =new Car();
                  lsls.price=12000000;
                  lsls.name="plus";
                  lsls.color="block";
                 lsls.stop();
                 lsls.start();
                 lsls.run();

                 System.out.println(benchi.color);
        System.out.println(benchi.name);
        System.out.println(benchi.price);
        System.out.println(lsls.price);
        System.out.println(lsls.name);
        System.out.println(lsls.color);
    }
}

Member variables

Defined in the class, outside the method

Can be a basic data type reference data type

You can not assign a value to it, but java will default to the assignment reference type null float 0.0 char space

Act on the entire class

A copy of the object will be copied to the class when the object is created

Can be accessed by class methods, constructors, and statement blocks of a specific class.

Local variable

Variables defined in methods, construction methods, or statement blocks are called local variables.

You can use any data type in the Java language (including basic types and reference types).

The assignment must be initialized before use.

Variable declaration and initialization are in the method. After the method ends, the variable will be automatically destroyed.

Guess you like

Origin blog.csdn.net/ZJ_1011487813/article/details/109145640