一个汽车Auto类,然后继承重写,也算是一个记录自己学习的过程吧
package com.Weekend.Dwh;/**
*
* @AuThor Administrator
* @category 建立一个汽车Auto类,包括轮胎个数,汽车颜色,车身重量、速度等成员变量。
* 并通过不同的构造方法创建实例。至少要求:汽车能够加速,减速,停车。再定义一个小汽车类Car,继承Auto,
* 并添加空调、CD等成员变量,覆盖加速,减速的方法
*/
public class Work18 {
public static void main(String[] args) {
Car2 c = new Car2("小汽车", "蓝色", 2, 1.5, "空调", "CD");
c.showInfo();
c.speedUp();
c.speedUp();
c.speedUp();
c.speedUp();
c.move();
c.speedDown();
c.stop();
}
}
class Auto {
private String color;
private int size;
private double weight;
double speed;
public Auto(String color, int size, double weight) {
super();
this.color = color;
this.size = size;
this.weight = weight;
}
public void speedUp() {
if (speed < 200) {
System.out.println("正在加速中......");
this.speed += 10;
} else if (speed == 0) {
System.out.println("停车");
} else {
this.speed = 200;
}
}
public void speedDown() {
if (speed > 10) {
System.out.println("正在减速中......");
this.speed -= 10;
} else {
stop();
}
}
public void stop() {
this.speed = 0;
System.out.println("停车");
}
public void move() {
System.out.println(this.getSize() + "座的车,以" + this.getSpeed() + "公里/小时的速度正在行驶中");
}
public void showInfo() {
System.out.println("一辆" + this.getColor() + "汽车," + this.getSize() + "个轮胎," + "车身重量" + this.weight + "吨");
}
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
}
class Car2 extends Auto {
String name;
String airCondition;
String CD;
public Car2(String name, String color, int size, double weight, String airCondition, String CD) {
super(color, size, weight);
this.airCondition = airCondition;
this.CD = CD;
}
//重写父类的方法
@Override
public void showInfo() {
System.out.println("一辆" + this.getColor() + "汽车," + this.getSize() + "个轮胎," + "车身重量" + this.getWeight() + "吨"
+ "\t有" + this.airCondition + "\t有" + this.CD);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAirCondition() {
return airCondition;
}
public void setAirCondition(String airCondition) {
this.airCondition = airCondition;
}
public String getCD() {
return CD;
}
public void setCD(String cD) {
CD = cD;
}
}
你不会是温大的吧,咋看着这么熟悉 99999999 14788871771 发表于 2020-11-11 23:18
你不会是温大的吧,咋看着这么熟悉
我不是啊
页:
[1]