記錄馬路上經過的來往車輛

該程序的功能爲記錄馬路上來往車輛的速度和名字信息。

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;
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章