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方法中创建一个品牌为“Honda”、颜色为“red”,载人数为2人的轿车。

 1 package text;
 2 
 3 public class Vehicle {
 4     String brand;
 5     String color;
 6     double speed = 0;
 7 
 8     //
 9     public void setVehicle(String brand, String color) {
10         this.brand = brand;
11         this.color = color;
12     }
13 
14     void access(String brand, String color, double speed) {
15         this.brand = brand;
16         this.color = color;
17         this.speed = speed;
18     }
19 
20     public void run() {
21         System.out.println("该汽车的品牌为:" + this.brand + "颜色为:" + this.color + "速度为:" + this.speed);
22     }
23 }
24 
25 
26 
27 
28 测试
29 public class VehicleTest {
30 
31     public static void main(String[] args) {
32         // TODO Auto-generated method stub
33         Vehicle c = new Vehicle();
34         c.setVehicle("benz", "black");
35         c.run();
36         c.access("benz", "red", 400);
37         c.run();
38     }
39 
40 }

 1 package text;
 2 
 3 public class Car1 extends Vehicle {
 4 
 5     int loader;
 6 
 7     void access(String brand, String color, double speed, int loader) {
 8         this.brand = brand;
 9         this.color = color;
10         this.speed = speed;
11         this.loader = loader;
12     }
13 
14     public void run() {
15         System.out.println("该汽车品牌为:" + this.brand + "颜色为:" + this.color + "速度为:" + this.speed + "核载人数" + this.loader);
16     }
17 }
18 测试
19 public class Test1 {
20 
21     public static void main(String[] args) {
22         // TODO Auto-generated method stub
23         Car1 a = new Car1();
24         a.access("Honda", "red", 400, 2);
25         a.run();
26     }
27 
28 }



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 text;
  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 
 12     public Shape(String color) {
 13         this.color = color;
 14     }
 15 
 16     public abstract void getArea();
 17 
 18     public abstract void getPer();
 19 
 20     public abstract void showAll();
 21 }
 22 
 23 
 24 package text;
 25 
 26 public class Rectangle extends Shape {
 27     double height;
 28     double width;
 29 
 30     public Rectangle() {
 31 
 32     }
 33 
 34     public Rectangle(double width, double height, String color) {
 35         super();
 36         this.width = width;
 37         this.height = height;
 38         this.color = color;
 39     }
 40 
 41     @Override
 42     public void getArea() {
 43         // TODO Auto-generated method stub
 44         area = height * width;
 45     }
 46 
 47     @Override
 48     public void getPer() {
 49         // TODO Auto-generated method stub
 50         per = (height + width) * 2;
 51     }
 52 
 53     @Override
 54     public void showAll() {
 55         // TODO Auto-generated method stub
 56         System.out.println("矩形面积为:" + area + " , 周长为:" + per + " , 颜色为:" + color);
 57     }
 58 }
 59 
 60 
 61 package text;
 62 
 63 public class Circle extends Shape {
 64     double radius;
 65 
 66     public Circle() {
 67 
 68     }
 69 
 70     public Circle(double radius, String color) {
 71         this.color = color;
 72         this.radius = radius;
 73     }
 74 
 75     @Override
 76     public void getArea() {
 77         // TODO 自动生成的方法存根
 78         area = radius * radius * 3.14;
 79     }
 80 
 81     @Override
 82     public void getPer() {
 83         // TODO 自动生成的方法存根
 84         per = 2 * radius * 3.14;
 85     }
 86 
 87     @Override
 88     public void showAll() {
 89         // TODO 自动生成的方法存根
 90         System.out.println("圆的面积为:" + area + " , 周长为:" + per + " , 颜色为:" + color);
 91     }
 92 
 93 }
 94 
 95 package text;
 96 
 97 public class Test3 {
 98 
 99     public static void main(String[] args) {
100         // TODO Auto-generated method stub
101         Circle circle = new Circle(3, "blue");
102         Rectangle rectangle = new Rectangle(2, 7, "yellow");
103         circle.getArea();
104         circle.getPer();
105         circle.showAll();
106 
107         rectangle.getArea();
108         rectangle.getPer();
109         rectangle.showAll();
110 
111     }
112 
113 }

猜你喜欢

转载自www.cnblogs.com/gwz-1314/p/12888043.html