Java核心技术(卷I) 8、类、多态、强制类型转换

2021-02-18  本文已影响0人  kaiker

继承已存在的类就是复用这些类的方法,而且可以怎家一些新的方法和字段,使新类能够适应新的方法和字段

1、定义子类

public class Manager extends Employee
{
   private double bonus;

   /**
    * @param name the employee's name
    * @param salary the salary
    * @param year the hire year
    * @param month the hire month
    * @param day the hire day
    */
   public Manager(String name, double salary, int year, int month, int day)
   {
      super(name, salary, year, month, day);
      bonus = 0;
   }

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

   public void setBonus(double b) // 这个setBonus就是Manager特有的,Employee对象就不能使用这个方法
   {
      bonus = b;
   }
}

2、覆盖方法

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

3、子类构造器

public Manager(String name, double salary, int year, int month, int day){
  super(name, salary, year, month, day); // 调用超类中有这些参数的构造器
  bonus = 0;
}

4、多态

https://www.runoob.com/java/java-polymorphism.html 多态的三个条件

Manager boss = new Manager("Marker", 80000, 1987, 12, 15);
var staff = new Employee[3];
staff[0] = boss;
staff[1] = new Employee("harry", 5000, 1989, 20, 1)
for (Employee e : staff)
  System.out.println(e.getName() + " " + e.getSalary());

5、final修饰符

不允许扩展的类被称为final类。
子类不可覆盖的方法被称为final方法。

6、强制类型转换

public class EmployeeTest {
    public static void main(String args[]){
        Manager boss = new Manager("Marker", 80000, 1987, 12, 15);
        Employee[] staff = new Employee[3];
        staff[0] = boss;
        staff[1] = new Employee("harry", 5000, 1989, 20, 1);
        System.out.println(staff[1] instanceof Manager);  // false
        System.out.println(staff[1] instanceof Employee);  // true
        System.out.println(staff[0] instanceof Manager);  // true
        System.out.println(staff[0] instanceof Employee);  // true
        Manager m = (Manager) staff[1]; // Exception in thread "main" java.lang.ClassCastException
        // 这种情况再用强制类型转换,因为要用manager里的setBouns
        Manager b = (Manager) staff[0]; 
        b.setBonus(199);
    }
}

class Employee
{
    // 字段
    private String name;
    private double salary;

    // 构造器
    public Employee(String n, double s, int year, int month, int day)
    {
        name = n;
        salary = s;
    }

    // 封装的实现,通过get set方法操作字段
    public String getName()
    {
        return name;
    }

    public double getSalary()
    {
        return salary;
    }

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

class Manager extends Employee
{
    private double bonus;

    /**
     * @param name the employee's name
     * @param salary the salary
     * @param year the hire year
     * @param month the hire month
     * @param day the hire day
     */
    public Manager(String name, double salary, int year, int month, int day)
    {
        super(name, salary, year, month, day);
        bonus = 0;
    }

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

    public void setBonus(double b) // 这个setBonus就是Manager特有的,Employee对象就不能使用这个方法
    {
        bonus = b;
    }
}

上一篇 下一篇

猜你喜欢

热点阅读