您的位置:首页 > 编程语言 > Java开发

java--面向对象之继承2

2017-12-12 09:27 190 查看
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();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java