publicclassSuperOperator{publicstaticvoidmain(String[] args){/*TODO 继承中的构造方法:1.对于子类对象在构建时,必须先创建其父类对象2.对于子类对象在调用构造方法时,默认需要先调用父类的无参构造3.在子类中使用super()调用父类的构造方法,必须要放在子类构造方法的第一行 ?由于必须要先构建父类对象,所以根据代码执行顺序,需要先将父类的构造方法执行完成TODO 使用 super 关键字super关键字用于表示父类,可以通过 super()调用父类的构造方法 或者使用 super.调用成员方法或变量*/Son son =newSon("李杨",17);son.show();System.out.println("===================");Zi z =newZi();z.show();}}/*对于一个代码文件中,只能存在有一个 public 修饰的class类*/classGrandFather{String name;int age;publicGrandFather(){System.out.println("爷爷出现了...");}publicfinalvoidshow(){System.out.println("我叫"+ name +" 今年:"+ age);}}//final class Father extends GrandFather {classFatherextendsGrandFather{publicFather(String name,int age){super.name = name;this.age = age;System.out.println("父亲出现了...");}}classSonextendsFather{publicSon(String name,int age){super(name,age);System.out.println("儿子出现了...");}// @Override// public void show() {// System.out.println("这是儿子中重新定义的show方法");// }}classFu{publicint num =10;publicFu(){System.out.println("fu");}}classZiextendsFu{publicint num =20;publicZi(){System.out.println("zi");}publicvoidshow(){int num =30;System.out.println(num);System.out.println(this.num);System.out.println(super.num);}}
5. final关键字
importjava.util.Arrays;publicclassDemo_Final{finalstaticdoublePI=3.145926;finalstaticint[]INT_NUM={3,4,6,5};publicstaticvoidmain(String[] args){/*TODO final① 使用该关键字修饰class,后续不能被其他的类所继承 表示为最终类② 使用该关键字修饰方法,后续不能被其他的子类所重写③ 使用该关键字修饰变量,表示一个常量 定义后不可改变其中的数据 必须要进行初始化常量的命名:所有单词字母大写,单词间使用 下划线 连接*/System.out.println(PI);// PI = 3.1459267;INT_NUM[0]=4;// INT_NUM = new int[5]; // 对于引用类型赋值不能修改其内存地址,但是可以修改其地址上对象的数据System.out.println(Arrays.toString(INT_NUM));}}