首页 > 其他 > 详细

第十一周上机练习

时间:2020-05-14 15:07:22      阅读:47      评论:0      收藏:0      [点我收藏+]

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 aaaaa;
 2 
 3 public class vehicle {
 4       public String brand;
 5       public String color;
 6       public double speed = 0;
 7       public vehicle(){      
 8       }
 9       public vehicle(String brand,String color){
10           this.brand=brand;
11           this.color=color;
12           speed=0;
13       }
14     public String getBrand() {
15         return brand;
16     }
17     public void setBrand(String brand) {
18         this.brand = brand;
19     }
20     public String getColor() {
21         return color;
22     }
23     public void setColor(String color) {
24         this.color = color;
25     }
26     public double getSpeed() {
27         return speed;
28     }
29     public void setSpeed(double speed) {
30         this.speed = speed;
31     }
32     public void run(){
33         System.out.println("颜色是"+getColor()+getBrand()+"速度是"+getSpeed());
34     }
35 }
 1 package aaaaa;
 2 
 3 public class vehicletest {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         vehicle a=new vehicle();
 8         a.brand="benz";
 9         a.color="black";
10         a.run();
11         }
12 }
 1 package aaaaa;
 2 
 3 public class car extends vehicle {
 4     public int loader;
 5     
 6     public car(){
 7     }
 8     public car(String brand,String color,int loader) {
 9         super(brand,color);
10         this.loader = loader;}
11      public void run() {
12             System.out.println("颜色是"+getColor()+getBrand()+"载人人数是:"+loader+"的速度是:"+getSpeed());
13         }
14 }
 1 package aaaaa;
 2 
 3 public class test {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         car b=new car();
 8         b.brand="honda";
 9         b.color="red";
10         b.loader=2;
11         b.run();
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 aaaaa;
 2 
 3 public abstract class shape {
 4     protected double area;
 5     protected double per;
 6     protected String color;
 7     public shape() {
 8     }
 9     public shape(String color) {
10         this.color = color;
11     }
12     public abstract void getArea();
13     public abstract void getPer();
14     public abstract void showAll();
15 }
 1 package aaaaa;
 2 
 3 public class Rectangle extends shape{
 4     double width;
 5     double height;
 6 
 7     public Rectangle() {
 8     }
 9     public Rectangle(double width, double height, String color) {
10         super();
11         this.width = width;
12         this.height = height;
13         this.color = color;
14     }
15     public void getArea() {
16         area = width * height;
17     }
18     public void getPer() {
19         per = (width + height) * 2;
20     }
21     public void showAll() {
22         System.out.println("矩形面积为:"+area+",周长为:"+per+",颜色:"+color);
23     }
24 }
 1 package aaaaa;
 2 
 3 public class Circle extends shape {
 4     double radius;
 5     public Circle() {
 6     }
 7     public Circle(double radius, String color) {
 8         this.color = color;
 9         this.radius = radius;
10     }
11     public void getArea() {
12         area = radius * radius * 3.14;
13     }
14     public void getPer() {
15         per = 2 * radius * 3.14;
16     }
17     public void showAll() {
18         System.out.println("圆的面积为"+area+"周长为"+per+"颜色为"+color);
19     }
20 }
 1 package aaaaa;
 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 }

 

第十一周上机练习

原文:https://www.cnblogs.com/2463594061ab----/p/12888197.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!