栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA基础学习之继承2(super)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA基础学习之继承2(super)

super

学习来源:狂神说JAVA

super注意点:

    super调用父类的构造方法,必须在构造方法的第一个

    super必须只能出现在子类的方法或者构造方法中!

    super和l this不能同时调用构造方法

super与this比较:

    代表的对象不同:
    this:本身调用者这个对象
    super:代表父类对象的应用

    前提

    this:没哟继承也可以使用
    super:只能在继承条件才可以使用

    构造方法
    this() ; 本类的构造
    super(); 父类的构造

代码
package com.oop;
import com.oop.demo05.Person;
import com.oop.demo05.Student;

public class Application {
    public static void main(String[] args) {
        Student student = new Student();
    }
}
package com.oop.demo05;
//Student 是 Person 的一种 ;Student(派生类,子类) 继承 Person
//子类继承父类,会拥有父类的方法
public class Student extends Person{
    
    public Student() {
        //隐藏代码:调用了父类的无参构造
        super();//必须在子类的构造器第一行
        System.out.println("Student无参构造执行了");
    }
    
    public Student(String name) {
        this.name = name;
    }
    
    private String name = "qingjiang";
}
package com.oop.demo05;
//Person 父类
public class Person {
    
    public Person() {
        System.out.println("Person无参执行了");
    }

    protected String name = "kuangshen";

}
异常截图
    this与super也一样,只能放在构造器的第一行,因为都要放在第一行所以不能同时使用
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/770614.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号