java 抽象
MotoVehicle抽象类
package text1; /*
* 抽象
*/
public abstract class MotoVehicle {
// 共同的属性
private String no;// 车牌号
private String brand;// 品牌2
private String color;
private double milage; public String getNo() {
return no;
} public void setNo(String no) {
this.no = no;
} public String getBrand() {
return brand;
} public void setBrand(String brand) {
this.brand = brand;
} public String getColor() {
return color;
} public void setColor(String color) {
this.color = color;
} public double getMilage() {
return milage;
} public void setMilage(double milage) {
this.milage = milage;
} public MotoVehicle() {// 无参构造
} // 有参构造
public MotoVehicle(String no, String brand, String color, int milage) {
this.no = no;
this.brand = brand;
this.color = color;
this.milage = milage;
} public MotoVehicle(String no, String brand) {
super();
this.no = no;
this.brand = brand;
} public abstract void display(); public abstract double calcRent(int days);
}
Bus类
package text1;
public class Bus extends MotoVehicle {
private int seatCount;
public Bus() {
super();
}
public Bus(String no, String brand, String color, int milage, int seatCount) {
super(no, brand, color, milage);
// TODO Auto-generated constructor stub
this.seatCount = seatCount;
}
public Bus(String no, String brand, int seatCount) {
super(no, brand);
// TODO Auto-generated constructor stub
this.seatCount = seatCount;
}
public int getSeatCount() {
return seatCount;
}
public void setSeatCount(int seatCount) {
this.seatCount = seatCount;
}
public void display() {
System.out.println("租车信息如下:");
System.out.println("品牌:" + this.getBrand() + ",座位数:" + this.getSeatCount() + ",颜色:" + this.getColor() + ",车牌号:"
+ this.getNo() + ",里程数:" + this.getMilage());
}
public double calcRent(int days) {
// TODO Auto-generated method stub
double money = 0;
if (seatCount <= 16) {
money = 800;
} else {
money = 1500;
}
return money * days;
}
}
Car类
package text1;
public class Car extends MotoVehicle {
//自己特有的属性
private String type;//类型
//构造方法,继承父类构造方法
public Car() {
super();
// TODO Auto-generated constructor stub
}
public Car(String no, String brand, String color, int milage,String type) {
super(no, brand, color, milage);
// TODO Auto-generated constructor stub
this.type=type;
}
public Car(String no,String brand,String type) {
super(no,brand);
// TODO Auto-generated constructor stub
this.type=type;
}
//get set方法
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
//重写显示租车信息方法
@Override
public void display() {
System.out.println("租车信息如下:");
System.out.println("品牌:"+this.getBrand()+
",类型:"+this.getType()+
",颜色:"+this.getColor()+
",车牌号:"+this.getNo()+
",里程数:"+this.getMilage());
}
//重写父类计算租金方法
@Override
public double calcRent(int days) {
// TODO Auto-generated method stub
double money=0;
if (type.equals("商务舱GL8")) {
money=600;
}else if (type.equals("550i")) {
money=500;
}else if (type.equals("林荫大道")) {
money=300;
}
return money*days;
}
}
Test
package text1;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建轿车对象
Car car=new Car("豫A8888", "别克", "银灰色", 10000, "商务舱GL8");
//调用显示信息方法
car.display();
//调用计算租金方法,结果由carPrice保存
double carPrice=car.calcRent(2);
System.out.println("总租金:"+carPrice);
System.out.println("-----------------");
//创建一个客车对象
Bus bus=new Bus("豫B3336", "金杯", "白色", 15000, 50);
bus.display();
double busPrice=bus.calcRent(3);
System.out.println("总租金:"+busPrice);
}
}
java 抽象的更多相关文章
- Java抽象工厂模式
Java抽象工厂模式 基本定义 抽象工厂模式是所有形态的工厂模式中最为抽象和最其一般性的.抽象工厂模式可以向客户端提供一个接口,使得客户端在不必指定产品的具体类型的情况下,能够创建多个产品族的产品对象 ...
- Java抽象与接口的区别
Java抽象与接口的区别 答案方式一.简单来说,1.接口是公开的,里面不能有私有的方法或变量,是用于让别人使用的,而抽象类是可以有私有方法或私有变量的, 2.另外,实现接口的一定要实现接口里定义的所有 ...
- Java 抽象工厂模式
抽象工厂模式(Abstract Factory Pattern)是工厂方法模式的进一步抽象,其英文原话"Provide an interface for creating families ...
- Java抽象接口技巧(一)
原文链接 http://blog.csdn.net/qq_35101189/article/details/70799155 在程序设计过程中,读者很可能遇到这样一种困境:设计了一个接口,但实现这个接 ...
- 设计模式(四)——Java抽象工厂模式
抽象工厂模式 1 基本介绍 1) 抽象工厂模式:定义了一个 interface 用于创建相关或有依赖关系的对象簇,而无需指明具体的类 2) 抽象工厂模式可以将简单工厂模式和工厂方法模式进行整合. 3) ...
- java抽象、接口 和final
抽象 一.抽象类:不知道是具体什么东西的类. abstract class 类名 1.抽象类不能直接new出来. 2.抽象类可以没有抽象方法. public abstract class USB { ...
- java抽象-老师的生日-逻辑思维-有趣的面试题-遁地龙卷风
(-1)写在前面 都快去北京了,硬生生的安排一场java考试,对于那些特别细节的东西我忘了吧也不觉得有什么不好,以前都记得,也都见过,只不过平时不常用连接断了,但是你死记硬背是没用的,一段时间后还是会 ...
- java——抽象
抽象类:特点:1,方法只有声明,没有实现时,该方法就是抽象方法,需要被abstract关键字修饰.抽象方法必须定义在抽象类中,该类也必须被abstract修饰2,抽象类不可以被实例化.为什么?因为调用 ...
- 【JAVA-JDT-AST】Java抽象语法树的构建、遍历及转成dot格式(附Github源码)
Background: 最近为了重现tree-based clone detection的论文:L. Jiang, G. Misherghi, Z. Su, and S. Glondu. Deckar ...
随机推荐
- mysql练习----SUM and COUNT/zh图(二)
世界国家概况 GROUP BY 和 HAVING 通过包括一个GROUP BY子句功能, SUM并将COUNT 其应用于共享值的项目组.当你指定 GROUP BY continent 结果是每个不同的 ...
- 洗礼灵魂,修炼python(73)--全栈项目实战篇(1)——【转载】前提准备之学习ubuntu
本篇是为项目实战做准备,学习Linux是必备的,不然都不好意思叫全栈对吧?下面是一位资深大神写的文章,够详细,我也不用浪费时间再写了 原文链接:Ubuntu学习——第一篇 内容: 一. Ubuntu简 ...
- Sql Server 按格式输出日期
SELECT dbo.fn_Data(getdate(),'yyyymmdd') CREATE FUNCTION [dbo].[fn_Data] (@date as datetime, @format ...
- 孟岩:通证(token)和通证经济的目的在于改善现有经济的效率性
孟岩是最早将token翻译成为通证的区块链大咖,这个翻译已经得到到了越来越人的认可.原来它叫代币,孟岩建议把它翻译成通证.以下是孟岩关于通证的注解. (孟岩,柏链道捷CEO,CSDN副总裁,区块链通证 ...
- PHP使用Apache中的ab(ApacheBench)测试网站的并发量
AB(ApacheBench) 是 Apache 自带的超文本传输协议 (HTTP) 性能测试工具. 其设计意图是描绘当前所安装的 Apache 的执行性能, 主要是显示 Apache 每秒可以处理多 ...
- 4.8Python数据处理篇之Matplotlib系列(八)---Figure的学习
目录 目录 前言 (一)figure()方法的定义 (二)figure()方法的参数 (三)figure()方法的例子 1.多窗体绘图: 2.窗口得分别率 目录 前言 今天我们来学习一下plt.fig ...
- excel数据表透视操作
虽然作为开发人员,很少用到office,但是在工作的时候,特别是做财务模块,或多或少都会用到excel处理数据,对比数据.比如说vlookup函数,数据透视表这些.vlookup函数我用得很熟练,但数 ...
- JavaScript的基本包装类型_String类型
String类型概述 String在底层字符串是以字符数组的形式保存的 var str = "Hello"; // 在底层其实就是['H','e','l','l','o'] 字符串 ...
- [2] LabelImg图片标注 与 YOLOv3 网络训练 (待补充)
LabelImg是一个图形图像注释工具. 它是用Python编写的,并使用Qt作为其图形界面. 注释以PASCAL VOC格式保存为XML文件,这是ImageNet使用的格式.Besdies,它也支持 ...
- [tool] AI视频翻译 解决英文视频字幕问题(类似youtube自动生成字幕)
1.网易见外是网易人工智能事业部旗下的AI视频翻译产品. 字幕支持手工编辑和下载 不过网易见外 只支持WEB在线操作 并且只支持单个上传操作 目前没有客户端 2.人人译视界 (IOS 安卓 PC客户端 ...