Java Basics (JavaSE) summary 07 - object oriented

Stage I: JavaSE learning 07

1. Comparison of object-oriented and process oriented ideas:

* * process oriented: * * is a process centered programming idea. Each step of realizing the function is realized by yourself

* * object oriented: * * is an object-centered programming idea, which realizes specific functions by commanding objects

Relationship between class and object:

Class and its understanding:

​ 1. Class is an abstraction of a kind of real-life transaction with common attributes and behaviors

​ 2. A class is the data type of an object. A class is a collection of objects with the same properties and behaviors

Simple understanding: class is a description of real things

Composition of classes: (properties and behaviors)

Attribute: refers to the characteristics of things, such as mobile phone things (brand, price, size)
Behavior: refers to the operation that things can perform, such as mobile phone things (making phone calls and sending text messages)

Relationship between class and object

Class: class is the abstraction of a class of things with common attributes and behaviors in real life

Object: a real entity that can be seen and touched

Simple understanding: a class is a description of things, and an object is an entity of a class (a specific thing)

2. Definition of class [ application ]

Class consists of two parts: attribute and behavior

  • **Attribute: * * reflected in the class through member variables (variables outside the methods in the class)
  • **Behavior: * * reflected in the class through member methods (compared with the previous methods, just remove the static keyword)

Class definition steps:

​ ① Define class

​ ② Write the member variable of the class

​ ③ Write member methods of classes

/**
 *@autor Your big chinchilla
 */
public class Student {
    // Attribute: name, age
    // Member variable: the format of the variable is the same as that defined before, but the position has changed, outside the method in the class
    String name;
    int age;
    // Behavior: Learning
    // Member method: the format of the method is the same as that defined before, except that the static keyword is removed
    public void study(){
        System.out.println("study hard and make progress every day");
    }
}

3. Creation and use of objects

  • Format for creating objects:
    • Class name object name = new class name ();
  • Format of calling member:
    • Object name Member variable
    • Object name Member method ();

Case:

/**
 * @author Your big chinchilla
 */
/*
Requirements: first define a mobile phone class, and then define a mobile phone test class. In the mobile phone test class, the use of member variables and member methods is completed through objects
     Member variables: brand, price
     Member method: call and send text messages
*/
public class Phone {
    // Brand, price
    String brand;
    int price;
    // Call, text
    public void call(String name){
        System.out.println("to"+name+"phone");
    }
    public void sendMessage(){
        System.out.println("Mass texting");
    }
}
/**
 * @author Your big chinchilla
 */
public class TestPhone {
    public static void main(String[] args) {
        // 1. Create object
        Phone p = new Phone();
        // 2. Assign values to member variables
        p.brand = "rice";
        p.price = 2999;
        // 3. Print the assigned member variables
        System.out.println(p.brand + "..." + p.price);
        // 4. Call member method
        p.call("A Qiang");
        p.sendMessage();
    }
}

4. Object memory diagram

1. Single object memory diagram [understanding]

2. Multiple object memory diagram [understanding]

  • Summary:

    Multiple objects have different memory partitions in heap memory. Member variables are stored in their respective memory areas. Member methods are shared by multiple objects

3. Multiple objects point to the same memory map [understand]

  • Summary:
    When the memory address of an object is the same as that of a record
    As long as any object modifies the data in memory, then, no matter which object is used for data acquisition, it is the modified data.

5. Members and local variables

Differences between member variables and local variables:


Default initialization value:

6. Packaging:

1.private keyword

​ Overview: private is a modifier that can be used to modify members (member variables, member methods)

​ Features: members modified by private can only be accessed in this class. If private modified member variables need to be used by other classes, Provide corresponding operation

​ Provide "getXxx()" method to obtain the value of member variable. The method is decorated with public

​ The "setXXX (parameter)" method is provided to set the value of member variables. The method is decorated with public

Example code:

/**
 * @author Your big chinchilla
 *  Student class
 */
class Student {
    //Member variable
    String name;
    private int age;
    //Provide get/set method
    public void setAge(int a) {
        if(a<0 || a>120) {
            System.out.println("You gave the wrong age");
        } else {
            age = a;
        }
    
    public int getAge() {
        return age;
    }
    //Member method
    public void show() {
        System.out.println(name + "," + age);
    }
}
/*
    Student testing
 */
public class StudentDemo {
    public static void main(String[] args) {
        //create object
        Student s = new Student();
        //Assign values to member variables
        s.name = "Your big chinchilla";
        s.setAge(30);
        //Call the show method
        s.show();
    }
}

2. Use of private keyword

  • Requirements:

    • Define standard student classes and require name and age to be decorated with private
    • It also provides set and get methods and show methods that are convenient for displaying data
    • Create an object in the test class and use it. The final console output is Lin Qingxia, 30
  • Example code:

