this、 super和final

this

指当前对象,也就是new 出来的对象,刚 new 出来的对象的属性是默认值或者null,我们通过调用set或者get方法赋值,通过 this 向当前的 new 出来的对象的属性赋值,如下:

public class Cat {

    private  String name;
    
    public Cat(String name) {
        this.name = name;
    }
}

我们还可以用于构造器

public class Cat {

    private int age;
    private  String name;

    public Cat() {
    	//调用Cat(String name)有参构造
       this("罗罗");
    }
    public Cat(String name) {
        this.name = name;
    }
}

super

指代父类

public class Animal {
    public Animal() {
    }
    public void shout(){
    }
    public void run(){
        System.out.println("run...");
    }
}

public class Cat extends Animal{

    private int age;
    private  String name;
    
    public Cat() {
    	//调用父类的无参构造
        super();
		//调用父类的方法
	    super.shout();
    }
 }

final

见名知意,是最终,最后的意思,也就是说 final 修饰的是不能被继承或者重写,或者修改的

猜你喜欢

转载自blog.csdn.net/qq_42224683/article/details/107411433