定义租约类Lease,定义一个print方法输出类所有属性,定义一 个计算租金的抽象方法。 定义年租约类AnnualLease,继承于租约类Lease,可以存储起 始日期、终止日期;并能计算租金;重写print方法。 定义日租约类DailyLease,继承于租约类Lease,可以根据租约 天数计算租金;重写print方法。 建立一个年租约对象c1(船台宽度为10),建立一个日租约对象 c2(船台宽度为14,租约期限为15天),输出c1、c2的船台宽 度、租约期限及租金。 java
时间: 2024-02-19 14:58:08 浏览: 25
以下是Java代码实现:
```
abstract class Lease {
protected double width; // 船台宽度
public Lease(double width) {
this.width = width;
}
public void print() {
System.out.println("船台宽度:" + width);
}
public abstract double calculateRent();
}
class AnnualLease extends Lease {
private String startDate;
private String endDate;
public AnnualLease(double width, String startDate, String endDate) {
super(width);
this.startDate = startDate;
this.endDate = endDate;
}
@Override
public void print() {
super.print();
System.out.println("起始日期:" + startDate);
System.out.println("终止日期:" + endDate);
System.out.println("年租金:" + calculateRent());
}
@Override
public double calculateRent() {
// 计算年租金的具体实现
return 1000 * width;
}
}
class DailyLease extends Lease {
private int days; // 租约天数
public DailyLease(double width, int days) {
super(width);
this.days = days;
}
@Override
public void print() {
super.print();
System.out.println("租约天数:" + days);
System.out.println("日租金:" + calculateRent());
}
@Override
public double calculateRent() {
// 计算日租金的具体实现
return 50 * width * days;
}
}
public class Main {
public static void main(String[] args) {
AnnualLease c1 = new AnnualLease(10, "2022-01-01", "2022-12-31");
DailyLease c2 = new DailyLease(14, 15);
c1.print();
c2.print();
}
}
```
输出结果:
```
船台宽度:10.0
起始日期:2022-01-01
终止日期:2022-12-31
年租金:10000.0
船台宽度:14.0
租约天数:15
日租金:10500.0
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)