super关键字

概述

super指父类对象,用来区分父类和子类,用于调用父类的属性和方法

用法和this非常类似:this指当前对象,super指父类对象

调用父类的属性和方法

public class Father {
    public int age = 60;
    
    public void play() {
        System.out.println("下象棋");
    }
}

public class Son extends Father {
    public int age = 16;
    
    @Override
    public void play() {
        System.out.println("玩游戏");
    }
    
    public void showAge() {
        int age = 20;
        System.out.println("局部变量:" + age);
        System.out.println("当前对象成员变量:" + this.age);
        System.out.println("父类对象成员变量:" + super.age);
    }
    
    public void callPlay() {
        // 调用当前对象的方法
        this.play();
        // 调用父类对象的方法
        super.play();
    }
}

public class Demo {
    public static void main(String[] args) {
        Son son = new Son();
        
        son.showAge();
        
        son.callPlay();
    }
}

调用父类的构造方法

默认调用父类的无参构造,且必须在代码的第一行

class Father {
    private String name;

    public Father() {
        System.out.println("Father's Constrator be performed");
    }
    
    public Father(String name) {
        System.out.println("Father's Constrator be performed with name");
    }
}

class Son extends Father {
    private int age;

    public Son() {
        super();
        System.out.println("Son's Constrator be performed");
    }
    
    public Son(String name, int age) {
        super(name);
        this.age = age;
        System.out.println("Son's Constrator be performed with name and age");
    }
}

public class TestSon {
    public static void main(String[] args) {
        Son son = new Son();
    }
}

【注意】super() 和this() 代码不能共存(都必须在首行),但是实际效果其实是可以的,如果不写 super() 也会自动调用

总结

1、super指父类对象,对比this关键字,使用方法都一样

2、super() 和this() 代码不能共存(都必须在首行),但是实际效果其实是可以的,如果不写 super() 也会自动调用

3、父类的属性要交给父类的构造方法去操作,没什么事就不要去使用 super() 来调用父类的构造方法了

final关键字

概述

final表示最终的,用来修饰变量,方法和类

1、final 修饰的局部变量只能被赋值一次
2、final 修饰的成员变量
3、final 修饰的基本类型变量是一个常量(只能被赋值一次),引用类型变量不可修改地址,如对象
4、final 修饰的方法不能被重写
5、final 修饰的类不能被继承
package com.fc.j._final;
/*
 * final修饰的局部变量
 */
public class FinalDemo1 {
    public static void main(String[] args) {
        // 测试final修饰的修饰的变量
        final int num;
        
        num = 10;
        
        System.out.println(num);
        
        /*
         * 被final修饰的变量只能赋值一次
         * 
         * The final local variable num may already have been assigned
         * 
         * 被final修饰的局部变量num可能已经被赋值
         */
        // num = 20;
    }
}

// final修饰的类不能被继承,断子绝孙
class Father {
    /*
     * final 修饰的成员变量必须在声明时就赋值
     * 
     * The blank final field age may not have been initialized
     * 空白的final成员变量可能未被初始化
     */
     // final int age;
    final int age = 16;
    
    public final void play() {
        System.out.println("下棋");
    }
}

class Son extends Father {
    /*
     *  Cannot override the final method from Father
     *  无法重写被final修饰的方法
     */
//    @Override
//    public final void play() {
//        
//    }
}

特点

final修饰可以保证安全性,比如数组的长度属性,String类,这些都是final修饰的,保证不可变

总结

1、final表示最终的,可以修饰变量方法和属性

2、final修饰的基本数据类型的成员变量只能被赋值一次

3、final修饰的引用数据类型的成员变量地址不可变,但不影响对堆中元素的操作

4、final修饰的方法不能被重写

5、final修饰的类不能被继承

最后修改:2021 年 01 月 24 日 12 : 31 PM
如果觉得此文章有用,请随意打赏