Java EE learning route -- object oriented Foundation

1. Member variables and local variables

1.1 differences between member variables and local variables

  • Different positions in the class: member variable (outside the method in the class) local variable (inside the method or on the method declaration)
  • Different locations in memory: member variables (heap memory) local variables (stack memory)
  • Different life cycles: member variables (exist with the existence of the object and disappear with the disappearance of the object) local variables (exist with the call of the method and disappear after the call of the method)
  • Different initialization values: member variable (with default initialization value) local variable (without default initialization value, it must be defined before assignment)

2. Packaging

2.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, 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

Example code:

/*
    Student class
 */
class Student {
    //Member variable
    String name;
    private int age;

    //Provide get/set methods
    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 = "Lin Qingxia";
        s.setAge(30);
        //Call the show method
        s.show();
    }
}

2.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("Lin Qingxia");
            s.setAge(30);
    
            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());
    
        }
    }
    

2.3 packaging ideas

  1. Packaging 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. Encapsulation principle
    Some information of the class is hidden inside the class, which is not allowed to be directly accessed by external programs. Instead, the operation and access of hidden information are realized through the methods provided by the class
    The member variable private provides the corresponding getXxx()/setXxx() methods
  3. Packaging benefits
    Methods are 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

3. Construction method

3.1 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 {
    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 StudentDemo {
    public static void main(String[] args) {
        //create object
        Student s = new Student();
        s.show();
    }
}

3.2 function of construction method

  • Used to initialize the data (properties) of an object
package com.itheima.constructor;

public 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 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);
    }
}
package com.itheima.constructor;

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

        Student stu2 = new Student();
    }
}

3.3 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

3.4 coding and use of standard classes

code:

package com.itheima.test3;

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

package com.itheima.test3;

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();
    }
}

Keywords: Java JavaEE

Added by ninedoors on Fri, 31 Dec 2021 21:35:08 +0200