Java第十一次作业

2、设计2个类,要求如下:(知识点:类的继承 方法的覆盖) [必做题]
2.1 定义一个汽车类Vehicle,
2.1.1 属性包括:汽车品牌brand(String类型)、颜色color(String类型)和速度speed(double类型)。
2.1.2 至少提供一个有参的构造方法(要求品牌和颜色可以初始化为任意值,但速度的初始值必须为0)。
2.1.3 为属性提供访问器方法。注意:汽车品牌一旦初始化之后不能修改。
2.1.4 定义一个一般方法run(),用打印语句描述汽车奔跑的功能
2.1.5 在main方法中创建一个品牌为―benz‖、颜色为―black‖的汽车。
2.2 定义一个Vehicle类的子类轿车类Car,要求如下:
2.2.1 轿车有自己的属性载人数loader(int 类型)。
2.2.2 提供该类初始化属性的构造方法。
2.2.3 重新定义run(),用打印语句描述轿车奔跑的功能。
2.2.4 在main方法中创建一个品牌为―Honda‖、颜色为―red‖,载人数为2人的轿车。

 1 package boke6月4日;
 2 
 3 public class Vehicle {
 4     private String brand;
 5     private String color;
 6     private double speed;
 7 
 8     public Vehicle() {
 9         super();
10 
11     }
12 
13     public Vehicle(String brand, String color, double speed) {
14         super();
15         this.brand = brand;
16         this.color = color;
17         this.speed = 0;
18     }
19 
20     public String getBrand() {
21         return brand;
22     }
23 
24     public String getColor() {
25         return color;
26     }
27 
28     public void setColor(String color) {
29         this.color = color;
30     }
31 
32     public double getSpeed() {
33         return speed;
34     }
35 
36     public void setSpeed(double speed) {
37         this.speed = speed;
38     }
39     public void run(){
40         System.out.println(brand+"品牌的"+color+"颜色的汽车以"+speed+"速度奔跑");
41     }
42 
43 }
 1 package boke6月4日;
 2 
 3 public class Car extends Vehicle {
 4     private int loader;
 5 
 6     public Car() {
 7         super();
 8 
 9     }
10 
11     public Car(String brand, String color, double speed, int loader) {
12         super(brand, color, speed);
13         this.loader = loader;
14     }
15 
16     public int getLoader() {
17         return loader;
18     }
19 
20     public void setLoader(int loader) {
21         this.loader = loader;
22     }
23 
24     public void run() {
25         System.out.println(super.getBrand() + "品牌的" + super.getColor()
26                 + "颜色的汽车以" + super.getSpeed() + "速度奔跑载" + this.loader + "人");
27     }
28 
29 }
 1 package boke6月4日;
 2 
 3 public class Test1 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         Vehicle v = new Vehicle("banz", "black", 0);
10         v.setSpeed(100);
11         v.run();
12 
13         Car c = new Car("Honda", "red", 100, 2);
14         c.setSpeed(120);
15         c.run();
16 
17     }
18 
19 }

3、设计三个类,分别如下:(知识点:抽象类及抽象方法) [必做题]
3.1 设计Shape表示图形类,有面积属性area、周长属性per,颜色属性color,有两个构造方法(一个是默认的、一个是为颜色赋值的),还有3个抽象方法,分别是:getArea计算面积、getPer计算周长、showAll输出所有信息,还有一个求颜色的方法getColor。
3.2 设计 2个子类:
3.2.1 Rectangle表示矩形类,增加两个属性,Width表示长度height表示宽度,重写getPer、getArea和showAll三个方法,另外又增加一个构造方法(一个是默认的、一个是为高度、宽度、颜色赋值的)。
3.2.2 Circle表示圆类,增加1个属性,radius表示半径,重写getPer、getArea和showAll三个方法,另外又增加两个构造方法(为半径、颜色赋值的)。
3.3 在main方法中,声明创建每个子类的对象,并调用2个子类的showAll方法

 1 package boke6月4日;
 2 
 3 public abstract class Shape {
 4     protected double area;
 5     protected double per;
 6     protected String color;
 7 
 8     public Shape() {
 9         super();
10 
11     }
12 
13     public Shape(String color) {
14         super();
15         this.color = color;
16     }
17 
18     public abstract void getArea();
19 
20     public abstract void getPer();
21 
22     public abstract void showAll();
23 
24     public String getColor() {
25         return color;
26     }
27 
28 }
 1 package boke6月4日;
 2 
 3 public class Rectangle extends Shape {
 4     protected double width;
 5     protected double height;
 6 
 7     public Rectangle() {
 8         super();
 9 
10     }
11 
12     public Rectangle(double width, double height, String color) {
13         super();
14         this.width = width;
15         this.height = height;
16         this.color = color;
17     }
18 
19     @Override
20     public void getArea() {
21         area = width * height;
22 
23     }
24 
25     @Override
26     public void getPer() {
27         per = (width + height) * 2;
28 
29     }
30 
31     @Override
32     public void showAll() {
33         System.out.println("矩形面积为:" + area + ",周长为:" + per + ",颜色:" + color);
34     }
35 
36 }
 1 package boke6月4日;
 2 
 3 public class Circle extends Shape {
 4     protected double radius;
 5 
 6     public Circle() {
 7         super();
 8 
 9     }
10 
11     public Circle(String color, double radius) {
12         super();
13         this.color = color;
14         this.radius = radius;
15 
16     }
17 
18     @Override
19     public void getArea() {
20         area = radius * radius * 3.14;
21     }
22 
23     @Override
24     public void getPer() {
25         per = 2 * radius * 3.14;
26     }
27 
28     @Override
29     public void showAll() {
30         System.out.println("圆的面积为:" + area + ",周长为:" + per + ",颜色:" + color);
31     }
32 
33 }
 1 package boke6月4日;
 2 
 3 public class Test2 {
 4 
 5     /**
 6      * @param args
 7      */
 8     public static void main(String[] args) {
 9         Circle c = new Circle("black", 2.0);
10         c.getArea();
11         c.getPer();
12         c.showAll();
13 
14         Rectangle r = new Rectangle(3.0, 5.0, "red");
15         r.getArea();
16         r.getPer();
17         r.showAll();
18 
19     }
20 
21 }

 

posted @ 2021-06-06 12:04  MXT16  阅读(121)  评论(0编辑  收藏  举报