    /*
        Student class
     */
    class Student {
        //Member variable
        private String name;
        private int age;
        //get/set method
        public void setName(String n) {
            name = n;
        }
        public String getName() {
            return name;
        }
        public void setAge(int a) {
            age = a;
        }
        public int getAge() {
            return age;
        }
        public void show() {
            System.out.println(name + "," + age);
        }
    }
    /*
        Student testing
     */
    public class StudentDemo {
        public static void main(String[] args) {
            //create object
            Student s = new Student();
            //Assign values to member variables using the set method
            s.setName("Your big chinchilla");
            s.setAge(18);
            s.show();
            //Use the get method to get the value of the member variable
            System.out.println(s.getName() + "---" + s.getAge());
            System.out.println(s.getName() + "," + s.getAge());
        }
    }
    

3. this keyword

The variable modified by this is used to refer to member variables. Its main function is to distinguish the problem of duplicate names of local variables and member variables

  • If the formal parameter of the method has the same name as the member variable, the variable without this modifier refers to the formal parameter, not the member variable
  • The formal parameter of the method does not have the same name as the member variable. The variable without this modifier refers to the member variable

4. Principle of this memory [understanding]

  • Note: this represents the reference of the current calling method. this represents the object that calls the method

  • Illustration:

5. Packaging ideas

1. Package overview

Is one of the three characteristics of object-oriented (encapsulation, inheritance, polymorphism)
It is the simulation of the objective world by object-oriented programming language. In the objective world, the member variables are hidden inside the object, and the outside world can not be operated directly

2. Packaging principle

Hide implementation details and only expose public access methods
The member variable is privatized and the corresponding getXxx()/setXxx() method is provided

3. Common manifestations of packaging:

​ 1. Private member variable, providing setXxx and getXxx methods

​ 2. Extract the code into the method, which is a kind of encapsulation of the code

​ 3. Extracting attributes into classes is an encapsulation of data

4. Packaging benefits

The method is used to control the operation of member variables, which improves the security of the code
The code is encapsulated by method, which improves the reusability of the code

7. Construction method

Format and execution timing of construction method

  • Format Note:

    • The method name is the same as the class name, and the case should be consistent
    • There is no return value type, not even void
    • There is no specific return value (result data cannot be brought back by retrun)
  • Execution time:

    • Called when creating an object. Each time an object is created, the construction method will be executed
    • Constructor cannot be called manually
  • Example code:

class Student {
    //Construction method
    public Student() {
        System.out.println("Nonparametric construction method");
    }
}
//Test class
public class StudentDemo {
    public static void main(String[] args) {
        //create object
        Student s = new Student();   
    }
}

8. Function of construction method

  • Used to initialize the data (properties) of the object
public class Student {
    /*
        Format:
               1. The method name should be the same as the class name, and the case should be the same
               2. There is no return value type, not even void
               3. There is no specific return value (return cannot bring back specific results)
     */
    private String name;
    private int age;
    // 1. If no constructor is written in a class, the system will provide a default parameterless constructor
    public Student(){}
    // 2. If the construction method is written manually, the system will no longer provide the default parameterless construction method
    public Student(String name, int age){
        this.name = name;
        this.age = age;
        System.out.println("I am Student Class construction method");
    }

    public void show(){
        System.out.println(name + "..." + age);
    }
}
public class TestStudent {
    public static void main(String[] args) {
        Student stu1 = new Student("Zhang San",23);
        stu1.show();

        Student stu2 = new Student();
    }
}

9. Precautions for construction method

Creation of construction method:

​ If no construction method is defined, the system will give a default parameterless construction method

​ If the construction method is defined, the system will no longer provide the default construction method

Creation of construction method:

​ If the construction method is not defined, the system will give a default parameterless construction method. If the construction method is defined, the system will no longer provide the default construction method

Recommended usage:

​ Whether used or not, manually write the nonparametric construction method and the parametric construction method

10. Coding and use of standard classes

code:

/*
    JavaBean Class: encapsulating data
 */
public class Student {
    private String name;
    private int age;
    public Student() {
    }
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public void show(){
        System.out.println(name + "..." + age);
    }
}
public class TestStudent {
    public static void main(String[] args) {
        // 1. Create an object with the nonparametric construction method and assign a value to the member variable through the setXxx method
        Student stu1 = new Student();
        stu1.setName("Zhang San");
        stu1.setAge(23);
        stu1.show();
        // 2. Directly assign values to attributes through the construction method with parameters
        Student stu2 = new Student("Li Si",24);
        stu2.show();
    }
}

11. Learn to view API help documents and use them (find your own materials and learn to use them - > key points)

Previous: summary of Java Basics (JavaSE) 06
Next:

Tags: Java Programming Back-end Programmer

Posted by barteelamar on Sun, 22 May 2022 09:05:50 +0300