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

  1. Java抽象工厂模式

    Java抽象工厂模式 基本定义 抽象工厂模式是所有形态的工厂模式中最为抽象和最其一般性的.抽象工厂模式可以向客户端提供一个接口,使得客户端在不必指定产品的具体类型的情况下,能够创建多个产品族的产品对象 ...

  2. Java抽象与接口的区别

    Java抽象与接口的区别 答案方式一.简单来说,1.接口是公开的,里面不能有私有的方法或变量,是用于让别人使用的,而抽象类是可以有私有方法或私有变量的, 2.另外,实现接口的一定要实现接口里定义的所有 ...

  3. Java 抽象工厂模式

    抽象工厂模式(Abstract Factory Pattern)是工厂方法模式的进一步抽象,其英文原话"Provide an interface for creating families ...

  4. Java抽象接口技巧(一)

    原文链接 http://blog.csdn.net/qq_35101189/article/details/70799155 在程序设计过程中,读者很可能遇到这样一种困境:设计了一个接口,但实现这个接 ...

  5. 设计模式(四)——Java抽象工厂模式

    抽象工厂模式 1 基本介绍 1) 抽象工厂模式:定义了一个 interface 用于创建相关或有依赖关系的对象簇,而无需指明具体的类 2) 抽象工厂模式可以将简单工厂模式和工厂方法模式进行整合. 3) ...

  6. java抽象、接口 和final

    抽象 一.抽象类:不知道是具体什么东西的类. abstract class 类名 1.抽象类不能直接new出来. 2.抽象类可以没有抽象方法. public abstract class USB { ...

  7. java抽象-老师的生日-逻辑思维-有趣的面试题-遁地龙卷风

    (-1)写在前面 都快去北京了,硬生生的安排一场java考试,对于那些特别细节的东西我忘了吧也不觉得有什么不好,以前都记得,也都见过,只不过平时不常用连接断了,但是你死记硬背是没用的,一段时间后还是会 ...

  8. java——抽象

    抽象类:特点:1,方法只有声明,没有实现时,该方法就是抽象方法,需要被abstract关键字修饰.抽象方法必须定义在抽象类中,该类也必须被abstract修饰2,抽象类不可以被实例化.为什么?因为调用 ...

  9. 【JAVA-JDT-AST】Java抽象语法树的构建、遍历及转成dot格式(附Github源码)

    Background: 最近为了重现tree-based clone detection的论文:L. Jiang, G. Misherghi, Z. Su, and S. Glondu. Deckar ...

随机推荐

  1. C#委托(转载)

    C#委托的介绍(delegate.Action.Func.predicate) from:http://www.cnblogs.com/akwwl/p/3232679.html 委托是一个类,它定义了 ...

  2. 给html标签加上鼠标划过小手样式

    给html标签加上鼠标划过小手样式 方法:给当前标签增加样式 style="cursor:pointer;" eg:增加返回箭头样式,给箭头增加小手 <span onclic ...

  3. Python爬虫-02:HTTPS请求与响应,以及抓包工具Fiddler的使用

    目录 1. HTTP和HTTPS 1.1. HTTP的请求和响应流程:打开一个网页的过程 1.2. URL 2. 客户端HTTP请求 3. Fiddler抓包工具的使用 3.1. 工作原理 3.2. ...

  4. linux历史命令查找快捷方式

      一.回到上次操作的目录# cd -进入上次访问目录 二.历史命令搜索操作快捷键:[Ctrl + r], [Ctrl + p], [Ctrl + n] 在终端中按捉 [Ctrl] 键的同时 [r] ...

  5. Apache Spark技术实战之6 --Standalone部署模式下的临时文件清理

    问题导读 1.在Standalone部署模式下,Spark运行过程中会创建哪些临时性目录及文件? 2.在Standalone部署模式下分为几种模式? 3.在client模式和cluster模式下有什么 ...

  6. 制作CSS绚烂效果的三种属性

    animation(动画).transition(过渡).transform(变形) https://www.cnblogs.com/shenfangfang/p/5713564.html

  7. 用js显示系统当前日期

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  8. 笔记-Android中打开各种格式的文件(apk、word、excel、ppt、pdf、音视频、图片等)

    打开后缀.apk的文件.即启动安装程序. //apkFilePath 文件路径 public void installAPK(String apkFilePath) { // 创建URI Uri ur ...

  9. sqlserver 2000数据压缩解决方法

    --sqlserver 2000数据压缩解决方法. /************************************************************************* ...

  10. Python中使用class(),面向对象有什么优势 转自知乎

    https://www.zhihu.com/question/19729316 首先我是辣鸡,然后这个问题的确有点意思 首先,类是一个集合,包含了数据,操作描述的一个抽象集合 你可以首先只把类当做一个 ...