Java练习——抽象类
需求:
2辆宝马,1辆别克商务舱,1辆金龙(34)座,租5天共多少租金。
| 轿车 | 客车(金杯、金龙) | ||||
| 车型 | 别克商务舱GL8 | 宝马550i | 别克林荫大道 | <=16座 | >16座 |
| 日租费(元/天) | 600 | 500 | 300 | 800 | 1500 |
新购置了卡车,根据吨位,租金每吨每天50,对系统进行扩展,计算汽车租赁的总租金
实现思路:
类:MotoVehicle(机动车,抽象类),Car,Bus
类中的属性:
MotoVehicle{No(车牌号),Brand(品牌),Color(颜色),Mileage(里程)}
Car{Type(型号),TypeRent(租车的数量*车型日租费)}
Bus{SeatCount(乘载数量),TypeRent(租车的数量*车型日租费)}
实现代码:
MotoVehicle.java
package com.sbx.ex5; /**
* Created by BlueLover
* Name:苏半夏
* Date:2021/2/7
* Time:16:36
*/
public abstract class MotoVehicle {
private int No;//数量
private String Brand;//品牌
private String Color;//颜色
private String Mileage;//里程
public abstract int CalcRent(int days); public int getNo() {
return No;
} public void setNo(int no) {
No = no;
} public String getBrand() {
return Brand;
} public void setBrand(String brand) {
Brand = brand;
} public String getColor() {
return Color;
} public void setColor(String color) {
Color = color;
} public String getMileage() {
return Mileage;
} public void setMileage(String mileage) {
Mileage = mileage;
}
}
Car.java
package com.sbx.ex5; /**
* Created by BlueLover
* Name:苏半夏
* Date:2021/2/7
* Time:16:38
*/
public final class Car extends MotoVehicle{
private String Type;//型号
private int TypeRent;//租车的数量*车型日租费 public int getTypeRent() {
return TypeRent;
} public void setTypeRent(int typeRent) {
TypeRent = typeRent;
} public String getType() {
return Type;
} public void setType(String type) {
Type = type;
} @Override
public int CalcRent(int days) {
this.BrandRent(this);
return this.TypeRent*days;
} //根据车的型号和数量
public Car(String type,int no) {
Type = type;
super.setNo(no); }
//定义一个方法用于计算租车的数量*车型日租费
public void BrandRent(Car car){
int DailyRental;
switch (car.getType()){
case "别克商务舱GL8":
DailyRental = 600;
break;
case "宝马550i":
DailyRental = 500;
break;
case "别克林荫大道":
DailyRental = 300;
break;
default:
throw new IllegalStateException("Unexpected value: " + car.getType());
}
car.setTypeRent(super.getNo()*DailyRental);
}
}
Bus.java
package com.sbx.ex5; /**
* Created by BlueLover
* Name:苏半夏
* Date:2021/2/7
* Time:16:39
*/
public final class Bus extends MotoVehicle{
private int SeatCount; //乘载数量
private int TypeRent;//租车的数量*车型日租费 @Override
public int CalcRent(int days) {
this.BrandRent(this);
return this.TypeRent*days;
} public int getSeatCount() {
return SeatCount;
} public void setSeatCount(int seatCount) {
SeatCount = seatCount;
} public int getTypeRent() {
return TypeRent;
} public void setTypeRent(int typeRent) {
TypeRent = typeRent;
} public Bus(int seatCount,int no) {
SeatCount = seatCount;
super.setNo(no);
}
//定义一个方法用于计算租车的数量*车型日租费
public void BrandRent(Bus bus){
int DailyRental;
if (bus.getSeatCount()>16){
DailyRental = 1500;
}else{
DailyRental = 800;
}
bus.setTypeRent(super.getNo()*DailyRental);
}
}
Truck.java
package com.sbx.ex5; /**
* Created by BlueLover
* Name:苏半夏
* Date:2021/2/7
* Time:18:53
*/
public class Truck extends MotoVehicle{
private final int ton;//吨
@Override
public int CalcRent(int days) {
return ton*50*days*super.getNo();
} public Truck(int ton,int no) {
this.ton = ton;
super.setNo(no);
}
}
Test.java
package com.sbx.ex5; /**
* Created by BlueLover
* Name:苏半夏
* Date:2021/2/7
* Time:18:37
*/
public class Test {
public static void main(String[] args) {
/*
* 2辆宝马
1辆别克商务舱
1辆金龙(34)座
租5天共多少租金
*/
//2辆宝马的租金
Car car1 = new Car("宝马550i",2);
car1.CalcRent(5);
//1辆别克商务舱的租金
Car car2 = new Car("别克商务舱GL8",1);
car2.CalcRent(5);
//1辆金龙(34)座的租金
Bus bus = new Bus(34,1);
bus.CalcRent(5);
int sum = car1.CalcRent(5)+car2.CalcRent(5)+bus.CalcRent(5);
System.out.println("2辆宝马,1辆别克商务舱,1辆金龙(34)座,租5天共需要租金"+sum+"元"); Truck truck = new Truck(100,2);
System.out.println("2辆卡车各100吨,租5天共需租金"+truck.CalcRent(5)+"元");
}
}
Java练习——抽象类的更多相关文章
- JAVA:抽象类VS接口
JAVA中抽象类和接口的区别比较,以及它们各自的用途. 1.JAVA抽象类: 抽象类除了不能实例化以外,跟普通类没有任何区别.在<JAVA编程思想>一书中,将抽象类定义为“包含抽象方法的类 ...
- 转:二十一、详细解析Java中抽象类和接口的区别
转:二十一.详细解析Java中抽象类和接口的区别 http://blog.csdn.net/liujun13579/article/details/7737670 在Java语言中, abstract ...
- 关于JAVA中抽象类和接口的区别辨析
今天主要整理一下新学习的有关于Java中抽象类和接口的相关知识和个人理解. 1 抽象类 用来描述事物的一般状态和行为,然后在其子类中去实现这些状态和行为.也就是说,抽象类中的方法,需要在子类中进行重写 ...
- [ Java学习基础 ] Java的抽象类与接口
一.抽象类 1. 抽象类 Java语言提供了两种类:一种是具体类:另一种是抽象子类. 2. 抽象类概念: 在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的 ...
- java中抽象类的定义和使用
java虽然比较简单,但是细节的知识点还是很多的,现在,介绍一下抽象类的定义和实现基础. 指的是在类中定义方法,而不去实现它,而在它的子类中去具体实现,继承抽象类的子类必须实现父类的抽象方法,除非子类 ...
- Java基础-抽象类和接口
抽象类与接口是Java语言中对抽象概念进行定义的两种机制,正是由于他们的存在才赋予java强大的面向对象的能力.他们两者之间对抽象概念的支持有很大的相似,甚至可以互换,但是也有区别. 抽象定义: 抽象 ...
- JAVA的抽象类和接口
抽象类 在面向对象的概念中,所有的对象都是通过类来描述的,但是反过来,并不是所有的类都是用来描述对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类. 抽象类除了不能实例化对 ...
- Java中抽象类也能实例化
在Java中抽象类真的不能实例化么? 在学习的过程中,发现了一个问题,抽象类在没有实现所有的抽象方法前是不可以通过new来构建该对象的,但是抽象方法却是可以有自己的构造方法的.这样就把我搞糊涂了,既然 ...
- Java中抽象类也能实例化.RP
在Java中抽象类真的不能实例化么? 在学习的过程中,发现了一个问题,抽象类在没有实现所有的抽象方法前是不可以通过new来构建该对象的,但是抽象方法却是可以有自己的构造方法的.这样就把我搞糊涂了,既然 ...
- Java 中抽象类与接口的区别
TypeScript 中的接口,有点类似抽象类的概念.Java 中抽象类属于包含属性与抽象行为,而接口通常只是抽象行为.抽象类可以实现模板模式. 参考 https://www.cnblogs.com/ ...
随机推荐
- Codeforces Global Round 8 B. Codeforces Subsequences(构造)
题目链接:https://codeforces.com/contest/1368/problem/B 题意 构造最短的至少含有 $k$ 个 $codeforces$ 子序列的字符串. 题解 如下表: ...
- Educational Codeforces Round 88 (Rated for Div. 2) C. Mixing Water(数学/二分)
题目链接:https://codeforces.com/contest/1359/problem/C 题意 热水温度为 $h$,冷水温度为 $c\ (c < h)$,依次轮流取等杯的热冷水,问二 ...
- Educational Codeforces Round 89 (Rated for Div. 2) A. Shovels and Swords (贪心)
题意:你有\(a\)个树枝和\(b\)个钻石,\(2\)个树枝和\(1\)个钻石能造一个铁铲,\(1\)个树枝和\(2\)个钻石能造一把剑,问最多能造多少铲子和剑. 题解:如果\(a\le b\),若 ...
- 国产网络测试仪MiniSMB - 如何3秒内创建出16,000条UDP/TCP端口号递增流
国产网络测试仪MiniSMB(www.minismb.com)是复刻smartbits的IP网络性能测试工具,是一款专门用于测试智能路由器,网络交换机的性能和稳定性的软硬件相结合的工具.可以通过此以太 ...
- Redis 穿透 & 击穿 & 雪崩
原文:https://www.cnblogs.com/binghe001/p/13661381.html 缓存穿透 如果在请求数据时,在缓存层和数据库层都没有找到符合条件的数据,也就是说,在缓存层和数 ...
- PyCharm 中文 字符 python 报错 的 完美 解决方案!
PyCharm 中文 字符 python 报错 的 完美 解决方案! #_*_ coding:utf-8_*_ https://www.python.org/dev/peps/pep-0263/ 到p ...
- browsers simulator
browsers simulator https://developers.google.com/web/tools/chrome-devtools/device-mode/testing-other ...
- js 大数计算
js 大数计算 原理 JavaScript 安全整数 是 -253-1 ~ 253-1 ,即: -9007199254740991 ~ 9007199254740991; 换句话说,整数超过这个范围就 ...
- taro render html
taro render html html = `<h1 style='color: red'>Wallace is way taller than other reporters.< ...
- taro & querySelector & refs
taro & querySelector & refs delayQuerySelector https://github.com/NervJS/taro-ui/blob/dev/sr ...