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
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 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.
在给定的代码中,我们可以看到一个抽象类Vehicle和它的两个子类Car和RaceCar。每个类都有一个名为speed()的方法,返回不同的速度值。在TestCar类的main方法中,创建了一个RaceCar对象、一个Car对象和一个Vehicle对象,并通过调用speed()方法分别获取它们的速度值,并将结果打印输出。输出结果为"150, 60, 60"。 抽象类的意义在于它提供了一种定义接口的方式,它可以包含抽象方法和具体方法。抽象方法是没有实现的方法,需要在具体的子类中实现。抽象类可以作为基类,被其他类继承,并在子类中实现抽象方法。它可以提供一些公共的方法和属性,以及规范子类的行为。通过使用抽象类,我们可以实现代码的重用和扩展性的增强。 编写建设银行接口CCB继承银联接口,并实现该接口,可以在建设银行接口CCB中定义燃气费支付的方法,并在实现该接口的类中实现该方法。接口可以为不同的类提供一种共同的协议或契约,使得这些类可以按照接口中定义的方法进行操作。 正确的说法是接口定义了一组方法的规范,而抽象类可以包含方法的实现。接口可以被多个类实现,而抽象类只能被单继承。接口中的方法默认是公共的,而抽象类中的方法可以有不同的访问修饰符。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [Java入门第67课——抽象类和接口作业](https://blog.csdn.net/houjunkang363/article/details/90726776)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值