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 ...
随机推荐
- 解决Unable to load native-hadoop library for your platform
使用hadoop fs相应命令时候总是出现 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your plat ...
- Spring MVC HelloWorld入门及运行机制 (一)
完整的项目案例: springmvc.zip 介绍 SpringMVC是一款Web MVC框架. 它跟Struts框架类似,是目前主流的Web MVC框架之一. 文章通过实例来介绍SpringMVC的 ...
- css把容器级别(div...)标签固定在一个位置(在页面最右边)
.process{ border:1px solid #B7B7B8; background:#F8F8F8; width:80px; height:250px; <!--固定定位; text- ...
- 语句调优基础知识-set statistics io on
set statistics io on --清空缓存数据 dbcc dropcleanbuffers go --清空缓存计划 dbcc freeproccache go set statistics ...
- OkHttp简单使用
OkHttp是一个轻量优秀的网络框架.用于替代HttpUrlConnection和Apache HttpClient(android API23 6.0里已移除HttpClient,) 使用方法: 1 ...
- LeetCode算法题-Implement Queue Using Stacks(Java实现)
这是悦乐书的第195次更新,第201篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第57题(顺位题号是232).使用栈实现队列的以下操作. push(x) - 将元素x推 ...
- echarts修改上下左右的边距
grid: { top: '4%', left: '3%', right: '4%', ...
- C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块
C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- 6.05-btc
import requests from lxml import etree import json class BtcSpider(object): def __init__(self): self ...
- centos7+nginx负载均衡Tomcat服务
接着上一篇:www.cnblogs.com/lkun/p/8252815.html 我们在上一篇在一台centos7服务器上部署了两个nginx,接下来我们使用一个nginx实现tomcat的负载均衡 ...