Java继承详解

时间:2021-08-10
本文章向大家介绍Java继承详解,主要包括Java继承详解使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

继承

1.类、超类和子类

1.1定义子类

有2个类,继承Employee类来定义Manager类,这里使用关键字extends 表示继承

public class Manager extends Employee{
    //add method and fields
}

关键字extends表明正在构造的新类派生于一个已存在的类

  • 已存在的类称为超类、基类或父类
  • 新类称为子类、派生类或孩子类

1.2覆盖方法

父类有些方法对子类不一定适用,就需要提供一个新的方法来覆盖父类中的那个方法

public class Manager extends Employee{
    ...
    public double getSalary(){//从写父类方法
        ...
    }
    ...
}

因为在子类重写父类的方法,方法名相同,所以在子类要调用父类的getSalary()方法,就需要用关键字super解决这个问题

super.getSalary();

准确的写法

public double getSalary(){//从写父类方法
       double baseSalary = super.getSalary();
       return baseSalary + bouns;
    }

super的用法

[(https://www.cnblogs.com/GDDG123/p/15123532.html)]:

1.3 子类构造器

在例子中(完整代码在最后),我们提供了一个构造器

public Manager(String name, double salary, int year, int month, int day) {
        super(name, salary, year, month, day);
        bonus = 0;
    }

这里的super具有不同的含义。语句

super(name, salary, year, month, day);

是“调用父类Employee中带有name,salary,year,month,day参数的构造器”的简写

1.使用super调用构造器的语句必须是子类的第一条语句

2.如果子类的构造器没有显示地调用父类的构造器,即将自动地调用父类的无参数构造器

3.如果父类没有无参数的构造器,并且在子类的构造器中又没有显示地调用父类的其他构造器,Java编译器就会报告一个错误

  • 关键字this有2个含义:1.指示隐式参数的引用,2.调用该类的其他构造器
  • 关键字super也有2个含义:1.调用父类的方法,2.调用父类的构造器

ManagerTest.java

package com.book.company;

public class ManagerTest {
    public static void main(String[] args)
    {
        // construct a Manager object
        Manager boss = new Manager("Carl Cracker", 80000, 1987, 12, 15);
        boss.setBonus(5000);

        Employee staff[] = new Employee[3];

        // fill the staff array with Manager and Employee objects

        staff[0] = boss;
        staff[1] = new Employee("Harry Hacker", 50000, 1989, 10, 1);
        staff[2] = new Employee("Tommy Tester", 40000, 1990, 3, 15);

        // print out information about all Employee objects
        for (Employee e : staff) {
            System.out.println("name=" + e.getName() + ",salary=" + e.getSalary());
        }
    }
}

Manager.java

package com.book.company;

public class Manager extends Employee{
    //add method and fields
    private double bonus;

    public Manager(String name, double salary, int year, int month, int day) {
        super(name, salary, year, month, day);
        bonus = 0;
    }

    @Override
    public double getSalary() {
        double baseSalary = super.getSalary();
        return baseSalary+bonus;

    }

    public void setBonus(double b){
        bonus = b;
    }
}

Employee.java

package com.book.company;

import java.time.LocalDate;

public class Employee {
    private String name;
    private double salary;
    private LocalDate hireDay;


    public Employee(String name, double salary, int year, int month, int day)
    {
        this.name = name;
        this.salary = salary;
        hireDay = LocalDate.of(year, month, day);
    }

    public String getName()
    {
        return name;
    }

    public double getSalary()
    {
        return salary;
    }

    public LocalDate getHireDay()
    {
        return hireDay;
    }

    public void raiseSalary(double byPercent)
    {
        double raise = salary * byPercent / 100;
        salary += raise;
    }
}

继续完善当中......

作者:GD1_1DG

-------------------------------------------

个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!

如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!

万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!

原文地址:https://www.cnblogs.com/GDDG123/p/15123905.html