java--面向对象之继承2

来源:互联网 发布:lol末日人工智能 编辑:程序博客网 时间:2024/05/01 11:14
Auto:
package JC1;

public class Auto {
private String wheel;
private String color;
private int weight;
private double speed;
public Auto(String wheel, String color, int weight, double speed) {
this.wheel = wheel;
this.color = color;
this.weight = weight;
this.speed = speed;
}

public void speedUp(){
this.speed+=10;
System.out.println("白色的"+this.color+",重量为"+this.weight+"kg"+",速度为"+this.speed+"km/h行驶!");
}
public void speedDown(){
if(this.speed-10>0){
this.speed-=10;
System.out.println("白色的"+this.color+",重量为"+this.weight+"kg"+",速度为"+this.speed+"km/h减速行驶!");
}
}
public void stop(){
this.speed=0;
System.out.println("车已停止!"+this.speed+"km/h");
}




public String getWheel() {
return wheel;
}




public String getColor() {
return color;
}




public int getWeight() {
return weight;
}




public double getSpeed() {
return speed;
}

}
Car:
package JC1;


public class Car extends Auto {
private String aircondition;
private String CD;
public Car(String wheel, String color, int weight, double speed,
String aircondition, String cD) {
super(wheel, color, weight, speed);
this.aircondition = aircondition;
CD = cD;
}
//重写
public void speedUp(){
System.out.println("白色的"+super.getColor()+",重量为"+super.getWeight()+"kg"+",速度为"+super.getSpeed()+"km/h行驶!");
}
public void speedDown(){
System.out.println("白色的"+super.getColor()+",重量为"+super.getWeight()+"kg"+",速度为"+super.getSpeed()+"km/h减速行驶!");
}

}
Test:
package JC1;

import org.junit.Test;

public class jTest {
@Test
public void test1(){
Auto auto=new Auto("4", "black", 50000, 40);
Car car=new Car("4","white",52000,65, "美的", "eason");

//System.out.println(auto.getSpeed());
auto.speedUp();
auto.speedUp();
auto.speedUp();
auto.speedDown();
auto.speedDown();
auto.speedDown();
auto.speedDown();
auto.speedDown();
auto.speedDown();
auto.speedDown();
auto.stop();
}
}