【java】继承

继承

子类能够使用父类的方法和变量。

当我们在一个类里面实现了很复杂的方法,如果给我们一个新的类,要求重新实现这个方法,我们直接继承就可以了。

java 复制代码
public class Student {
    public String height;
    public void study() {
        System.out.println("学生应该努力学习");
    }
}
class XiaoMing extends Student {
}
class XiaoHong extends Student {
}
class Demo {
    public static void main(String[] args) {
        XiaoMing xiaoMing = new XiaoMing();
        XiaoHong xiaoHong = new XiaoHong();

        xiaoMing.height = "180.1";
        xiaoHong.height = "168.1";
        System.out.println(xiaoMing.height);
        System.out.println(xiaoHong.height);

        xiaoMing.study();
        xiaoHong.study();
    }
}

运行结果

方法的重写与重载
重载

只有方法能够重载,重载发生在同一个类当中。

要求:方法名相同但参数列表不同(变量形式不同)。

重写

发生在父子类当中。如果子类和父类的方法重合了,子类优先调用自己的方法。

子类可以重写父类的方法,要求重写其实现逻辑,方法名、参数列表必须相同。

返回值范围小于等于父类,抛出的异常范围小于等于父类,访问修饰符范围大于等于父类。

java 复制代码
public class Student {
    public String height;
    public void study() {
        System.out.println("学生应该努力学习");
    }
}
class XiaoMing extends Student {
    public void study() {
        System.out.println("小明应该努力学习");
    }
}
class XiaoHong extends Student {
}
class Demo {
    public static void main(String[] args) {
        XiaoMing xiaoMing = new XiaoMing();
        XiaoHong xiaoHong = new XiaoHong();

        xiaoMing.height = "180.1";
        xiaoHong.height = "168.1";
        System.out.println(xiaoMing.height);
        System.out.println(xiaoHong.height);

        xiaoMing.study();
        xiaoHong.study();
    }
}

运行结果

super关键字

① 在子类方法当中调用父类的方法。

java 复制代码
public class Student {
    public String height;
    public void study() {
        System.out.println("学生应该努力学习");
    }
    public void run(String name) {
        System.out.println(name + "应该经常跑步");
    }
}
class XiaoMing extends Student {
    public void study() {
        super.run("小明");
        System.out.println("小明应该努力学习");
    }
}
class XiaoHong extends Student {
    public void study() {
        super.run("小红");
    }
}
class Demo {
    public static void main(String[] args) {
        XiaoMing xiaoMing = new XiaoMing();
        XiaoHong xiaoHong = new XiaoHong();

        xiaoMing.study();
        xiaoHong.study();
    }
}

运行结果

② 调用父类的构造器,只能在子类构造器的第一行使用。

java 复制代码
public class Student {
    public String height;
    public void study() {
        System.out.println("学生应该努力学习");
    }
    public void run(String name) {
        System.out.println(name + "应该经常跑步");
    }
    public Student() {
        System.out.println("学生应该多多吃饭");
    }
}
class XiaoMing extends Student {
    public void study() {
        super.run("小明");
        System.out.println("小明应该努力学习");
    }
}
class XiaoHong extends Student {
    public XiaoHong() {
        super();
    }
}
class Demo {
    public static void main(String[] args) {
        XiaoMing xiaoMing = new XiaoMing();
        XiaoHong xiaoHong = new XiaoHong();

        xiaoMing.study();
        xiaoHong.study();
    }
}

运行结果

由于创建子类对象的同时必须创建父类对象,因此输出两条"学生应该多多吃饭"。

继承的层次

java只支持单继承。一个子类只能有一个父类,一个父类可以有多个子类。

相关推荐
曹牧2 分钟前
C#与Java后台交互
java·windows·microsoft·c#
张晓祥-长草颜团子5 分钟前
【重庆两江新区歌词】
java
东小西6 分钟前
【SAA实战】第 2 篇:模型与消息——ReactAgent 怎么挑模型、怎么传消息
java·后端·spring
LabVIEW开发10 分钟前
LabVIEW按段拆分TDMS文件的格式边界与重构
开发语言·数据库·重构·labview·labview知识·labview功能·labview程序
码匠许师傅18 分钟前
【C++ 面试真题】35. 聊聊 C++ 的万能引用(T&&)和完美转发(std::forward)
java·c++·面试
asdzx6722 分钟前
使用 Python 为 Excel 添加各类数据验证规则
开发语言·python·excel
Romantic_love_22 分钟前
理解USART真实收发过程
开发语言·stm32·单片机·学习
十年Java程序媛25 分钟前
注解已经加上,但是事务没有回滚?拆解 AOP 代理底层原因 + 代码示例
java
μθημα26 分钟前
K8s Pod 管理实战:虚拟机环境下的具体命令与步骤
java·docker·kubernetes
数据狐(Datafox)34 分钟前
1688.item_search工程实战:1688商品列表API分页采集与B2B货源数据分析落地
java·前端·数据分析