5.4

What is the meaning of the this keyword? How to use the
  this keyword: A reference to the current object
  In most cases, it is used to solve the phenomenon that the incoming parameters and member variables have the same name.
  You can use the this keyword to call other constructs in the current class Method this();
What does the static keyword mean? How to use it?
  static: static, can modify variables (member variables, methods, classes)
  Modified variables:
    static modified variables do not exist in stack space and heap space, they exist In the data area
    No matter how many objects are instantiated, all objects share a
    statically modified variable, and do not need to be instantiated when accessing, just take the class name to point it out

  Modified method:
    use, use some tool classes
    because it does not need to be instantiated, it is more convenient to use
  Note:
    static modified methods cannot access non-static members!!!

public  class Student {
     private String name;
     public  static String school;     // school 
    public Student() {
         // this(""); 
        System.out.println("Empty parameter constructor!" );
        
    }
    
    public static void showNameInfo(String name) {
        char c = school.charAt(0);
/*        char c = name.charAt(0);
        String n = name.substring(1);
        System.out.println("Student last name: " + c);
        System.out.println("Student name: " + n); */
    }

    public void showInfo2() {
        
    }
    
    public Student(String name) {
        this();
        this.name = name;
    }
    
    public void setName(String name) {
        // this();
        this.name = name;
    }
    
    public String getName() {
        return this.name;
    }
}

Print

public class Test {

    public static void main(String[] args) {    
        
        // TODO Auto-generated method stub
/*        Student s1 = new Student("小红");
        Student s2 = new Student("小明");
        
        Student.school = "Shandong University of Technology";   //The static member variable is called with the class name
        
        System.out.println(Student.school);
        
        Student.showNameInfo(s1.getName());
        // Math.
        
        m();*/
    }
    

11, Introduction to Singleton Pattern
12, Use of Documentation Comments
Exercise: Be able to write your own singleton pattern

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325305292&siteId=291194637
5.4