记录马路上经过的来往车辆
来源:互联网 发布:淘宝韩国代购有真的吗 编辑:程序博客网 时间:2024/04/28 07:48
该程序的功能为记录马路上来往车辆的速度和名字信息。
package neww;
import com.javaeasy.override.Bus;
import com.javaeasy.override.CarBase;
import com.javaeasy.override.SportsCar;
public class LogCarOnAStreet {
public static void main(String[] args) {
CarBase car = null; // 创建一个CarBase类的引用
car = new CarBase("红色", "天津大发", 0); // 让car指向一个CarBase类的对象
car.speedUp(50); // 提速50
TransportRecoder.recordTransport(car); // 记录CarBase类对象的状态
car = new Bus("黄色", "大桥六线", 0, 0, 0); // car指向一个Bus类的对象
car.speedUp(60); // 提速60
TransportRecoder.recordTransport(car); // 记录Bus类对象的状态
car = new SportsCar("黄色", "Eclipse", 0, 0); // 让car指向一个SportsCar类的对象
car.speedUp(70); // 提速70
TransportRecoder.recordTransport(car); // 记录SportsCar类对象的状态
}
}
下面是该程序引入的类:
Bus类
package com.javaeasy.override;
public class Bus extends CarBase {// 表示Bus类继承自CarBase类
public int max_Passenger = 35;// 只需包含Bus特有的属性
public int current_Passenger = 0;
public int max_slow = 27;// 每次减速的最大值
public Bus() {
System.out.println("Bus类的构造方法被调用了!");
}
public void slowDown(int p_speed) {
System.out.println("Bus类中定义的slowDown(int)方法被调用了。");
if (p_speed > max_slow) {
p_speed = max_slow;
}
if (p_speed > 0) {
int tempSpeed = speed - p_speed;
if (tempSpeed >= 0) {
speed = tempSpeed;
}
}
}
public Bus(String color, String name, int speed,
int current_Passenger, int max_Passenger) {
super(color, name, speed);
this.current_Passenger = current_Passenger;
this.max_Passenger = max_Passenger;
System.out.println("Bus类有参数的构造方法被调用了!");
}
// 只需包含Bus特有的方法
public boolean getOnBus(int p_amout) {
int temp = current_Passenger + p_amout;
if (temp > max_Passenger) {
return false;
} else {
current_Passenger = temp;
return true;
}
}
}
CarBase类:
package com.javaeasy.override;
import neww.CarStatus;
public class CarBase {
public int speed;
public String name;
public String color;
public int maxSpeed = 90;
public String direction;
public CarStatus getCarStatus(){
CarStatus CarStatus=new CarStatus(speed,name);
return CarStatus;
}
public int getSpeed(){
return speed;
}
public String getName(){
return name;
}
public CarBase(String color, String direction, int speed) {
this.color = color;
this.direction=direction;
this.speed = speed;
System.out.println("CarBase类的有参数构造方法被调用了!");
}
public CarBase() {
System.out.println("CarBase类的构造方法被调用了!");
}
public void followSpeed(CarBase car) {// 与car的速度同步
String className = this.getClass().getName();
System.out.println("调用者的类型为" + className);
int newSpeed = car.speed;
if (newSpeed > speed) {
speedUp(newSpeed - this.speed);
} else {
slowDown(this.speed - newSpeed);
}
}
public void speedUp(int p_speed) {
System.out.println("CarBase类中定义的speedUp(int)方法被调用了。");
int tempSpeed = 0;
if (p_speed > 0) {
tempSpeed = speed + p_speed;
}
if (tempSpeed <= maxSpeed) {
speed = tempSpeed;
}
}
public void slowDown(int p_speed) {
System.out.println("CarBase类中定义的slowDown(int)方法被调用了。");
if (p_speed > 0) {
int tempSpeed = speed - p_speed;
if (tempSpeed >= 0) {
speed = tempSpeed;
}
}
}
}
SportsCar类
package com.javaeasy.override;
public class SportsCar extends CarBase {// 继承自CarBase类
public int nAmount = 90;// 保存氮气的剩余量
public int autoUsingN = 5;// 每次加速使用的氮气量
public SportsCar() {
System.out.println("SportsCar类的构造方法被调用了!");
}
public SportsCar(String color, String name, int speed,
int amount) {
super(color, name, speed);
nAmount = amount;
System.out.println("SportsCar类有参数的构造方法被调用了!");
}
public void speedUp(int p_speed) {// (1)SportsCar类特有的speedUp方法
System.out.println("SportsCar类中定义的speedUp(int)方法被调用了。");
if (nAmount >= autoUsingN) { // 判断剩余氮气量是否大于一次加速应该使用的氮气量
nAmount -= autoUsingN;
} else {
nAmount = 0;// 不够则剩余多少用多少
}
super.speedUp(p_speed);//使用super调用父类的方法
}
}
CarStatus类:
package neww;
public class CarStatus {
private String name;
private int speed;
public CarStatus(int speed,String name){
this.name=name;
this.speed=speed;
}
public String getName(){
return name;
}
public void setName(String name){
this.name=name;
}
public int getSpeed(){
return speed;
}
public void setSpeed(int speed){
this.speed=speed;
}
}
- 记录马路上经过的来往车辆
- 马路上的路灯
- 马路上的路灯
- 马路上的十种车,最好离远点!
- 算法题:马路上的路灯<考察重复数据剔除>
- 赛马网基本算法之--马路上的路灯
- 每天一个小题目——马路上的路灯
- 编程题——马路上的路灯
- 来往
- 来往
- 马路边上的一毛钱
- 车辆出险记录查询
- 马云的来往软着陆
- 简单记录LaTex的入门经过
- 记录一下第一次配SSM的经过
- 马路上“低头族”的交通隐患 ,小心埋头苦干的恶果
- 在马路上游泳的实施细则以及经验介绍(2008-06-02日晚)
- 一条直线上经过的格子
- Eclipse设置全局编码为UTF-8的方法
- This Handler class should be static or leaks might occur 解决办法
- 机器学习week 1
- dip dp px sp 定义
- ARM 虚拟机使用同一个公共 IP 访问公网的解决方案
- 记录马路上经过的来往车辆
- Android入门
- Android Studio真机安装报错
- Original error: Unable to launch WebDriverAgent because of xcodebuild failure: EROFS: read-only file
- LeetCode c语言-Search for a Range
- iOS波纹动画
- java反射机制入门介绍
- iOS11 调整tableview 头部空白高度关闭自动调整
- 从内核文件系统看文件读写过程