Java learning notes - object oriented

1, Understanding object-oriented

1.1.1 difference between process oriented and object-oriented

1. Process oriented: process oriented programming focuses not on "things", but on doing it in several steps, first what to do and then what to do.
For example:
① Get up in the morning: get up, dress, wash, eat and go to work.
According to this step, we can realize the function of "one day". In the whole process, we focus on how to do it step by step, not on "people".

② Open the door, adjust the seat, fasten the safety belt, step on the clutch, start, shift and oil supply.
As long as this step is followed, the car will go. Obviously, the focus is still on the steps. As long as each step is realized, the whole process does not pay attention to the "car".

2. Object oriented: focus only on the final result.
For example:
Buy a cigarette – > buy a lighter – > find a place where you can smoke – > light a cigarette – > start smoking.
Through this, follow this process step by step and adopt an object-oriented approach with different concerns.

Type 1.1.2

Class: a class of things with the same attributes, characteristics and behaviors, which is called class. Class is abstract and invisible.

Property is used to describe the characteristics of a class.
Method is used to describe the behavior of a class.

Students are a class and create specific objects for everyone.

Attribute: describes that the thing has information.
Behavior: what can this thing do

Things - > classes
Attribute - > member variable
Behavior - > member method

2, Object creation

2.2.1 creation of objects

After the class is defined, you can use the class as a "template" to create "objects". A class can create multiple objects.

Syntax format:
new class name ()

public class test07 {
    public static void main(String[] args) {
        //Create a student object
        new Student();
        //Create another student object
        new Student();
    }
}

In order to make it easier to use objects, it is recommended to use variables to receive the following code:

public class test07 {
    public static void main(String[] args) {
        //Create a student object
        Student stu1 = new Student();
        //Create another student object
        Student stu2 = new Student();
        
        int i=10;
    }
}

Code interpretation:

  • For int i = 10; In general, int is a basic data type, I is the variable name, and 10 is the literal of int type.

  • Student stu1 = new Student(), where student is a reference data type, stu1 is a variable name, and new Student() is an object of student type after execution.

2.2.2 construction method

Construction method is a special method in a class. It calls construction method to complete the creation of object and the initialization of object properties.

How to define the construction method depends on the following syntax format:

[Modifier list] Construction method name(Formal parameter list){ 
Construction method; 
} 

Construction method

public class Student {
    String id;
    String name;
    int age;
    public Student(){
        System.out.println("Call the parameterless constructor");
    }

    public Student(String name){
        System.out.println("Called with parameters name Construction method of");
        this.name = name;
    }
    public Student(String id,String name,int age){
        System.out.println("Constructor with all parameters assigned to member variables was called");
        this.id = id;
        this.name = name;
        this.age = age;
    }
}

Test class

 public static void main(String[] args) {
        /*
            Construction method:
                Used to create objects, used with the new keyword
            Syntax:
                new Construction method

            Create multiple construction methods to form overloads
         */

        Student s1 = new Student();
        Student s2 = new Student("zs");
        //System.out.println("Name:" + s2.name);
        Student s3 = new Student("A001","zs",23);
        //System.out.println("student number:" + s3.id + "Name:" + s3.name + "age" + s3.age);

    }

Operation results

  • a key:
    (1) Constructor name and class name must be consistent.
    (2) Construction methods are used to create objects and complete property initialization operations.
    (3) The return value type of the constructor does not need to be written, and an error is reported when it is written, including void.
    (4) The return value type of the constructor is actually the type of the current class.
    (5) Multiple constructors can be defined in a class, which constitute method overloading.

How to call a construction method? The syntax format is: new construction method name (actual parameter list)

Type of reference

In the process of learning the above contents, it is found that the instance variable is referenced. For example, in the Student class, there is a property "String name;", This attribute / instance variable name describes the name of the Student. The data type of the name variable is string type. String type does not belong to the category of basic data type, that is, string type belongs to reference data type.

Keywords: Java

Added by rskandarpa on Wed, 09 Mar 2022 14:32:56 +0200