Java作业 人员类之抽象类和接口

【问题描述】
6.8 设计一个人员类(Person),包含姓名,方法pay()代表人员的工资支出;方法toString()输出人员的信息。从Person类派生出教师类(Teacher)和大学生类(CollegeStudent),其中:教师的工资支出=基本工资+授课时数*30,大学生的工资支出=奖学金支出。
增加行政员工类(Administrative),行政员工的工资支出=基础工资+考核奖励(优秀5000,良好3000,合格1000)。
(1)将人员类Person定义为抽象类,pay()为抽象方法,设计程序实现多态性。
(2)定义一接口,将pay定义为其抽象方法,设计程序实现多态性。
【Java代码】
(1)抽象类

//抽象类
abstract class Person {
    protected String name;
    public Person(String name){
        this.name = name;
    }
    abstract public double pay();
    abstract public String toString();
}
class Teacher extends Person {
    private double price; //授课单价
    private double baseWage; //基本工资
    private int teachingHours; //授课时数
    public Teacher(String name, double baseWage, double price, int teachingHours){
        super(name);
        this.baseWage = baseWage;
        this.price = price;
        this.teachingHours = teachingHours;
    }
    public double pay(){
        return baseWage + teachingHours * price;
    }
    public String toString(){
        return "姓名: " + this.name + "\t职位: 教师\t工资支出: " + this.pay() + "\n";
    }
}

class CollegeStudent extends Person{
    private double scholarship; // 奖学金
    public CollegeStudent(String name, double scholarship){
        super(name);
        this.scholarship = scholarship;
    }
    public double pay(){
        return scholarship;
    }
    public String toString(){
        return "姓名: " + this.name + "\t职位: 学生\t工资支出: " + this.pay() + "\n";
    }
}

class AdministrativeStaff extends Person{
    private double baseWage1;
    private String assess;
    public AdministrativeStaff(String name, double baseWage1, String assess){
        super(name);
        this.baseWage1 = baseWage1;
        this.assess = assess;
    }
    public double pay(){
        if (assess == "优秀")
            return baseWage1 + 5000;
        else if(assess == "良好")
            return baseWage1 + 3000;
        else if(assess == "合格")
            return  baseWage1 + 1000;
        else
            return baseWage1;
    }
    public String toString(){
        return "姓名: " + this.name + "\t职位: 行政员工\t工资支出: " + this.pay() + "\n";
    }
}
public class text {
    public static void main(String[] args) {
        Person a[] = new Person[3];
        a[0] = new Teacher("张三", 1000, 2, 49);
        a[1] = new CollegeStudent("李四", 500);
        a[2] = new AdministrativeStaff("王五", 2000, "优秀");
        for(int i = 0; i < a.length; i++){
            System.out.println(a[i]);
        }
    }
}

(2)接口

public interface PersonPay {
    public double pay();
}
abstract class Person implements PersonPay {
    protected String name;
    public Person(String name){
        this.name = name;
    }
    abstract public String toString();
}
class Teacher extends Person {
    private double price; //授课单价
    private double baseWage; //基本工资
    private int teachingHours; //授课时数
    public Teacher(String name, double baseWage, double price, int teachingHours){
        super(name);
        this.baseWage = baseWage;
        this.price = price;
        this.teachingHours = teachingHours;
    }
    public double pay(){
        return baseWage + teachingHours * price;
    }
    public String toString(){
        return "姓名: " + this.name + "\t职位: 教师\t工资支出: " + this.pay() + "\n";
    }
}

class CollegeStudent extends Person{
    private double scholarship; // 奖学金
    public CollegeStudent(String name, double scholarship){
        super(name);
        this.scholarship = scholarship;
    }
    public double pay(){
        return scholarship;
    }
    public String toString(){
        return "姓名: " + this.name + "\t职位: 学生\t工资支出: " + this.pay() + "\n";
    }
}

class AdministrativeStaff extends Person{
    private double baseWage1;
    private String assess;
    public AdministrativeStaff(String name, double baseWage1, String assess){
        super(name);
        this.baseWage1 = baseWage1;
        this.assess = assess;
    }
    public double pay(){
        if (assess == "优秀")
            return baseWage1 + 5000;
        else if(assess == "良好")
            return baseWage1 + 3000;
        else if(assess == "合格")
            return  baseWage1 + 1000;
        else
            return baseWage1;
    }
    public String toString(){
        return "姓名: " + this.name + "\t职位: 行政员工\t工资支出: " + this.pay() + "\n";
    }
}

class WageText {
    public static void main(String args[]){
        Person a[] = new Person[3];
        a[0] = new Teacher("张三", 1000, 2, 49);
        a[1] = new CollegeStudent("李四", 500);
        a[2] = new AdministrativeStaff("王五", 2000, "优秀");
        for(int i = 0; i < a.length; i++){
            System.out.println(a[i]);
        }
    }
}

【测试结果】
在这里插入图片描述

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Write a class called Person with the following attributes: title (Mr., Mrs., Ms., etc.) first name last name nickname age in years sex (boolean - true/false to indicated either male or female) Write a constructor that takes no parameters and performs no initializations. Write a constructor that takes a parameter for each of the attributes listed above and sets them within the objects by calling the setter methods listed below. The Person class should have a setter method and a getter method with public access for each attribute. In the setter methods, get rid of any leading or trailing spaces (String trim() method). For a Person with the following attributes: title = "Mr." first name = "Michael" last name = "Zheng" nickname = "Mike" age = 22 sex = true (true is male, false is female) The Person class should have the following public access methods that return Strings as follows: standardName() concatenation of the first and last names (i.e., "Michael Zheng") formalName() concatenation of the title, first name, lastname (i.e., "Mr. Michael Zheng") casualName() return the nickname if it is not null, otherwise return the first name (i.e., "Mike") Be realistic when generating names. If a particular attribute does not exist for a given person, don't try to concatenate it. If necessary, add appropriate spacing and punctuation, but do not leave any leading or trailing spaces in the String that is returned. MakePerson Write a class called MakePerson with a main() method that instantiates 2 Person objects. Initialize the attributes of one of the Person objects by supplying parameters to it's constructor. Instantiate the other Person object with the default constructor (that does not accept any parameters), then set it's attributes via the appropriate setter methods. For each of the Person objects, execute and print (System.out.println()) the results of all of the getter methods and of the standardName(), formalName(), and casualName() methods.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值