day07_ Object oriented introduction, encapsulation and construction methods

Getting started with object oriented

Object oriented and process oriented ideas

  • Process oriented: it is a process centered programming idea. Each step of realizing the function is realized by itself
  • Object oriented: it is an object-centered programming idea, which realizes specific functions by commanding objects

Relationship between classes and objects

  • 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. Everything that exists objectively is an object, so we often say that everything is an object.

Simple understanding: class is a description of things, and the object is a concrete thing

Class understanding

  • Class is an abstraction of a class of things with common attributes and behaviors in real life
  • A class is the data type of an object. A class is a collection of objects with the same properties and behavior
  • Simple understanding: class is a description of real things

Composition of classes

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

Class definition

Class consists of two parts: attribute and behavior

  • Attribute: represented by member variables in the class
  • Behavior: represented by member methods in a class

Class definition steps:

  1. Define class
  2. Write the member variable of the class
  3. Write member methods of classes

Code example

public class Student {
    // attribute : full name, Age
    // Member variable: The format of the variable is the same as that defined before, It's just that the location has changed, Outside method in class
    String name;
    int age;

    // behavior : study
    // Member method: The format is the same as the method defined before, It's just removed static keyword.
    public void study() {
        System.out.println("study");
    }
}

Object creation and use

Format for creating objects:

  • Class name object name = new class name ();

Format of calling member:

  • Object name. Member variable
  • Object name. Member method ();

Code example

public class TestStudent {
    /*
        Format for creating objects:
                Class name object name = new class name ();
        Format of calling member variable:
                Object name. Variable name
        Format of calling member method:
                Object name. Method name ();
     */
    public static void main(String[] args) {
        // Class name object name = new Class name();
        Student stu = new Student();
        // Object name.Variable name
        // Default initialization value
        System.out.println(stu.name);  // null
        System.out.println(stu.age);   // 0

        stu.name = "Zhang San";
        stu.age = 23;

        System.out.println(stu.name);  // Zhang San
        System.out.println(stu.age);   // 23

        // Object name.Method name();
        stu.study();
      
        // Full class name(Package name + Class name)
        System.out.println(stu);
    }
}

Object memory graph

Single object memory map

 

 

Multiple object memory graph

 

 

Summary: multiple objects have different memory partitions in heap memory, member variables are stored in their respective memory areas, and member methods are shared by multiple objects

Multiple objects point to the same memory map

Summary:

  • When references to multiple objects point to the same memory space (the address values recorded by variables are the same)
  • 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.

Differences between member variables and local variables

Different positions in class:

  • Member variable (outside method in class)
  • Local variable (inside method or on method declaration)

Different locations in memory:

  • Member variable (heap memory)
  • Local variable (stack memory)

Different life cycles:

  • Member variable (exists with the existence of the object and disappears with the disappearance of the object)
  • Local variable (exists with the method call and disappears after the method call is completed)

Different initialization values:

  • Member variable (with default initialization value)
  • Local variable (there is no default initialization value, which must be defined before assignment)

encapsulation

Packaging overview

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

Encapsulation principle

  • Some information of the class is hidden inside the class, and external programs are not allowed to access it directly. Instead, the operation of hidden information and access to member variable private are realized through the methods provided by the class, and the corresponding getXxx()/setXxx() methods are provided

Packaging benefits

  • The operation of member variables is controlled by methods, which improves the security of the code, encapsulates the code with methods, and improves the reusability of the code

Encapsulation of member variables

The encapsulation of member variables is realized through the private keyword. Private is a modifier that can be used to modify members (member variables, member methods)

characteristic  

  • Members modified by private can only be accessed in this class. If private modified member variables need to be used by other classes, corresponding operations are provided
  • The "get variable name ()" method is provided to obtain the value of the member variable. The method is decorated with public
  • The set variable name (parameter) method is provided to set the value of the member variable. The method is decorated with public

this keyword

This represents the reference of the current calling method, the method of which object is invoked, and which object this represents.

Overview: this modified variable 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. We can distinguish the member variable from the formal parameter by the variable name

Member variable encapsulation, code example

public class Student {
    private String name;
    private int age;

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void show() {
        System.out.println(name + "," + age);
    }
}

Construction method

characteristic

  • 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

Code example

class Student {
    private String name;
    private int age;

    //Construction method
    public Student() {
        System.out.println("Nonparametric construction method");
    }

    public void show() {
        System.out.println(name + "," + age);
    }
}
/*
    Test class
 */
public class TestStudent {
    public static void main(String[] args) {
        //create object
        Student s = new Student();
        s.show();
    }
}

Constructor function: used to initialize the data (properties) of an object

class Student {
   /*
        Format:

               1. The method name should be the same as the class name, and the case should be consistent
               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 construction method
    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);
    }
}

/*
    Test class
 */
public class TestStudent {
    public static void main(String[] args) {
        Student stu1 = new Student("Zhang San", 23);
        stu1.show();

        Student stu2 = new Student();
    }
}

Precautions for construction method

Creation of construction method:

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

Creation of construction method:

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

Recommended usage:

  • Whether it is used or not, manually write the nonparametric construction method and the parametric construction method

Coding and use of standard classes

/*
    JavaBean Classes: 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);
    }
}

Test class

public class TestStudent {
    public static void main(String[] args) {
        // 1. Creating objects using parameterless construction methods, adopt setXxx Method to assign a value to a member variable
        Student stu1 = new Student();
        stu1.setName("Zhang San");
        stu1.setAge(23);
        stu1.show();

        // 2. Through the construction method with parameters, Assign values directly to attributes
        Student stu2 = new Student("Li Si",24);
        stu2.show();
    }
}

Posted by raguskra on Wed, 01 Dec 2021 13:53:21 -0800