需求:

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练习——抽象类的更多相关文章

  1. JAVA:抽象类VS接口

    JAVA中抽象类和接口的区别比较,以及它们各自的用途. 1.JAVA抽象类: 抽象类除了不能实例化以外,跟普通类没有任何区别.在<JAVA编程思想>一书中,将抽象类定义为“包含抽象方法的类 ...

  2. 转:二十一、详细解析Java中抽象类和接口的区别

    转:二十一.详细解析Java中抽象类和接口的区别 http://blog.csdn.net/liujun13579/article/details/7737670 在Java语言中, abstract ...

  3. 关于JAVA中抽象类和接口的区别辨析

    今天主要整理一下新学习的有关于Java中抽象类和接口的相关知识和个人理解. 1 抽象类 用来描述事物的一般状态和行为,然后在其子类中去实现这些状态和行为.也就是说,抽象类中的方法,需要在子类中进行重写 ...

  4. [ Java学习基础 ] Java的抽象类与接口

    一.抽象类 1. 抽象类 Java语言提供了两种类:一种是具体类:另一种是抽象子类. 2. 抽象类概念: 在面向对象的概念中,所有的对象都是通过类来描绘的,但是反过来,并不是所有的类都是用来描绘对象的 ...

  5. java中抽象类的定义和使用

    java虽然比较简单,但是细节的知识点还是很多的,现在,介绍一下抽象类的定义和实现基础. 指的是在类中定义方法,而不去实现它,而在它的子类中去具体实现,继承抽象类的子类必须实现父类的抽象方法,除非子类 ...

  6. Java基础-抽象类和接口

    抽象类与接口是Java语言中对抽象概念进行定义的两种机制,正是由于他们的存在才赋予java强大的面向对象的能力.他们两者之间对抽象概念的支持有很大的相似,甚至可以互换,但是也有区别. 抽象定义: 抽象 ...

  7. JAVA的抽象类和接口

    抽象类 在面向对象的概念中,所有的对象都是通过类来描述的,但是反过来,并不是所有的类都是用来描述对象的,如果一个类中没有包含足够的信息来描绘一个具体的对象,这样的类就是抽象类. 抽象类除了不能实例化对 ...

  8. Java中抽象类也能实例化

    在Java中抽象类真的不能实例化么? 在学习的过程中,发现了一个问题,抽象类在没有实现所有的抽象方法前是不可以通过new来构建该对象的,但是抽象方法却是可以有自己的构造方法的.这样就把我搞糊涂了,既然 ...

  9. Java中抽象类也能实例化.RP

    在Java中抽象类真的不能实例化么? 在学习的过程中,发现了一个问题,抽象类在没有实现所有的抽象方法前是不可以通过new来构建该对象的,但是抽象方法却是可以有自己的构造方法的.这样就把我搞糊涂了,既然 ...

  10. Java 中抽象类与接口的区别

    TypeScript 中的接口,有点类似抽象类的概念.Java 中抽象类属于包含属性与抽象行为,而接口通常只是抽象行为.抽象类可以实现模板模式. 参考 https://www.cnblogs.com/ ...

随机推荐

  1. poj3757 Training little cats

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11496   Accepted: 2815 Description Face ...

  2. Codeforces Round #651 (Div. 2) A Maximum GCD、B GCD Compression、C Number Game、D Odd-Even Subsequence

    A. Maximum GCD 题意: t组输入,然后输入一个n,让你在区间[1,n]之间找出来两个不相等的数a,b.求出来gcd(a,b)(也就是a,b最大公约数).让你求出来最大的gcd(a,b)是 ...

  3. Windows环境下Robot Framework 下载及安装流程

    1.安装包下载 注意安装包统一64位或32位 1)python-2.7.16.amd64.msi 2)robotframework-3.0.2.tar.gz 3)robotframework-ride ...

  4. 【转】Redis数据备份和重启恢复

    一.对Redis持久化的探讨与理解 目前Redis持久化的方式有两种: RDB 和 AOF 首先,我们应该明确持久化的数据有什么用,答案是用于重启后的数据恢复.Redis是一个内存数据库,无论是RDB ...

  5. 修改jpg的图片大小

    using System.Drawing.Imaging; public void ResizePic(string oldFilePath, int thumbnailImageWidth, int ...

  6. Linux 驱动框架---模块参数

    Linux 模块的参数 通过在内核模块中定义模块参数从而可以在安装模块时通过insmod module_name paramname=param形式给模块传递参数.如果安装模块是传参数则将使用模块内定 ...

  7. adjust All In One

    adjust All In One 调整 https://www.adjust.com/ Maximize the impact of your mobile marketing Adjust is ...

  8. Vue 3.x & v-model

    Vue 3.x & v-model https://v3.vuejs.org/guide/migration/v-model.html#overview BREAKING: When used ...

  9. shit LeetCode interview Question

    shit LeetCode interview Question https://leetcode.com/interview/1/ 有点晕,啥意思,没太明白,到底是要按什么排序呀? 去掉 标识符 不 ...

  10. Principle for iOS App Animation Design

    Principle for iOS App Animation Design Animate Your Ideas, Design Better Apps https://principleforma ...