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/ ...
随机推荐
- HDU4467 Graph【轻重点维护】
HDU4467 Graph 题意: 给出一张染色图,\(n\)个点每个点是黑色或者白色,\(m\)条带权边,\(q\)次操作,有两种操作: 改变一个点的颜色 问所有边中两个端点的颜色为给定情况的边权和 ...
- Codeforces Round #644 (Div. 3)
比赛链接:https://codeforces.com/contest/1360 A - Minimal Square 题意 计算能包含两个 $a \times b$ 矩形的最小正方形的面积. 题解 ...
- A. Vitaly and Strings
Vitaly is a diligent student who never missed a lesson in his five years of studying in the universi ...
- Codeforces Round #582 (Div. 3) E. Two Small Strings (构造,思维,全排列)
题意:给你两个长度为\(2\)的字符串\(s\)和\(t\),你需要构造一个长度为\(3n\)的字符串,满足:含有\(n\)个\(a\),\(n\)个\(b\),\(n\)个\(c\),并且\(s\) ...
- hdu-6699 Block Breaker
题意: 就是给你一个n行m列的矩形,后面将会有q次操作,每次操作会输入x,y表示要击碎第x行第y列的石块,当击碎它之后还去判断一下周围石块是否牢固 如果一个石块的左右两边至少一个已经被击碎且上下也至少 ...
- SpringSecurity简单使用
什么是SpringSecurity? Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架.它提供了一组可以在Spring应用上下文中配置 ...
- Hexo-使用阿里iconfont图标
Hexo-使用阿里iconfont图标 因为使用hexo搭建的博客中,大家并不懂都有什么图标,fa fa-xx就懵了,不知道都有什么. 首先,fa fa-xxx中的图标可以在 图标库 中寻找. (上面 ...
- HDU 4649 Professor Tian(概率DP)题解
题意:一个表达式,n + 1个数,n个操作,每个操作Oi和数Ai+1对应,给出每个操作Oi和数Ai+1消失的概率,给出最后表达式值得期望.只有| , ^,&三个位操作 思路:显然位操作只对当前 ...
- Flow All In One
Flow All In One Flow is a static type checker for JavaScript https://github.com/facebook/flow https: ...
- ES6 Arrow Function All In One
ES6 Arrow Function All In One this const log = console.log; const arrow_func = (args) => log(`arg ...