实验七 类的抽象类、多态及重写函数学习

实验目的:

  1. 理解类的多态性
  2. 掌握抽象类的概念
  3. 掌握函数重写
  4. 掌握toString和equals函数
  5. 掌握对象类型转换

实验内容:

  1. toString和equals函数学习
  1. 创建一个三角形类,有double型私有变量底、高,有构造器、更改器和访问器,求面积的方法
  2. 重写Object类的toString方法和equals方法,toString方法显示三角形的底、高和面积,显示方法见下图,equals方法参照教材P173或PPT编写,当底和高都相等时判断为相等。
  3. 编写程序,创建包含4个三角形对象的数组,自己设置高和底,用for each语句显示4个三角形对象,并分别显示两两三角形是否相等。

  1. 抽象类和多态学习(请认真理解抽象、多态、函数重写)
  1. 定义一个抽象类Staff,数据成员有姓名name(String),工资salary(double),雇佣日期hireday(hireday)。函数有:带参数构造函数;getSalary函数,返回salary;重写Object的toString函数,返回姓名、工资和雇用日期;重写Object的equals函数,当工资和雇用日期相同时认为两个对象相同;在重写函数前添加@Override抽象函数getType,返回String;抽象函数work,返回String
  2. 定义Employee类继承与Staff,增加用于描述身份的数据成员type(String类型),函数有:带参数构造函数,重写getType 函数,返回Type;重写work函数,返回从事的工作字符串
  3. 定义Manager类继承与Staff,增加用于描述身份的数据成员type(String类型),增加表示奖金的数据成员bonus(double);函数有:带参数构造函数,重写getType 函数,返回Type;重写work函数,返回从事的工作字符串,重写getSalary函数,返回salary+bonus
  4.  

 

public class TriangleTest {
    public static void main(String[] args) {
        Triangle[] t=new Triangle[]{new Triangle(2,3),new Triangle(3,2)
                ,new Triangle(4,5),new Triangle(4,5)};
        for (Triangle i:t)
            System.out.println(i);
        System.out.println("前两个三角形相等吗?"+t[0].equals(t[1]));
        System.out.println("后两个三角形相等吗?"+t[2].equals(t[3]));
    }
}
class  Triangle{
    private double width;
    private double height;

    public Triangle(double width, double height) {
        this.width = width;
        this.height = height;
    }

    public double getWidth() {
        return width;
    }

    public double getHeight() {
        return height;
    }



    public void setWidth(double width) {
        this.width = width;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public double setArea() {
        return width*height/2;
    }
    @Override
    public String toString(){
        return "三角形的底是"+width+", 高是"+height+"面积是 "+setArea();
    }
    @Override
    public boolean equals(Object otherObject){
        if(this==otherObject)
            return true;
        if(otherObject==null)
            return false;
        if(getClass()!=otherObject.getClass())
            return false;
        Triangle other=(Triangle)otherObject;
        return width==other.width && height==other.height;
    }

}
import java.time.LocalDate;

public class StaffTest {
    public static void main(String []args) {
        Staff[] s = new Staff[3];
        s[0]=new Employee("Alice",3000,2002,2,5,"普通雇员");
        s[1]=new Manager("Bob",5000,2000,1,5,"管理者",3000);
        s[2]=new Employee("kevin",3000,2002,2,5,"普通雇员");
        for(Staff e:s){
            System.out.println(e);
            System.out.println(e.getType());
            System.out.println(e.work());
        }
        System.out.println("两个雇员入职时间和待遇相同吗?"+s[0].equals(s[2]));
    }
}
abstract class Staff{
    private String name;
    private double salary;
    private LocalDate hireday;

    public Staff(String name, double salary, int year,int month,int day) {
        this.name = name;
        this.salary = salary;
        this.hireday = LocalDate.of(year,month,day);
    }
    abstract String getType();
    double getSalary(){
        return salary;
    };
    abstract String work();
    @Override
    public String toString(){
        return "姓名:"+name+" 工资:"+getSalary()+" 雇佣日期:"+hireday;
    }
    @Override
    public boolean equals(Object otherObject){
        if(this==otherObject)
            return true;
        if(otherObject==null)
            return false;
        if(getClass()!=otherObject.getClass())
            return false;
        Staff other=(Staff) otherObject;
        return salary==other.salary && hireday.equals(other.hireday);
    }
}
class Employee extends Staff{
    String type;
    public Employee(String name, double salary, int year, int month, int day, String type) {
        super(name, salary, year, month, day);
        this.type = type;
    }
    @Override
    public String getType() {
        return type;
    }
    String work(){
        return "从事生产线工作";
    }
}
class Manager extends Staff{
    String type;
    double bonus;

    public Manager(String name, double salary, int year, int month, int day, String type, double bonus) {
        super(name, salary, year, month, day);
        this.type = type;
        this.bonus = bonus;
    }
    @Override
    public String getType() {
        return type;
    }
    String work(){
        return "从事管理工作";
    }
    @Override
    double getSalary(){
        return super.getSalary()+bonus;
    };
}

 


版权声明:本文为weixin_43560803原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。