java based object-oriented, encapsulation overview

Freshman rookie is learning java by himself. If there are mistakes in the article, welcome to exchange and correct them and make common progress! 🐵🐵🐵

I Object oriented overview

  1. Object oriented: a programming idea used in programming
  2. Object oriented features: encapsulation, inheritance, polymorphism
  3. Object: everything is an object. It is an objective entity that can be seen and touched
  4. Class: that is, type. The abstraction and generalization of a class of things with common attributes and behaviors in real life is actually a description of things
  5. Attributes are objects with various characteristics
  6. Object behavior: the actions an object can perform
  7. A class is an abstraction of an object, and an object is an entity of a class
  8. Class definition format:

[modifier] class name{

Attribute: that is, the attribute of variable, member variable and object;

Behavior: that is, method, object behavior, object method, remove the static keyword

public return value data type method name (parameter list){

Method body;

return data;

}

}

  1. Definition of object: using objects must depend on classes. Without classes, there will be no objects
  2. Format for creating objects:

Class name object name = new class name ();

Class name: the data type of the object to be created

=Storage: assign the first address of the memory space applied by the object in the heap area to the object name

new: apply for a piece of memory space in the heap area and assign the first address of the application space to the object name

Class name: consistent with the previous class name

() indicates that a method is called, which is the constructor (the method used by the constructor to create the object)

Use of objects:

Call properties:

Object name Attribute name; (access the value in the attribute);

Object name Attribute name = value; (assignment operation)

Call method:

Object name Method name ();

  1. Code example:
public class Student {
    //Member variable
    String name;
    int age;

    //Member method
    public void study() {
        System.out.println("study hard and make progress every day");
    }

    public void work() {
        System.out.println("Try to knock in the code...");
    }
}
public class StudentDemo {
    public static void main(String[] args) {
        //create object
        Student s = new Student();
        System.out.println(s);
        s.name = "xiaojiang";
        s.age = 20;
        System.out.println(s.name);
        System.out.println(s.age);

        //Call member method
        System.out.println(s.name + "-" + s.age);
        s.study();
        System.out.println(s.name + "-" + s.age);
        s.work();
    }
}

II Object memory graph

  1. Single object:

  2. Multiple memory maps:

  3. Multiple addresses of the same object:

  4. Differences between member variables and local variables:

    differenceMember variablelocal variable
    Different positions in the definition classOutside method in classMethod in class
    Different spatial locationsHeap memoryStack memory
    Different life cyclesExists as the object exists, and disappears as the object disappearsExists with the method call and disappears with the end of the method
    Different initialization valuesThere are default initialization valuesThere is no default initialization value. You must define the assignment before using it

III private keyword

  1. Encapsulation concept: hide the implementation details of object attributes and disclose a public method
  2. Benefits of encapsulation:
  • Hide implementation details
  • Improve code reusability
  • Improve security
  • Principles of class encapsulation
  • Hide the attributes of things
  • Hide the details of things
  • Provide a public access method
  1. private keyword can modify the following contents:
  • Decorated member variable
  • Modifier member method
  • Modification construction method
  • Decorate internal classes
  1. After being modified by private, it can only be accessed directly inside the class, but after leaving the class, it cannot be accessed directly outside the class
  2. After the attribute (member variable) is privatized, the outside world cannot access it directly, so it is necessary to provide a public access method so that the outside world can access the member variable indirectly
    Public method of attribute assignment: Setter method (assign value to member variable) public method of attribute value: Getter method (get the value of member variable)

Property format of Setter method of property:

  • Data type property name

    public void set + initial capital of attribute name (data type parameter name) {parameter data type is consistent with member variable data type

Attribute name = parameter name;

}

For example: String name;

public void setName(String n){

name = n;

}

Property's Getter method format:

  • Data type property name

public return value data type get + attribute name initial capital () {return value data type is consistent with member variable data type

return attribute name;

}

For example: String name;

public String getName(){

return name;

}

Quick generation method: right click to find the generate key to quickly generate getter and setter methods

IV Construction method

  1. Constructor is a special method, also known as constructor
  2. Format of construction method:
    Modifier (usually public) method name (parameter list){
    Method body;
    }

