面向对象学习25 - 关键字abstract

1. abstract的概念:抽象的

2. abstract可以用来修饰:类、方法

3. 具体的使用:

3.1 abstract修饰类

> 此类称为抽象类
> 抽象类不能实例化
> 抽象类中是包含构造器的,因为子类对象实例化时,需要直接或间接的调用到父类的构造器
> 抽象类中可以没有抽象方法。反之,抽象方法所在的类,一定是抽象类。

abstract修饰方法

> 此方法即为抽象方法
> 抽象方法只有方法的声明,没有方法体(甚至连大括号都没有)
> 抽象方法其功能是确定的(通过方法的声明即可确定),只是不知道该如何实现,(因为子类的情况复杂,体现为没有方法体)
> 子类必须重写父类中所有的抽象方法之后,方可实例化,否则,此子类仍是一个抽象类。

4. abstract不能使用的场景:

4.1 abstract不能修饰的结构:属性、构造器、代码块等

4.2 abstract不能与哪些关键字功能?

不能用abstract修饰私有方法、静态方法、final的方法、final的类

私有方法不能重写(封装性,类外部无法找到该方法,子类不能重写)
避免静态方法使用类进行调用(静态方法可以直接被调用,抽象方法不能被调用)
final的方法不能被重写
final修饰的类不能有子类

练习题:

(1) 定义一个Employee类,该类包含:
private成员变量name,number,birthday,其中birthday为MyDate类型的对象;
提供必要的构造器;
abstract方法earnings(),返回工资数额;
toString()方法输出对象的name,number和birthday。
(2)MyDate类包含:
private成员变量year,month,day
提供必要的构造器;
toDateString()方法返回日期对应的字符串:xxxx年xx月xx日
(3)定义SalariedEmployee类继承Employee类,实现按月计算工资的员工处理。
该类包括:private成员变量monthlySalary;
提供必要的构造器;
实现父类的抽象方法earnings(),该方法返回monthlySalary值;
toString()方法输出员工类型信息及员工的name,number,birthday。比如:SalariedEmployee[name = '',number = ,birthday = xxxx年xx月xx日]
(4)参照SalariedEmployee类定义HourlyEmployee类,实现按小时计算工资的员工处理。该类包括:
private成员变量wage和hour;
提供必要的构造器;
实现父类的抽象方法earnings(),该方法返回wage*hour值;
(5)定义PayrollSystem类,创建Employee变量数组并初始化,该数组存放各类雇员对象的引用。
利用循环遍历数组,输出各个对象的类型,name,number,birthday,以及该对象生日。
当键盘输入本月月份值时,如果本月是某个Employee对象的生日,还要输出增加工资信息。

public abstract class Employee {
    private String name;
    private int number;
    private MyDate birthday;

    public Employee() {
    }
    public Employee(String name, int number, MyDate birthday) {
        this.name = name;
        this.number = number;
        this.birthday = birthday;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public MyDate getBirthday() {
        return birthday;
    }

    public void setBirthday(MyDate birthday) {
        this.birthday = birthday;
    }
    public abstract double earnings();

    public String toString(){
        return "name = " + name + ",number = " + number +
                ", birthday = " + birthday.toDateString();
    }
}

public class MyDate {
    int year;
    int month;
    int day;

    public MyDate() {
    }
    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public String toDateString() {
        return year + "年" + month + "月" + day + "日";
    }
}

public class SalariedEmployee extends Employee{
    private double monthlySalary;

    public SalariedEmployee() {
    }

    public SalariedEmployee(String name, int number, MyDate birthday, double monthlySalary) {
        super(name, number, birthday);
        this.monthlySalary = monthlySalary;
    }

    public double earnings() {
        return monthlySalary;
    }

    public void setMonthlySalary(double monthlySalary) {
        this.monthlySalary = monthlySalary;
    }
    public String toString() {
        return "SalariedEmployee[" + super.toString() + "]";
    }
}

public class HourlyEmployee extends Employee {
    private double wage;//单位小时的工资
    private int hour;

    public HourlyEmployee(String name, int number, MyDate birthday, double wage, int hour) {
        super(name, number, birthday);
        this.wage = wage;
        this.hour = hour;
    }

    public HourlyEmployee() {
    }

    public double getWage() {
        return wage;
    }

    public void setWage(double wage) {
        this.wage = wage;
    }

    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    @Override
    public double earnings() {
        return wage * hour;
    }

    @Override
    public String toString() {
        return "HourlyEmployee[" + super.toString() + "]";
    }
}

import java.util.Scanner;

public class PayrollSystem {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Employee[] emps = new Employee[2];

        emps[0] = new SalariedEmployee("路人甲", 1001,
                new MyDate(1992, 12, 21), 18000);

        emps[1] = new HourlyEmployee("炮灰乙", 1002,
                new MyDate(1998, 7, 14), 100, 240);
        System.out.println("请输入现在的月份");
        int month = scan.nextInt();
        for(int i = 0; i < emps.length; i++) {
            System.out.println(emps[i].toString());
            System.out.println("工资为" + emps[i].earnings());

            if(month == emps[i].getBirthday().getMonth()) {
                System.out.println("生日快乐,涨薪100");
            }
        }
    }
}

posted @ 2025-06-21 20:59  谁来着?  阅读(34)  评论(0)    收藏  举报