第十一周上机练习

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 methoda;
 2 
 3 public class Vehicle {
 4         String brand ;
 5         String color ;
 6         double speed ;
 7         public Vehicle() {
 8             super();
 9             // TODO Auto-generated constructor stub
10         }
11         public Vehicle(String brand, String color, double speed) {
12             super();
13             this.brand = brand;
14             this.color = color;
15             this.speed = speed;
16         }
17         public String getBrand() {
18             return brand;
19         }
20         public void setBrand(String brand) {
21             this.brand = brand;
22         }
23         public String getColor() {
24             return color;
25         }
26         public void setColor(String color) {
27             this.color = color;
28         }
29         public double getSpeed() {
30             return speed;
31         }
32         public void setSpeed(double speed) {
33             this.speed = speed;
34         }
35         
36         @Override
37         public String toString() {
38             return "Vehicle [brand=" + brand + ", color=" + color + ", speed=" + speed + "]";
39         }
40         
41         public void run(){
42             System.out.println("brand=" + brand + ", color=" + color + ", speed=" + speed );
43         }
44         
45 }
 1 package methoda;
 2 
 3 public class VehicleTest {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         Vehicle v = new Vehicle("benz","black",0.0);
 8         System.out.println(v);
 9 
10     }
11 
12 }
 1 package methoda;
 2 
 3 public class Car extends Vehicle {
 4     int loader ;
 5     public Car (){
 6         super();
 7     }
 8     public Car (String brand, String color, double speed,int loader){
 9         super(brand,color,speed);
10         this.loader = loader;
11     }
12     @Override
13     public void run(){
14         System.out.println("brand=" + brand + ", color=" + color + ", speed=" + speed  +",loader"+ loader);
15     }
16     
17 
18 }
 1 package methoda;
 2 
 3 public class Test {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         Car c= new Car("Honda","red",0.0,2);
 8         
 9         c.run();
10 
11     }
12 
13 }

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 methodc;
 2 
 3 public abstract class Shape {
 4     int area;
 5     int per;
 6     String color;
 7 
 8     public Shape() {
 9         super();
10         // TODO Auto-generated constructor stub
11     }
12 
13     public Shape(int area, int per, String color) {
14         super();
15         this.area = area;
16         this.per = per;
17         this.color = color;
18     }
19 
20     public int getArea() {
21         return area;
22     }
23 
24     public void setArea(int area) {
25         this.area = area;
26     }
27 
28     public int getPer() {
29         return per;
30     }
31 
32     public void setPer(int per) {
33         this.per = per;
34     }
35 
36     public String getColor() {
37         return color;
38     }
39 
40     public void setColor(String color) {
41         this.color = color;
42     }
43 
44     public abstract void getAreaa();
45 
46     public abstract void getPera();
47 
48     public abstract void showAll();
49 
50 }
 1 package methodc;
 2 
 3 public class Rectangle extends Shape {
 4     int Width=5;
 5     int height=3;
 6     
 7     
 8     
 9     
10     @Override
11     public void getAreaa() {
12         System.out.println("area=" + Width*height );
13 
14     }
15 
16     public void getPera() {
17         System.out.println("per=" + (Width+height)*2 );
18 
19     }
20 
21     public void showAll() {
22         getAreaa();
23         getPera();
24         System.out.println("Width="+ Width + ",height=" + height);
25         
26 
27     }
28 }
 1 package methodc;
 2 
 3 public class Circle extends Shape{
 4     int Circle=2;
 5     
 6     
 7 
 8     @Override
 9     public void getAreaa() {
10         System.out.println("area="+ 3.14*Circle*Circle  );
11 
12     }
13 
14     public void getPera() {
15         System.out.println("per=" + 4*3.14*Circle);
16 
17     }
18 
19     public void showAll() {
20         getAreaa();
21         getPera();
22         System.out.println("Circle=" + Circle);
23         
24 
25     }
26 
27 }
 1 package methodc;
 2 
 3 public class PolyDemo {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         Rectangle r = new Rectangle();
 8         r.getAreaa();
 9         r.getPera();
10         r.showAll();
11         
12         Circle c = new Circle();
13         c.getAreaa();
14         c.getPera();
15         c.showAll();
16         
17     
18 
19     }
20 
21 }
原文地址:https://www.cnblogs.com/wuhaoovo/p/12887655.html