抽象、接口、以及工资系统

  1. static 修饰的属性,相较于实例变量,有哪些特别之处(>=3点)

    随着类的加载而加载;早于对象的创建,只要权限通过,可以通过。 对象static.属性的方式进行调用;存在于方法区的静态域。

  2. final 可以用来修饰哪些结构,分别表示什么意思?

    final 可以用来修饰的结构:类、方法、变量

    final :用来修饰方法:表明此方法不可以被重写

    final:用来修饰一个类:此类不能被继承

    final:用来修饰变量,表示这个变量是常量

  3. 类的属性赋值的位置有哪些?先后顺序为何?

    位置:

    1默认初始化。2.显示初始化。3.构造器中初始化。4.有了对象以后,可以通过“对象.属性”或“对象.方法”的方式,进行赋值。5.在代码块中赋值.

    执行的先后顺序 : 1 - 2/5 -3 - 4

  4. abstract 能修饰哪些结构? 修饰以后,有什么特点?

    abstract(抽象)修饰符,可以修饰类和方法 1,abstract修饰类,会使这个类成为一个抽象类,这个类将不能生成对象实例,但可以做为对象变量声明的类型,也就是编译时类型,抽象类就像当于一类的半成品,需要子类继承并覆盖其中的抽象方法。

    2,abstract修饰方法,会使这个方法变成抽象方法,也就是只有声明(定义)而没有实现,实现部分以";"代替。需要子类继承实现(覆盖)。

  5. 接口是否能继承接口? 抽象类是否能实现(implements)接口? 抽象类是否能继承非抽象的类?

    接口能继承接口,抽象类能实现接口, 抽象类是可以继承实体类,但前提是实体类必须有明确的构造函数。

  6. 抽象类和接口有哪些共同点和区别?

    共同点:不能实例化,都可以被继承

    不同点:抽象类:有构造器。 接口:不能声明构造器;类与类只能单继承,接口与接口能多继承

使用抽象类,来构建一个工资系统

题目:编写工资系统,实现不同类型员工(多态)的按月发放工资。如果当月出现某个
Employee对象的生日,则将该雇员的工资增加100元。
实验说明:
(1)定义一个Employee类,该类包含:
    private成员变量name,number,birthday,其中birthday 为MyDate类的对象;
    abstract方法earnings();
    toString()方法输出对象的name,number和birthday。
(2)MyDate类包含:
    private成员变量year,monxth,day ;
    toDateString()方法返回日期对应的字符串:xxxx年xx月xx日
(3)定义SalariedEmployee类继承Employee类,实现按月计算工资的员工处
理。该类包括:private成员变量monthlySalary;
    实现父类的抽象方法earnings(),该方法返回monthlySalary值;toString()方法输
    出员工类型信息及员工的name,number,birthday。
(4)参照SalariedEmployee类定义HourlyEmployee类,实现按小时计算工资的员工处理。该类包括:
    private成员变量wage和hour;
    实现父类的抽象方法earnings(),该方法返回wage*hour值;
    toString()方法输出员工类型信息及员工的name,number,birthday。
(5)定义PayrollSystem类,创建Employee变量数组并初始化,该数组存放各类雇员对象的引用。利用循环结构遍历数组元素,输出各个对象的类型,name,number,birthday,以及该对象生日。当键盘输入本月月份值时,如果本月是某个Employee对象的生日,还要输出增加工资信息。  

分析:

1.先创建好对应的类

2.  因为birthday是以MyDate为类型,所以在后面要输出birthdat的时候要注意对应的类型

3.因为SalariedEmployee类,HourlyEmployee类都继承Employee类,而Employee类为抽象类,所以他们两个都要重写抽象类中的抽象方法。

4.又因为要求使用toString方法,而Employee继承了Object中的toString方法,所以重写的时候要注意返回值

代码实现:

Employee类:

public abstract class Employee{
    //设置成员变量
    private String name;
    private int number;
    private MyDate birthday;

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

    public String getName() {
        return name;
    }

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

    public int getNumber() {
        return number;
    }

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

    public MyDate getBirthday() {
        return birthday;
    }

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

    public abstract int earnings();

    @Override
    public String toString() {
        return "姓名为:"+name+",编号为:"+number+",生日为:"+birthday.toDateString();
    }
}

MyDate类:

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

    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+"日";
    }
}

SalariedEmployee类:

public class SalariedEmployee extends Employee{
    private int monthlySalary;

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

    @Override
    public int earnings() {
        return monthlySalary;
    }

    @Override
    public String toString() {
        return "姓名为:"+super.getName()+",编号为:"+super.getNumber()+",生日为:"+super.getBirthday().toDateString()+"员工的工资为:"+monthlySalary;
    }
}

HourlyEmployee类:

public class HourlyEmployee extends Employee{
    private int wage;
    private int hour;

    //构造函数
    public HourlyEmployee(String name, int number, MyDate birthday, int wage, int hour) {
        super(name, number, birthday);
        this.wage = wage;
        this.hour = hour;
    }

    public int getWage() {
        return wage;
    }

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

    public int getHour() {
        return hour;
    }

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

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

    @Override
    public String toString() {
        return "姓名为:"+super.getName()+",编号为:"+super.getNumber()+",生日为:"+super.getBirthday().toDateString()+"小时工的工资为:"+earnings();
    }
}

PayrollSystem类:

import java.util.Scanner;

public class PayrollSystem {
    public static void main(String[] args) {
        Employee[] employees = new Employee[5];
        employees[0] = new SalariedEmployee("张三", 9001, new MyDate(2000, 5, 30), 2500);
        employees[1] = new SalariedEmployee("李四", 9002, new MyDate(2001, 12, 30), 3000);
        employees[2] = new SalariedEmployee("王五", 9003, new MyDate(2000, 8, 30), 2900);
        employees[3] = new HourlyEmployee("赵六", 9004, new MyDate(200, 6, 7), 98, 100);


        Scanner sc = new Scanner(System.in);
        int mon = sc.nextInt();
        for (int x = 0; x < employees.length; x++) {
            if (employees[x] != null) {
                if (mon == employees[x].getBirthday().getMonth()) {
                    String s = employees[x].toString();
                    int nowsalary = employees[x].earnings() + 100;
                    System.out.println(s + ",并且因为本月有他的生日,工资加100,现有工资为" + nowsalary);
                    System.out.println("------------------");
                } else {
                    String s = employees[x].toString();
                    System.out.println(s);
                    System.out.println("------------------");
                }
            }
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值