1.(1)
2.(1)package finish; public abstract class finish { protected double area;// 面积 protected double per;// 周长 protected String color;// 颜色 public finish() { } public finish(String color) { this.color = color; } public abstract void s(); public abstract void c(); public abstract void showAll();
(2)public class Rectangle extends finish { double width; double height; public Rectangle() { } public Rectangle(double width, double height, String color) { super(); this.width = width; this.height = height; this.color = color; } @Override public void s() { area = width * height; } @Override public void c() { // TODO 自动生成的方法存根 per = (width + height) * 2; } @Override public void showAll() { // TODO 自动生成的方法存根 System.out.println("矩形面积为:" + area + ",周长为:" + per+",颜色:"+color); } } }
public class Rectangle extends finish { double radius; public Rectangle() { } public Rectangle(double radius, String color) { this.color = color; this.radius = radius; } @Override public void s() { // TODO 自动生成的方法存根 area = radius * radius * 3.14; } @Override public void c() { // TODO 自动生成的方法存根 per = 2 * radius * 3.14; } @Override public void showAll() { // TODO 自动生成的方法存根 System.out.println("圆的面积为:" + area + ",周长为:" + per+",颜色:"+color); } }
public class finish { public static void main(String[] args) { Circle circle = new Circle(2,"break"); Rectangle rectangle = new Rectangle(3,5,"red"); circle.s(); circle.c(); circle.showAll(); rectangle.s(); rectangle.c(); rectangle.showAll(); } }
public class Vehicle { // 属性 public String brand; public String color; public Double speed = 0.0; // set() 和 get()方法
(2)package finish; public class Car extends Vehicle{ // 属性 public Integer loader; // set() 和 get()方法 public Integer getLoader() { return loader; } public void setLoader(Integer loader) { this.loader = loader; } // 无参构造方法 public Car() { super(); } // 有参构造方法 public void Car(String brand, String color, Double speed,Integer loader) { this.brand = brand; this.color = color; this.speed = speed; this.loader = loader; } // 重写 /* * 重写是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变。即外壳不变,核心重写! * 重写的好处在于子类可以根据需要,定义特定于自己的行为。 也就是说子类能够根据需要实现父类的方法。 * 重写方法不能抛出新的检查异常或者比被重写方法申明更加宽泛的异常。 * 例如: 父类的一个方法申明了一个检查异常 IOException,但是在重写这个方法的时候不能抛出 Exception 异常, * 因为 Exception 是 IOException 的父类,只能抛出 IOException 的子类异常。 * */ @Override public String toString() { return "Car [loader=" + loader + "]"; } // 用打印语句描述轿车奔跑的功能 public void run() { System.out.println("汽车品牌:"+this.brand+" 颜色:"+this.color+" 速度:"+this.speed+" 核载人数:"+this.loader); } }
package finish; import org.junit.Test; public class RunTest { @Test public void runTest() { Vehicle ve = new Vehicle(); ve.Vehicle("benz","black"); ve.run(); ve.Vehicle("benz","black",300.0); ve.run(); } @Test public void runTest2() { Car ca = new Car(); ca.Car("Honda","red",300.0,2); ca.run(); } }
public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = 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 finish() { super(); } // 有参构造方法 public void Vehicle(String brand, String color) { this.brand = brand; this.color = color; } public void Vehicle(String brand, String color, Double speed) { this.brand = brand; this.color = color; this.speed = speed; } // 重写 @Override public String toString() { return "Vehicle [brand=" + brand + ", color=" + color + ", speed=" + speed + "]"; } // 用打印语句描述汽车奔跑的功能 public void run() { System.out.println("汽车品牌:"+this.brand+" 颜色:"+this.color+" 速度:"+this.speed); } }