package shier;
public class Vehicle {
protected String brand;
protected String color;
protected double speed;
public Vehicle(String brand, String color, double speed) {
super();
this.brand = brand;
this.color = color;
this.speed = 0;
}
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;
}
public void run(){
System.out.println("品牌是"+brand+",颜色是"+color+"的汽车,正在以"+speed+"速度奔跑");
}
}
package shier;
public class Car extends Vehicle {
int loader;
public int getLoader() {
return loader;
}
public void setLoader(int loader) {
this.loader = loader;
}
public Car(String brand, String color, double speed, int loader) {
super(brand, color, speed);
this.loader = loader;
}
public void run(){
System.out.println("品牌是"+super.getBrand()+",颜色是"+color+",载人数为"+loader+"的汽车,正在以"+speed+"速度奔跑");
}
}
package shier;
public class TestVehicle {
public static void main(String[] args) {
// TODO Auto-generated method stub
Vehicle v=new Vehicle("benz", "black", 120);
v.setSpeed(120);
v.run();
Car c=new Car("Honda", "red", 100, 2);
c.setSpeed(200);
c.run();
}
}
![](https://img2020.cnblogs.com/blog/2320047/202106/2320047-20210618125416080-1509261277.png)
public abstract class Shape {
double area;
double per;
char color;
public Shape() {
super();
}
public Shape(char color) {
super();
this.color = color;
}
public abstract double getPer();
public abstract double getArea();
public abstract void showAll();
public char getColor() {
return color;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
public class Circle extends Shape {
double radius;
public void showAll() {
System.out.println("圆形的半径是" + radius + ",颜色是" + color + ",周长是"
+ getPer() + ",面积是" + getArea());
}
@Override
public double getPer() {
return 2 * 3.14 * radius;
}
@Override
public double getArea() {
return 3.14 * radius * radius;
}
public Circle(double radius, char color) {
super();
this.radius = radius;
this.color = color;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
public class Rectangle extends Shape {
public double getPer() {
return (width + height) * 2;
}
public double getArea() {
return width * height;
}
double width;
double height;
public Rectangle(double width, double height, char color) {
super();
this.width = width;
this.height = height;
this.color = color;
}
public Rectangle() {
super();
}
public void showAll() {
System.out.println("矩形的长是" + width);
System.out.println("宽是" + height);
System.out.println("颜色是" + color);
System.out.println("周长是" + getPer());
System.out.println("面积是" + getArea());
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Rectangle r = new Rectangle(7, 4, '白');
r.showAll();
Circle c = new Circle(6, '黑');
c.showAll();
}
}
![](https://img2020.cnblogs.com/blog/2320047/202106/2320047-20210618125617709-851584276.png)