第十一次java作业

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 LXR;
 2 
 3 public class Vehicle{
 4     public String brand;
 5     public String color;
 6     double speed=0;
 7     public String getBrand() {
 8         return brand;
 9     }
10     public void setBrand(String brand) {
11         this.brand = brand;
12     }
13     public String getColor() {
14         return color;
15     }
16     public void setColor(String color) {
17         this.color = color;
18     }
19     public double getSpeed() {
20         return speed;
21     }
22     public void setSpeed(double speed) {
23         this.speed = speed;
24     }
25     public void run(){
26         System.out.println(this.color+"的"+this.brand+"正在以"+this.speed+"的速度行驶");
27     }    
28 }
 1 package LXR;
 2 
 3 public class Vehicle{
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         Vehicle vehicle = new Vehicle("bujiadi","black",120);
 8         vehicle.run();
 9     }
10 
11 }
 1 package LXR;
 2 
 3 public class Car extends Vehicle {
 4     private int loader;
 5 
 6     public Car() {
 7 
 8     }
 9 
10     public Car(String brand, String color, double speed, int loader) {
11         super(brand, color, speed);
12         this.loader = loader;
13     }
14 
15     public void run() {
16         System.out.println(getColor() + "色的" + getBrand()+"载人人数:" + loader  + "的速度为:" + getSpeed());
17     }
18 
19 }
 1 package LXR;
 2 
 3 public class test {
 4 
 5     public static void main(String[] args) {
 6         // TODO Auto-generated method stub
 7         Car car = new Car("lanbojini", "red", 80,2);
 8         car.run();
 9     }
10 
11 }

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 public class LXR {
 2     public static void main(String[] args) {
 3         Rectangle r=new Rectangle("白",4,5);
 4         Circle c=new Circle("白",8);
 5         r.getAll();
 6         c.getAll();
 7     }
 8 }
 9 
10 
11 public abstract class Shape {
12     double area;
13     double per;
14     String color;
15 
16     public Shape() {
17     }
18 
19     public Shape(String color) {
20         this.color = color;
21     }
22 
23     public Shape(int area, int per, String color) {
24         this.area = area;
25         this.per = per;
26         this.color = color;
27     }
28     public abstract double getArea();
29     public abstract double getPer();
30     public abstract void getAll();
31 
32     public String getColor() {
33         return color;
34     }
35 }
 1 public class Rectangle extends Shape{
 2     int width;
 3     int height;
 4 
 5     public Rectangle(String color, int width, int height) {
 6         super(color);
 7         this.width = width;
 8         this.height = height;
 9     }
10 
11     @Override
12     public double getArea() {
13          area=width*height;
14         return area;
15     }
16 
17     @Override
18     public double getPer() {
19         per=(width+height)*2;
20         return per;
21     }
22 
23     @Override
24     public void getAll() {
25         System.out.println("高为"+height+"宽为"+wight+"面积为"+getArea()+"周长为"+getPer());
26 
27     }
28 }
 1 public class Circle extends Shape {
 2     int radius;
 3 
 4     public Circle(String color, int radius) {
 5         super(color);
 6         this.radius = radius;
 7     }
 8 
 9     @Override
10     public double getArea() {
11     area=3.14*(radius*radius);
12         return area;
13     }
14 
15     @Override
16     public double getPer() {
17     per=2*3.14*radius;
18         return per;
19     }
20 
21     @Override
22     public void getAll() {
23         System.out.println("半径为"+radius+"面积为"+getArea()+"周长为"+getPer());
24     }
25 }
原文地址:https://www.cnblogs.com/919753740yu/p/12888803.html