![]()
package com.homework013;
public class Vehicle {
protected String brand; // 品牌
protected String color; // 颜色
protected double speed; // 速度
public Vehicle(String brand, String color) {
super();
this.brand = brand;
this.color = color;
this.speed = 0;
}
public Vehicle() {
super();
}
public void run() {
System.out.println(color + "的" + brand + "速度" + speed);
}
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 String getBrand() {
return brand;
}
}
package com.homework013;
public class Car extends Vehicle {
private int loader;
public Car(String brand, String color, int loader) {
super(brand, color);
this.loader = loader;
}
public Car() {
super();
}
public int getLoader() {
return loader;
}
public void setLoader(int loader) {
this.loader = loader;
}
@Override
public void run() {
System.out.println(color + "的" + brand + "载" + loader + "人" + "速度"
+ speed);
}
}
package com.homework013;
public class Test {
public static void main(String[] args) {
Vehicle v = new Vehicle("benz", "black");
v.run();
v.setSpeed(20);
v.run();
System.out.println("///////////");
Car c = new Car("Honda", "rea", 2);
c.run();
c.setSpeed(45);
c.run();
}
}
![]()
![]()
package com.homework014;
public abstract class Shape {
protected double area; //面积
protected double per; //周长
protected String color; //颜色
public abstract double getArea();
public abstract double getPer();
public abstract void showAll();
public String getColor() {
return color;
}
public Shape() {
super();
}
public Shape(String color) {
super();
this.color = color;
}
}
package com.homework014;
public class Rectangle extends Shape{
private double whidth;
private double height;
@Override
public double getArea() {
return area = whidth * height;
}
@Override
public double getPer() {
return per = 2*(whidth + height);
}
@Override
public void showAll(){
System.out.println("whidth=" + whidth);
System.out.println("height=" + height +" area=" + getArea() + " per=" + getPer() + " color=" + getColor());
}
public Rectangle(double whidth, double height, String color) {
super(color);
this.whidth = whidth;
this.height = height;
}
}
package com.homework014;
public class Circle extends Shape{
private double radius;
@Override
public double getArea() {
return 3.14* radius * radius;
}
@Override
public double getPer() {
return 2* 3.14* radius;
}
@Override
public void showAll(){
System.out.println("radius=" + radius +" area=" + getArea() + " per="
+ getPer() + " color=" + getColor());
}
public Circle(double radius, String color) {
super(color);
this.radius = radius;
}
public Circle() {
super();
}
}
package com.homework014;
public class Test {
public static void main(String[] args) {
Rectangle r = new Rectangle(5, 3, "白");
Circle c = new Circle(2, "蓝");
r.showAll();
c.showAll();
}
}
![]()