记录马路上经过的来往车辆

来源:互联网 发布:淘宝韩国代购有真的吗 编辑:程序博客网 时间: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;
}
}

原创粉丝点击