第十一周java作业
class fff{ public static void main(String[] args) { Vehicle v=new Vehicle("benz","black",0); String s = v.toString(); System.out.println(s); v.run(); } } public class Vehicle { private String brand; private String color="红"; private double spend=0; public Vehicle(String brand,String color,double spend) { this.brand = brand; this.color=color; this.spend=spend; } public Vehicle(){} public String getColor() { return color; } public void setColor(String color) { this.color = color; } public double getSpend() { return spend; } public void setSpend(double spend) { this.spend = spend; } public String getBrand() { return brand; } public void run(){ System.out.println("车在跑"); } @Override public String toString() { return "Vehicle{" + "brand='" + brand + '\'' + ", color='" + color + '\'' + ", spend=" + spend + '}'; } } //子类 class text2{ public static void main(String[] args) { Car c=new Car("Honda","red",0,2); c.run(); } } public class Car extends Vehicle { int loader; public Car(String brand, String color, double spend, int loader) { super(brand, color, spend); this.loader = loader; } public int getLoader() { return loader; } public void setLoader(int loader) { this.loader = loader; } public void run(){ System.out.println("功能升级"); } }
public class PolyDemo { public static void main(String[] args) { Rectangle r=new Rectangle("蓝",6,6); Circle c=new Circle("蓝",7); r.getAll(); c.getAll(); } } //父类(形状类) public abstract class Shape { double area; double per; String color; public Shape() { } public Shape(String color) { this.color = color; } public Shape(int area, int per, String color) { this.area = area; this.per = per; this.color = color; } public abstract double getArea(); public abstract double getPer(); public abstract void getAll(); public String getColor() { return color; } } //子类(矩形类) public class Rectangle extends Shape{ int width; int height; public Rectangle(String color, int width, int height) { super(color); this.width = width; this.height = height; } @Override public double getArea() { area=width*height; return area; } @Override public double getPer() { per=(width+height)*2; return per; } @Override public void getAll() { System.out.println("宽为"+width+"高为"+height+"面积为"+getArea()+"周长为"+getPer()); } } 子类(圆形类) public class Circle extends Shape { int radius; public Circle(String color, int radius) { super(color); this.radius = radius; } @Override public double getArea() { area=3.14*(radius*radius); return area; } @Override public double getPer() { per=2*3.14*radius; return per; } @Override public void getAll() { System.out.println("半径为"+radius+"面积为"+getArea()+"周长为"+getPer()); } }