5.14作业
1、(1)定义一个汽车类Vehicle,要求如下:(知识点:类的继承 方法的覆盖)
(a)属性包括:汽车品牌brand(String类型)、颜色color(String类型)和速度speed(double类型)。
(b)至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但速度的初始值必须为0)。
(c)为属性提供访问器方法。注意:汽车品牌一旦初始化之后不能修改。
(d)定义一个一般方法run(),用打印语句描述汽车奔跑的功能
定义测试类VehicleTest,在其main方法中创建一个品牌为“benz”、颜色为“black”的汽车。
(2)定义一个Vehicle类的子类轿车类Car,要求如下:
(a)轿车有自己的属性载人数loader(int 类型)。
(b)提供该类初始化属性的构造方法。
(c)重新定义run(),用打印语句描述轿车奔跑的功能。
(d)定义测试类Test,在其main方法中创建一个品牌为
1 class text{ 2 public static void main(String[] args) { 3 Vehicle v=new Vehicle("benz","black",0); 4 String s = v.toString(); 5 System.out.println(s); 6 v.run(); 7 } 8 9 10 } 11 public class Vehicle { 12 private String brand; 13 private String color="红"; 14 private double spend=0; 15 16 public Vehicle(String brand,String color,double spend) { 17 this.brand = brand; 18 this.color=color; 19 this.spend=spend; 20 } 21 public Vehicle(){} 22 23 public String getColor() { 24 return color; 25 } 26 27 public void setColor(String color) { 28 this.color = color; 29 } 30 31 public double getSpend() { 32 return spend; 33 } 34 35 public void setSpend(double spend) { 36 this.spend = spend; 37 } 38 39 public String getBrand() { 40 return brand; 41 } 42 public void run(){ 43 System.out.println("车在跑"); 44 } 45 46 @Override 47 public String toString() { 48 return "Vehicle{" + 49 "brand='" + brand + '\'' + 50 ", color='" + color + '\'' + 51 ", spend=" + spend + 52 '}'; 53 } 54 } 55 //子类 56 class text2{ 57 public static void main(String[] args) { 58 Car c=new Car("Honda","red",0,2); 59 c.run(); 60 } 61 62 } 63 public class Car extends Vehicle { 64 int loader; 65 66 public Car(String brand, String color, double spend, int loader) { 67 super(brand, color, spend); 68 this.loader = loader; 69 } 70 71 public int getLoader() { 72 return loader; 73 } 74 75 public void setLoader(int loader) { 76 this.loader = loader; 77 } 78 public void run(){ 79 System.out.println("功能升级"); 80 } 81 }
2、设计四个类,分别是:(知识点:抽象类及抽象方法)
(1)Shape表示图形类,有面积属性area、周长属性per,颜色属性color,有两个构造方法(一个是默认的、一个是为颜色赋值的),还有3个抽象方法,分别是:getArea计算面积、getPer计算周长、showAll输出所有信息,还有一个求颜色的方法getColor。
(2)2个子类:
1)Rectangle表示矩形类,增加两个属性,Width表示长度、height表示宽度,重写getPer、getArea和showAll三个方法,另外又增加一个构造方法(一个是默认的、一个是为高度、宽度、颜色赋值的)。
2)Circle表示圆类,增加1个属性,radius表示半径,重写getPer、getArea和showAll三个方法,另外又增加两个构造方法(为半径、颜色赋值的)。
(3)一个测试类PolyDemo,在main方法中,声明创建每个子类的对象,并调用2个子类的showAll方法。
1 package Homework3; 2 3 public abstract class Shape { 4 protected double area; 5 protected double per; 6 protected String color; 7 8 public Shape() { 9 } 10 11 public Shape(String color) { 12 this.color = color; 13 } 14 15 public abstract void getArea(); 16 17 public abstract void getPer(); 18 19 public abstract void showAll(); 20 21 }
1 package Homework3; 2 3 public class Rectangle extends Shape { 4 double width; 5 double height; 6 7 public Rectangle() { 8 } 9 10 public Rectangle(double width, double height, String color) { 11 super(); 12 this.width = width; 13 this.height = height; 14 this.color = color; 15 } 16 17 public void getArea() { 18 area = width * height; 19 } 20 21 public void getPer() { 22 per = (width + height) * 2; 23 } 24 25 public void showAll() { 26 System.out.println("矩形面积为:" + area + ",周长为:" + per + ",颜色:" + color); 27 28 }
1 package Homework3; 2 3 public class Circle extends Shape { 4 double radius; 5 6 public Circle() { 7 } 8 9 public Circle(double radius, String color) { 10 this.color = color; 11 this.radius = radius; 12 } 13 14 public void getArea() { 15 area = radius * radius * 3.14; 16 } 17 18 public void getPer() { 19 per = 2 * radius * 3.14; 20 } 21 22 public void showAll() { 23 System.out.println("圆的面积为:" + area + ",周长为:" + per + ",颜色:" + color); 24 } 25 }
1 package Homework3; 2 3 public class PolyDemo { 4 5 public static void main(String[] args) { 6 // TODO Auto-generated method stub 7 Shape a = new Circle(2, "紫色"); 8 Shape b = new Rectangle(3, 6, "粉色"); 9 a.getArea(); 10 a.getPer(); 11 a.showAll(); 12 b.getArea(); 13 b.getPer(); 14 b.showAll(); 15 } 16 17 }