美文网首页
Java三大特性之继承

Java三大特性之继承

作者: Java编程日记 | 来源:发表于2022-04-06 14:42 被阅读0次

    1. 继承

    众所周知,我们Java语言是一种面向对象的编程语言,每当我们提到Java的特性,大家一定会在脑海里浮现出Java中的 继承、多态以及封装 。 我们在日常的开发中经常会用到这三种特性,本文首先给大家介绍这三大特性之一——继承。

    1.1 继承的实现

    • 继承的概念

    继承是面向对象三大特征之一,可以使得子类具有父类的属性和方法,还可以在子类中重新定义,以及追加属性和方法。

    • 实现继承的格式: 继承通过extends实现
    • 格式: class 子类 extends 父类 { }

    <pre class="hljs scala" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 0.75em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">class Dog extends Animal { }
    </pre>

    我们为了让大家更好的掌握其格式,现在给出一个简单的案例加以说明:

    <pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">public class Fu {
    public void show() {
    System.out.println("show方法被调用");
    }
    }
    public class Zi extends Fu {
    public void method() {
    System.out.println("method方法被调用");
    }
    }
    public class Demo {
    public static void main(String[] args) {
    //创建对象,调用方法
    Fu f = new Fu();
    f.show();

        Zi z = new Zi();
        z.method();
        z.show();
    }
    

    }</pre>

    该案例执行的结果如下:

    [图片上传失败...(image-a4b144-1649227330310)]

    1.2 继承的好处和弊端

    • 继承好处

    • 提高了代码的复用性(多个类相同的成员可以放到同一个类中)

    • 提高了代码的维护性(如果方法的代码需要修改,修改一处即可)

    • 继承弊端

    • 继承让类与类之间产生了关系,类的耦合性增强了,当父类发生变化时子类实现也不得不跟着变化,削弱了子类的独立性

    • 继承的应用场景

    • 使用继承,需要考虑类与类之间是否存在 is..a 的关系,不能盲目使用继承

    • is..a的关系:谁是谁的一种,例如:老师和学生是人的一种,那人就是父类,学生和老师就是子类

    2. 继承中的成员访问特点

    2.1 继承中变量的访问特点

    在子类方法中访问一个变量,采用的是 **就近原则 **。

    1. 子类局部范围找
    2. 子类成员范围找
    3. 父类成员范围找
    4. 如果都没有就报错(不考虑父亲的父亲…)

    我们还是以上面的案例加以说明:

    <pre class="prettyprint hljs cs" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">class Fu {
    int num = 10;
    }
    class Zi {
    int num = 20;
    public void show(){
    int num = 30;
    System.out.println(num);
    }
    }
    public class Demo1 {
    public static void main(String[] args) {
    Zi z = new Zi();
    z.show(); // 输出show方法中的局部变量30
    }
    }</pre>

    具体该案例执行的结果如下:

    [图片上传失败...(image-8a55f0-1649227330310)]

    2.2 super

    • thisVS.super关键字

    • this :代表 本类对象 的引用

    • super :代表 父类存储空间 的标识(可以理解为父类对象引用)

    • this和super的使用:

    • 成员变量

    • this.成员变量 - 访问本类成员变量

    • super.成员变量 - 访问父类成员变量

    • 成员方法

    • this.成员方法 - 访问本类成员方法

    • super.成员方法 - 访问父类成员方法

    • 构造方法:

    • this(…) - 访问本类构造方法

    • super(…) - 访问父类构造方法

    2.3 继承中构造方法的访问特点

    注意:子类中所有的构造方法默认都会访问父类中无参的构造方法

    子类会继承父类中的数据,可能还会使用父类的数据。所以,子类初始化之前,一定要先完成父类数据的初始化,原因在于,每一个子类构造方法的第一条语句默认都是: super()

    问题:如果父类中没有无参构造方法,只有带参构造方法,该怎么办呢?

    1. 过使用super关键字去显示的调用父类的带参构造方法

    2. 父类中自己提供一个无参构造方法

    推荐方案:

    自己给出无参构造方法

    2.4 继承中成员方法的访问特点

    通过子类对象访问一个方法

    1. 子类成员范围找

    2. 父类成员范围找

    3. 如果都没有就报错(不考虑父亲的父亲…)

    2.5 super内存图

    • 对象在堆内存中,会单独存在一块super区域,用来存放父类的数据

    [图片上传失败...(image-4c0874-1649227330310)]

    2.6 方法重写

    • 1、 **方法重写概念 **

    • 子类出现了和父类中一模一样的方法声明(方法名一样,参数列表也必须一样)

    • 2、 **方法重写的应用场景 **

    • 当子类需要父类的功能,而功能主体子类有自己特有内容时,可以重写父类中的方法,这样,即沿袭了父类的功能,又定义了子类特有的内容

    • 3、 **Override注解 **

    • 用来检测当前的方法,是否是重写的方法,起到【校验】的作用

    2.7 方法重写的注意事项

    • 方法重写的注意事项
    1. 私有方法不能被重写( 父类私有成员子类是不能继承的 )
    2. 子类方法访问权限不能更低( public > 默认 > 私有 )

    我们仍是通过前面的案例加以说明这个问题:

    <pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">public class Fu {
    private void show() {
    System.out.println("Fu中show()方法被调用");
    }

    void method() {
        System.out.println("Fu中method()方法被调用");
    }
    

    }

    public class Zi extends Fu {

    /* 编译【出错】,子类不能重写父类私有的方法*/
    @Override
    private void show() {
        System.out.println("Zi中show()方法被调用");
    }
    
    /* 编译【出错】,子类重写父类方法的时候,访问权限需要大于等于父类 */
    @Override
    private void method() {
        System.out.println("Zi中method()方法被调用");
    }
    
    /* 编译【通过】,子类重写父类方法的时候,访问权限需要大于等于父类 */
    @Override
    public void method() {
        System.out.println("Zi中method()方法被调用");
    }
    

    }</pre>

    [图片上传失败...(image-e856d3-1649227330310)]

    与我们给出的注释是一致的;因此在我们日常的编程中一定要注意。

    2.8. Java中继承的注意事项

    • Java中继承的注意事项
    1. Java中类只支持单继承,不支持多继承
    • 错误范例: class A extends B, C { }
    1. Java中类支持多层继承

    为了让大家更好的理解该知识点,我们给出以下的代码:

    <pre class="prettyprint hljs scala" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">public class Granddad {

    public void drink() {
        System.out.println("爷爷爱喝酒");
    }
    

    }

    public class Father extends Granddad {

    public void smoke() {
        System.out.println("爸爸爱抽烟");
    }
    

    }

    public class Mother {

    public void dance() {
        System.out.println("妈妈爱跳舞");
    }
    

    }
    public class Son extends Father {
    // 此时,Son类中就同时拥有drink方法以及smoke方法
    }</pre>

    3. 继承练习

    • 需求 :定义老师类和学生类,然后写代码测试;最后找到老师类和学生类当中的共性内容,抽取出一个父类,用继承的方式改写代码,并进行测试
    • 根据其需求我们给出以下的思路:

    ①定义老师类(姓名,年龄,教书())

    ②定义学生类(姓名,年龄,学习())

    ③定义测试类,写代码测试

    ④共性抽取父类,定义人类(姓名,年龄)

    ⑤定义老师类,继承人类,并给出自己特有方法:教书()

    ⑥定义学生类,继承人类,并给出自己特有方法:学习()

    ⑦定义测试类,写代码测试

    我们通过以上的需求以及思路实现如下的代码:

    <pre class="prettyprint hljs java" style="padding: 0.5em; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; color: rgb(68, 68, 68); border-radius: 4px; display: block; margin: 0px 0px 1.5em; font-size: 14px; line-height: 1.5em; word-break: break-all; overflow-wrap: break-word; white-space: pre; background-color: rgb(246, 246, 246); border: none; overflow-x: auto; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">package inheritExample;
    class Person {
    private String name;
    private int age;

    public Person() {
    }
    
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public int getAge() {
        return age;
    }
    
    public void setAge(int age) {
        this.age = age;
    }
    

    }

    class Teacher extends Person {

    public Teacher() {}
    
    public Teacher(String name,int age) {
        super(name,age);
    }
    
    public void teach() {
        System.out.println("用心去撰写每一篇博客");
    }
    

    }

    class Student extends Person{
    public Student() {}

    public Student(String name, int age) {
        super(name,age);
    }
    
    public void study(){
        System.out.println("读者阅读");
    }
    

    }

    class PersonDemo {
    public static void main(String[] args){
    //创建老师类对象并进行测试
    Teacher t = new Teacher();
    t.setName("一计之长");
    t.setAge(28);
    System.out.println(t.getName() + "," + t.getAge());
    t.teach();

     // 创建学生类对象测试
        Student s1 = new Student("readA",18);
        System.out.println(s1.getName() + "," + s1.getAge());
        s1.study();
    
        Student s2 = new Student("readB",23);
        System.out.println(s2.getName() + "," + s2.getAge());
        s2.study();
    }
    

    }</pre>

    具体执行的结果如下:

    [图片上传失败...(image-a2aecd-1649227330310)]

    总结

    本文主要是详细给大家介绍了java中的三大特性中的继承,包括继承的概念、继承的实现以及优缺点,另外还介绍了继承中成员访问特点,接着给大家介绍了 super和this这两个关键字的相关知识, 最后给大家介绍了方法的重写以及在使用继承中需要注意的事项。与此同时,我们在介绍相关知识的同时给出了相应的案例帮助大家更好的理解相应的知识点。

    相关文章

      网友评论

          本文标题:Java三大特性之继承

          本文链接:https://www.haomeiwen.com/subject/srirsrtx.html