java super()的用法1

来源:互联网 发布:过山车大亨 for mac 编辑:程序博客网 时间:2024/05/17 07:33

super 在继承中有三个功能:

Super() 表示子类指定调用基类的构造函数版本 ,必须放在构造函数第一行

Super.  在子类中访问父类中继承的数据(子类 data /   父类super.data

Super.func()  访问基类的成员方法

package s1;
class person {
    String name="zhangsan"; 
    int age; 
    public person() {
        print();
        this.age=0;
        this.name=name;
    }


    public void print() {
        System.out.println("名字" + name + "年龄 " +age);
    }
}
public class student extends person {    
    public student() {
        super();    // 调用基类的构造函数
    }
    public static void main(String[] args) {
        new student();
    }
}

原创粉丝点击