Format Description:

  1. As like as two peas, the name of the method must be exactly the same as the case name.

  2. The constructor has no return value. Don't even write void

  3. The constructor does not have a return statement. If you must write a return, write only one return;

  1. The construction method does not need to be called manually. The construction method is automatically called by the jvm when the object is created

  2. Do not call a constructor with an object name

  3. Create an object and call the constructor only once

3. Precautions for 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; If you define a construction method with parameters and use a parameterless construction method, you must write a parameterless construction method

  1. Overload of construction method:

Overload definition: in a class, the method name is the same and the parameter list is different (type, order and number), which is independent of the return value type and parameter name

The constructor name is the same as the class name, but the parameter list is different and there is no return value Then multiple construction methods are overloaded methods

  1. Code example:
public class StudentDemo {
    public static void main(String[] args) {
        //No parameter creation
        Student s = new Student();
        s.show();
        //Create a parametric object
        //public Student(String name)
        Student s2 = new Student("xiaojiang");
        s2.show();
        //public Student(int age)
        Student s3 = new Student(20);
        s3.show();
        //public Student (String name,int age)
        Student s4 = new Student("xiaojiang", 20);
        s4.show();
    }
}
public class Student {
    //Member variable
    private String name;
    private int age;

    //Nonparametric construction method
    public Student() {
    }

    //Parametric structure
    public Student(String name) {
        this.name = name;
    }

    public Student(int age) {
        this.age = age;
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

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

public class StudentDemo {
    public static void main(String[] args) {
        //Use setxxx() to assign values after the object is created by the parameterless construction method
        Student s1 = new Student();
        s1.setName("xiaojiang");
        s1.setAge(20);
        s1.show();
      //Use the object constructed with parameters to directly create the object with attributes, and assign values through the construction with parameters
        Student s2=new Student("xiaojiang",20);
         s2.show();
    }
}
public class Student {
    //Member variable
    private String name;
    private int age;

    //Constructor class names must be consistent
    //Define parameterless construction method
    public Student() {
        System.out.println("Nonparametric construction method...");
    }
    //Define parametric construction method
    public Student(String name, int age) {
        System.out.println("Assign a value to a member property through a parameterized construct...");
        this.name = name;
        this.age = age;
    }

    //set/get method
    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

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

Quick generation method: right click to find the generate key, and find the constructor key to quickly generate the construction method

V this keyword

  1. The variable modified by this refers to the member variable
  2. this keyword is used to hide member variables from local variables
  3. this represents the reference of the class object
  4. this represents the object to which the method is called

If the formal parameter of the method is the same as the member variable, the variable without this modifier refers to the formal parameter

If the formal parameter of the method is not the same as the member variable, the variable without this modifier refers to the member variable

5. Schematic diagram of this:

  1. This keyword can be used to access the properties, methods and constructors of this class
  2. Syntax format of access member method: this Method name (parameter list)
  3. Access constructor syntax format: this (parameter list); Note: it can only be used in constructors (that is, it can only access another constructor in one constructor) and must be placed in the first statement
  4. this cannot be used outside the class definition. It can be used directly in the method of the class definition

Code case:

public class Student {
    //Member properties member variables
    private String userName;
    private int userAge;

    //Nonparametric construction method
    public Student() {
        // this(21);  this(); Call the parameterized construction. Pay attention to this();
        // When calling other construction methods, they must be placed on the first line of the statement
        //System.out.println("nonparametric construction method...);
        //this("xiaojiang",21); this(); Call parameterized construction
        // this("xiaojiang");  this(); Call parameterized construction
    }

    //Parametric construction method
    public Student(String userName, int userAge) {
        this.userName = userName;
        this.userAge = userAge;
    }

    public Student(String userName) {
        this.userName = userName;
    }

    public Student(int userAge) {
        this.userAge = userAge;
    }


    //set/get method
    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setUserAge(int userAge) {
        this.userAge = userAge;
    }

    public String getUserName() {
        return userName;
    }

    public int getUserAge() {
        return userAge;
    }

    public void a1() {
        System.out.println("method a1");
        this.a2();//Call to other methods through this
    }

    public void a2() {
        System.out.println("method a2");
    }


    public static void main(String[] args) {
        Student s = new Student();
        s.a1();
    }

}

Keywords: Java Back-end

Added by bios on Sun, 06 Feb 2022 10:47:31 +0200