定义一个Vehicle类的子类轿车类Car,要求如下:
2.2.1 轿车有自己的属性载人数loader(int 类型)。
2.2.2 提供该类初始化属性的构造方法。
2.2.3 重新定义run(),用打印语句描述轿车奔跑的功能。
2.2.4 在main方法中创建一个品牌为“Honda”、颜色为“red”,载人数为2人的轿车。
public abstract class Vehicle {
private String brand;
private String color;
private double speed;
public String getBrand() {
return brand;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public double getSpeed() {
return speed;
}
public void setSpeed(double speed) {
this.speed = speed;
}
public Vehicle(String brand, String color) {
super();
this.brand = brand;
this.color = color;
// this.speed = speed;
}
public abstract void run();
// public void run() {
// System.out.println(getColor() + "的" + getBrand() + "的速度是:" + getSpeed());
// }
}
public class Car extends Vehicle{
private int loader;
public Car(int loader,String brand,String color) {
super(brand,color);
this.loader = loader;
}
public int getLoader() {
return loader;
}
public void setLoader(int loader) {
this.loader = loader;
}
public void run() {
System.out.println(getColor() + "的" + getBrand() + "的载人数是:" + getLoader());
}
}
public class VehicleTest {
public static void main(String[] args) {
// Vehicle v = new Vehicle("benz", "black", 100);
// v.run();
Vehicle car = new Car(2,"Honda","red");
car.run();
}
}
public abstract class Shape {
private int area;
private int per;
private String color;
public Shape() {
}
public Shape(String color) {
this.color = color;
}
/*
public Shape(int area, int per, String color) {
super();
this.area = area;
this.per = per;
this.color = color;
}
*/
/* public int getArea() {
return area;
}
public void setArea(int area) {
this.area = area;
}
public int getPer() {
return per;
}
public void setPer(int per) {
this.per = per;
}*/
public String getColor() {
return color;
}
/*
public void setColor(String color) {
this.color = color;
}*/
/* public void getColor() {
this.color = color;
System.out.println();
}*/
public abstract void getArea();
public abstract void getPer();
public abstract void showAll();
}
public class Rectangle extends Shape{
/*public Rectangle(int area, int per, String color) {
super(area, per, color);
}*/
private int Width;
private int height;
@Override
public void getArea() {
System.out.println("Rectangle矩形面积为:"+Width*height);
}
@Override
public void getPer() {
System.out.println("Rectangle矩形周长为:"+2*(height+Width));
}
@Override
public void showAll() {
System.out.println("矩形长为:"+height+",矩形的宽为:"+Width);
this.getArea();
this.getPer();
getColor();
}
public Rectangle(int width, int height,String color) {
super(color);
this.Width = width;
this.height = height;
}
}
public class Circle extends Shape{
private double radius;
@Override
public void getArea() {
System.out.println("Circle的面积为:"+3.14*(radius*radius));
}
@Override
public void getPer() {
System.out.println("Circle的周长为:"+3.14*(2*(radius)));
}
@Override
public void showAll() {
System.out.println("Circle的半径为:"+this.radius);
this.getArea();
this.getPer();
getColor();
}
Circle(double radius,String color) {
super(color);
this.radius = radius;
}
Circle() {
}
}
public class Test {
public static void main(String[] args) {
Shape a = new Rectangle(5,10,"黄");//a是矩形
Shape b = new Circle(5,"蓝");//b是圆
a.showAll();
b.showAll();
}
}