自動車のスピード

概要

自動車を表すCarクラスをスーパークラスとして、これを継承するRapidCarクラス、カーナビ搭載のCarWithNaviクラスを作り動作を確認する。継承の基本を学ぶ。

class Car {// スーパークラス
	private int speed;// 車のスピード

	Car(int speed) {
		System.out.println("Carクラスのオブジェクトを生成しました");
		this.setSpeed(speed);
		System.out.println(this);
	}

	Car() {// 引数なしコンストラクタ

	}

	public String toString() {
		if (speed > 0) {
			return "ただいま、スピードは[" + speed + "]です";
		} else {
			return "ただいま、停止しています";
		}
	}

	public void speedUp() {
		this.speed += 10;
		System.out.println("スピードを上げました");
	}

	public void speedDown() {
		this.speed -= 10;
		if (this.speed < 0) {
			this.speed = 0;
		}
		System.out.println("スピードを下げました");
	}

	public int getSpeed() {
		return this.speed;
	}

	public void setSpeed(int speed) {
		this.speed += speed;

	}
}

class RapidCar extends Car {
	RapidCar() {
		// super();が自動的に挿入されている。空なので何も起きない。
		System.out.println("RapidCarクラスのオブジェクトを生成しました");
		System.out.println(this);
	}

	RapidCar(int speed) {
		// super();が自動的に挿入されている。空なので何も起きない。
		System.out.println("RapidCarクラスのオブジェクトを生成しました");
		super.setSpeed(speed);
		System.out.println(this);
	}

	public void speedUp() {
		super.setSpeed(20);
		System.out.println("スピードをかなり上げました");
	}

	public void speedDown() {
		super.setSpeed(-20);
		if (super.getSpeed() < 0) {
			super.setSpeed(0);
		}
		System.out.println("スピードをかなり下げました");
	}
}

class CarNavi {
	public void navigation() {
		System.out.println("カーナビで目的地まで誘導中");
	}
}

class CarWithNavi extends Car {
	public CarNavi carnavi;

	CarWithNavi(int speed) {
		System.out.println("CarWithNaviオブジェクトを生成しました");
		super.setSpeed(speed);
		carnavi = new CarNavi();// すでにフィールドで確保しているので型宣言がいらない
		System.out.println(this);
	}

	public CarWithNavi() {
		System.out.println("CarWithNaviオブジェクトを生成しました");
		carnavi = new CarNavi();
		System.out.println(this);
	}

	public void drive() {
		System.out.println("ドライブ出発");
		carnavi.navigation();
	}
}

public class CarTest {
	public static void main(String[] args) {
		Car car =new Car(0);
		System.out.println();
		RapidCar rapidcar=new RapidCar(10);
		System.out.println();
		CarWithNavi carwithnavi=new CarWithNavi(0);
		System.out.println();
		
		System.out.println("---Car---");
		car.speedUp();
		car.speedUp();
		car.speedDown();
		System.out.println(car);
		System.out.println();
		
		System.out.println("--rapidcar--");
		rapidcar.speedUp();
		rapidcar.speedUp();
		rapidcar.speedDown();
		System.out.println(rapidcar);
		System.out.println();
		
		System.out.println("--carwithnavi--");
		carwithnavi.drive();
		carwithnavi.speedUp();
		carwithnavi.speedDown();
		carwithnavi.speedDown();
		carwithnavi.speedDown();
		System.out.println(carwithnavi);
		System.out.println();
	}
}

実行結果

Carクラスのオブジェクトを生成しました
ただいま、停止しています

RapidCarクラスのオブジェクトを生成しました
ただいま、スピードは[10]です

CarWithNaviオブジェクトを生成しました
ただいま、停止しています

      • Car---

スピードを上げました
スピードを上げました
スピードを下げました
ただいま、スピードは[10]です

    • rapidcar--

スピードをかなり上げました
スピードをかなり上げました
スピードをかなり下げました
ただいま、スピードは[30]です

    • carwithnavi--

ドライブ出発
カーナビで目的地まで誘導中
スピードを上げました
スピードを下げました
スピードを下げました
スピードを下げました
ただいま、停止しています

ダイナミック図解 自動車のしくみパーフェクト事典

ダイナミック図解 自動車のしくみパーフェクト事典