super关键字基本用法
super关键字的用法和this关键字相似
-
this关键字:代表本类对象的引用
-
super关键字:代表父类存储空间的标识(可以理解为父亲对象的引用)
super关键字的相关知识
继承中构造方法的访问特点
父类代码
public class _138SuperConstructedFunction {
public _138SuperConstructedFunction(){
System.out.println("我是父类无参构造方法");
}
public _138SuperConstructedFunction(int age){
System.out.println("我是父类有参构造方法");
}
}
子类代码
public class _138SuperConstructedFunctionSon extends _138SuperConstructedFunction {
public _138SuperConstructedFunctionSon(){
//super() 子类构造方法的第一句默认有:super()
System.out.println("我是子类的无参构造方法");
}
public _138SuperConstructedFunctionSon(int age){
System.out.println("我是子类的有参构造方法");
}
}
实例及输出结果
public static void main(String[] args) {
_138SuperConstructedFunctionSon s1=new _138SuperConstructedFunctionSon();
//执行学生类的无参构造方法
_138SuperConstructedFunctionSon s2=new _138SuperConstructedFunctionSon(20);
//执行学生类的有参构造方法
}