Day05_Keywords [Static, this, super]

1.Static keyword

Insert picture description here

1. When static modifies member variables

If a member variable uses the static keyword, then the variable no longer belongs to the object itself, but belongs to the class it belongs to. Multiple objects share the same data.

public class Student {
    
    

    private int id; // 学号
    private String name; // 姓名
    private int age; // 年龄
    static String room; // 所在教室
    private static int idCounter = 0; // 学号计数器,每当new了一个新对象的时候,计数器++

    public Student() {
    
    
        this.id = ++idCounter;
    }

    public Student(String name, int age) {
    
    
        this.name = name;
        this.age = age;
        this.id = ++idCounter;
    }
    
    public int getId() {
    
    
        return id;
    }

    public void setId(int id) {
    
    
        this.id = id;
    }

    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 class Demo01StaticField {
    
    

    public static void main(String[] args) {
    
    

        Student two = new Student("黄蓉", 16);
        two.room = "101教室";
        System.out.println("姓名:" + two.getName()
                + ",年龄:" + two.getAge() + ",教室:" + two.room
                + ",学号:" + two.getId());

        Student one = new Student("郭靖", 19);
        System.out.println("姓名:" + one.getName()
                + ",年龄:" + one.getAge() + ",教室:" + one.room
                + ",学号:" + one.getId());
    }

}
2. When static modifies the method
一旦使用static修饰成员方法,那么这就成为了静态方法。静态方法不属于对象,而是属于类的。

如果没有static关键字,那么必须首先创建对象,然后通过对象才能使用它。(MyClass obj = new MyClass(); obj.method();)
如果有了static关键字,那么不需要创建对象,直接就能通过类名称来使用它。(MyClass.methodStatic();)

无论是成员变量,还是成员方法。如果有了static,都推荐使用类名称进行调用。这样可读性行好,别人一看就知道它是静态的。
静态变量:类名称.静态变量
静态方法:类名称.静态方法()

注意事项:
1. 静态不能直接访问非静态。
原因:因为在内存当中是【先】有的静态内容,【后】有的非静态内容。
“先人不知道后人,但是后人知道先人。”
2. 静态方法当中不能用this。
原因:this代表当前对象,通过谁调用的方法,谁就是当前对象。
public class MyClass {
    
    

    int num; // 成员变量
    static int numStatic; // 静态变量

    // 成员方法
    public void method() {
    
    
        System.out.println("这是一个成员方法。");
        // 成员方法可以访问成员变量
        System.out.println(num);
        // 成员方法可以访问静态变量
        System.out.println(numStatic);
    }

    // 静态方法
    public static void methodStatic() {
    
    
        System.out.println("这是一个静态方法。");
        // 静态方法可以访问静态变量
        System.out.println(numStatic);
        // 静态不能直接访问非静态【重点】
//        System.out.println(num); // 错误写法!

        // 静态方法中不能使用this关键字。
//        System.out.println(this); // 错误写法!
    }

}


public class Demo02StaticMethod {
    
    

    public static void main(String[] args) {
    
    
        
        MyClass obj = new MyClass(); 
        obj.method();// 首先创建对象,然后才能使用没有static关键字的内容

        // 对于静态方法来说,可以通过对象名进行调用,也可以直接通过类名称来调用。
        obj.methodStatic(); // 正确,不推荐
        MyClass.methodStatic(); // 正确,推荐,你一这样写所有人都知道methodStatic是一个静态方法

        // 对于本类当中的静态方法,可以省略类名称
        myMethod();//他就等价于Demo02StaticMethod.myMethod();
    }

    public static void myMethod() {
    
    
        System.out.println("自己的方法!");
    }

}
3. Priority execution of static code blocks
静态内容总是优先于非静态,所以静态代码块比构造方法先执行。

静态代码块的典型用途:
当第一次用到本类时,本类中的静态代码块执行唯一的一次,一次性地对静态成员变量进行赋值。
public class Person {
    
    

    static {
    
    
        System.out.println("静态代码块执行!");
    }

    public Person() {
    
    
        System.out.println("构造方法执行!");
    }

}


public class Demo04Static {
    
    

