2018.12.12 java22

定义一个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();
	}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值