实验二
实验名称: 封装,继承与多态
实验目的: 熟悉 JAVA 面向对象的三大特性。
实验时间: (2 学时)
实验类型: 设计型
实验内容:
(1)
定义一个形状类(Shape)方法:计算周长,计算面积
子类:
矩形类(Rectangle) :额外的方法: differ() 计算长宽差
圆形类(Circle)
三角形类(Triangle)
正方形类(Square) 矩形的子类
生成几个不同的形状对象,放在一个
Shape 类型的数组里,分别求每个形状的周长和面积。
如果形状对象是一个矩形,且不是正方形,则计算长宽差。
(2)某公司的雇员分为以下若干类:
Employee:这是所有员工总的父类,属性:员工的生日月份。方法: getSalary(int
month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励
100 元。
SalariedEmployee: Employee 的子类,拿固定工资的员工。属性:月薪
HourlyEmployee: Employee 的子类,按小时拿工资的员工,每月工作超出 160 小时的部分按照 1.5 倍工资发放。属性:每小时的工资、每月工作的小时数
SalesEmployee: Employee的子 类,销售 人员,工资由 月销售额 和提成率决定 。属性: 月销售额、提 成率
BasePlusSalesEmployee: SalesEmployee 的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。属性:底薪。
写一个程序,把若干各种类型的员工放在一个
Employee 数组里,写一个函数,打印出某月每个员工的工资数额。注意:不允许非私有化属性。
(1)ShapeDemo.java:
1 abstract class Shape { 2 public abstract double calculatePerimeter(); 3 public abstract double calculateArea(); 4 } 5 6 class Rectangle extends Shape { 7 private double length; 8 private double width; 9 10 public Rectangle(double length, double width) { 11 this.length = length; 12 this.width = width; 13 } 14 15 public double getLength() { 16 return length; 17 } 18 19 public double getWidth() { 20 return width; 21 } 22 23 @Override 24 public double calculatePerimeter() { 25 return 2 * (length + width); 26 } 27 28 @Override 29 public double calculateArea() { 30 return length * width; 31 } 32 33 public double differ() { 34 return Math.abs(length - width); 35 } 36 } 37 38 class Circle extends Shape { 39 private double radius; 40 41 public Circle(double radius) { 42 this.radius = radius; 43 } 44 45 public double getRadius() { 46 return radius; 47 } 48 49 @Override 50 public double calculatePerimeter() { 51 return 2 * Math.PI * radius; 52 } 53 54 @Override 55 public double calculateArea() { 56 return Math.PI * radius * radius; 57 } 58 } 59 60 class Triangle extends Shape { 61 private double side1; 62 private double side2; 63 private double side3; 64 65 public Triangle(double side1, double side2, double side3) { 66 this.side1 = side1; 67 this.side2 = side2; 68 this.side3 = side3; 69 } 70 71 @Override 72 public double calculatePerimeter() { 73 return side1 + side2 + side3; 74 } 75 76 @Override 77 public double calculateArea() { 78 double s = calculatePerimeter() / 2; 79 return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3)); 80 } 81 } 82 83 class Square extends Rectangle { 84 public Square(double side) { 85 super(side, side); 86 } 87 } 88 89 public class ShapeDemo { 90 public static void main(String[] args) { 91 Shape[] shapes = new Shape[4]; 92 shapes[0] = new Rectangle(5, 10); 93 shapes[1] = new Circle(3); 94 shapes[2] = new Triangle(3, 4, 5); 95 shapes[3] = new Square(7); 96 97 for (int i = 0; i < shapes.length; i++) { 98 double perimeter = shapes[i].calculatePerimeter(); 99 double area = shapes[i].calculateArea(); 100 101 System.out.println("Shape " + (i + 1) + ":"); 102 System.out.println("Perimeter: " + perimeter); 103 System.out.println("Area: " + area); 104 105 if (shapes[i] instanceof Rectangle && !(shapes[i] instanceof Square)) { 106 Rectangle rectangle = (Rectangle) shapes[i]; 107 double differ = rectangle.differ(); 108 System.out.println("Differ: " + differ); 109 } 110 111 System.out.println(); 112 } 113 } 114 }
(2)EmployeeDemo.java
1 class Employee { 2 private int birthMonth; 3 4 public Employee(int birthMonth) { 5 this.birthMonth = birthMonth; 6 } 7 8 public double getSalary(int month) { 9 double salary = 0; 10 if (month == birthMonth) { 11 salary += 100; 12 } 13 return salary; 14 } 15 } 16 17 class SalariedEmployee extends Employee { 18 private double monthlySalary; 19 20 public SalariedEmployee(double monthlySalary, int birthMonth) { 21 super(birthMonth); 22 this.monthlySalary = monthlySalary; 23 } 24 25 @Override 26 public double getSalary(int month) { 27 return super.getSalary(month) + monthlySalary; 28 } 29 } 30 31 class HourlyEmployee extends Employee { 32 private double hourlyRate; 33 private int hoursWorked; 34 35 public HourlyEmployee(double hourlyRate, int hoursWorked, int birthMonth) { 36 super(birthMonth); 37 this.hourlyRate = hourlyRate; 38 this.hoursWorked = hoursWorked; 39 } 40 41 @Override 42 public double getSalary(int month) { 43 double salary = super.getSalary(month); 44 if (hoursWorked > 160) { 45 int extraHours = hoursWorked - 160; 46 double overtimeSalary = extraHours * hourlyRate * 1.5; 47 salary += overtimeSalary; 48 } else { 49 salary += hoursWorked * hourlyRate; 50 } 51 return salary; 52 } 53 } 54 55 class SalesEmployee extends Employee { 56 private double monthlySales; 57 private double commissionRate; 58 59 public SalesEmployee(double monthlySales, double commissionRate, int birthMonth) { 60 super(birthMonth); 61 this.monthlySales = monthlySales; 62 this.commissionRate = commissionRate; 63 } 64 65 @Override 66 public double getSalary(int month) { 67 return super.getSalary(month) + monthlySales * commissionRate; 68 } 69 } 70 71 class BasePlusSalesEmployee extends SalesEmployee { 72 private double baseSalary; 73 74 public BasePlusSalesEmployee(double monthlySales, double commissionRate, double baseSalary, int birthMonth) { 75 super(monthlySales, commissionRate, birthMonth); 76 this.baseSalary = baseSalary; 77 } 78 79 @Override 80 public double getSalary(int month) { 81 return super.getSalary(month) + baseSalary; 82 } 83 } 84 85 public class EmployeeDemo { 86 public static void main(String[] args) { 87 Employee[] employees = new Employee[4]; 88 employees[0] = new SalariedEmployee(5000, 3); 89 employees[1] = new HourlyEmployee(20, 180, 6); 90 employees[2] = new SalesEmployee(10000, 0.1, 9); 91 employees[3] = new BasePlusSalesEmployee(20000, 0.08, 3000, 12); 92 93 int month = 9; // 指定月份 94 95 for (int i = 0; i < employees.length; i++) { 96 double salary = employees[i].getSalary(month); 97 System.out.println("Employee " + (i + 1) + " Salary: " + salary); 98 } 99 } 100 }
在上面的程序中,我们定义了 Employee
类作为所有员工的父类,其中包含属性 birthMonth
表示员工的生日月份,并实现了 getSalary()
方法来计算工资。
然后,我们分别创建了 SalariedEmployee
、HourlyEmployee
、SalesEmployee
和 BasePlusSalesEmployee
这几个子类,它们继承自 Employee
类,并重写了父类的 getSalary()
方法,根据各自的工资计算规则计算工资并返回。
在 EmployeeDemo
类中,我们创建了一个 Employee
类型的数组 employees
,并将不同类型的员工对象放入数组中。然后使用循环遍历数组,调用每个员工的 getSalary()
方法来获取指定月份的工资数额,并打印出来。
你可以根据需要修改员工的属性和工资计算规则,并指定不同的月份来测试程序。