    public static void main(String[] args) {
    
    
        Person one = new Person();
        Person two = new Person();
        //每次new一个对象就肯定立刻生成并执行该对象的构造方法,但静态方法比构造方法还要优先执行
    }

}

2.super keyword

1. The subclass will definitely call the parameterless construction method of the parent class (if the parent class only has the parameter construction method, the subclass will not call it)
There is a default implicit "super()" call in the subclass construction method, so It must be the parent class structure that is called first, and the subclass structure that is executed later.
And the super parent class construction call must be the first statement of the subclass construction method.

public class Fu {
    
    
    public Fu() {
    
    
        System.out.println("父类的无参数构造方法!");
    }
}

public class Zi extends Fu {
    
    
    public Zi() {
    
    
        super(); // 不管你写不写这行代码,系统都默认给你带上了
        System.out.println("子类构造方法!");
    }
}

public class Demo01Constructor {
    
    
    public static void main(String[] args) {
    
    
        Zi zi = new Zi();
    }
}

输出:
父类的无参数构造方法!
子类构造方法!

2. The subclass must call the parent class's construction method. If you don't write it, you will give it super(); if you write it, you will use the specified super to call it. There can only be one super, and it must be the first one.

public class Fu {
    
    
    public Fu(int num) {
    
    
        System.out.println("父类的有参数构造方法!");
    }
}

public class Zi extends Fu {
    
    
    public Zi() {
    
    
        super(); // 不管你写不写这行代码,系统都默认给你带上了
        System.out.println("子类构造方法!");
    }
}

public class Demo01Constructor {
    
    
    public static void main(String[] args) {
    
    
        Zi zi = new Zi();
    }
}

会报错,当你在子类不写super()时系统默认给你赠送了一个super()来继承父类**无参数的**构造方法,而父类只有一个**有参数**的构造方法,而你子类没有写有参数的构造方法。你改错的方法就是自己写一个super(10)来抵消掉系统给你的super()

3. There are three uses of the super keyword:

  • In the member method of the subclass, access the member variables of the parent class.
  • In the member methods of the subclass, access the member methods of the parent class.
  • In the construction method of the subclass, access the construction method of the parent class.
public class Zi extends Fu {
    
    

    @Override
    public void method() {
    
    
        super.method(); // 在子类的成员方法中,访问父类的成员方法。
        System.out.println("子类方法");
    }

3.this keyword

this关键字用来访问本类内容。用法也有三种:

1. 在本类的成员方法中,访问本类的成员变量。
2. 在本类的成员方法中,访问本类的另一个成员方法。
3. 在本类的构造方法中,访问本类的另一个构造方法。
在第三种用法当中要注意:
A. this(...)调用也必须是构造方法的第一个语句,唯一一个。
B. superthis两种构造调用,不能同时使用。
  1. In the member methods of this class, access the member variables of this class.
局部变量:         直接写成员变量名
本类的成员变量:    this.成员变量名
父类的成员变量:    super.成员变量名
public class Zi extends Fu {
    
    

    int num = 20;

    public void method() {
    
    
        int num = 30;
        System.out.println(num); // 30,局部变量
        System.out.println(this.num); // 20,本类的成员变量
        System.out.println(super.num); // 10,父类的成员变量
    }
}
  1. In the member methods of this class, access another member method of this class.
public class Zi extends Fu {
    
    

    public void methodA() {
    
    
        System.out.println("AAA");
    }

    public void methodB() {
    
    
        this.methodA(); //在本类的成员方法中,访问本类的另一个成员方法。
        System.out.println("BBB");
    }

}
  1. In the construction method of this class, visit another construction method of this class.
    Pay attention to the third usage:
    A. This(...) call must also be the first statement of the constructor, and the only one.
    B. The two construction calls of super and this cannot be used at the same time.
public class Zi extends Fu {
    
    

    public Zi() {
    
    
        this(123); // 本类的无参构造,调用本类的有参构造
    }

    public Zi(int n) {
    
    
        this(1, 2); //在本类的构造方法中,访问本类的另一个构造方法。
    }
    
    public Zi(int n, int m) {
    
    
    
    }
}
public class Zi extends Fu {
    
    

    public Zi() {
    
    
        super(); // 错误,super和this不能同时使用,因为 this(...)调用必须是构造方法的第一个语句,super(...)调用也必须是构造方法的第一个语句,到底谁是第一个?
        this(123); // 本类的无参构造,调用本类的有参构造
    }

    public Zi(int n) {
    
    
        this(1, 2);
    }

    public Zi(int n, int m) {
    
    
    }
}

Guess you like

Origin blog.csdn.net/weixin_45014721/article/details/114917070