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人的轿车。
packagezlm;public classVehicle {publicString brand;publicString color;public double speed=0;voidsetVehicle(String brand,String color) {this.brand=brand;this.color=color;
}void access(String brand,String color,doublespeed) {this.brand=brand;this.color=color;this.speed=speed;
}voidrun() {
System.out.println("该汽车的品牌为:"+this.brand+"颜色为:"+this.color+"速率为:"+this.speed);
}
}
packagezlm;public classVehicleTest {public static voidmain(String[] args) {//TODO Auto-generated method stub
Vehicle c=newVehicle();
c.setVehicle("benz","yellow");
c.run();
c.access("benz" ,"black", 300);
c.run();
}
}
packagezlm;public class Car extendsVehicle{intloader;void access(String brand,String color,double speed,intloader) {this.brand=brand;this.color=color;this.speed=speed;this.loader=loader;
}voidrun() {
System.out.println("该汽车的品牌为:"+this.brand+"颜色为:"+this.color+"速率为:"+this.speed+"核载人数为:"+this.loader);
}
}
packagezlm;public classTest {public static voidmain(String[] args) {//TODO Auto-generated method stub
Car c=newCar();
c.access("Honda", "red", 300,2);
c.run();
}
}
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方式。
packagezlm;public abstract classShape {protected double area;//面积
protected double per;//周长
protected String color;//颜色
publicShape() {
}publicShape(String color) {this.color=color;
}public abstract voids();public abstract voidc();public abstract voidshowAll();
}
packagezlm;public class Rectangle extendsShape {doublewidth;doubleheight;publicRectangle() {
}public Rectangle(double width, doubleheight, String color) {super();this.width =width;this.height =height;this.color =color;
}
@Overridepublic voids() {
area= width *height;
}
@Overridepublic voidc() {//TODO 自动天生的方式存根
per = (width + height) * 2;
}
@Overridepublic voidshowAll() {//TODO 自动天生的方式存根
System.out.println("矩形面积为:" + area + ",周长为:" + per+",颜色:"+color);
}
}
packagezlm;public class Circle extendsShape {doubleradius;publicCircle() {
}public Circle(doubleradius, String color) {this.color =color;this.radius =radius;
}
@Overridepublic voids() {//TODO 自动天生的方式存根
area = radius * radius * 3.14;
}
@Overridepublic voidc() {//TODO 自动天生的方式存根
per = 2 * radius * 3.14;
}
@Overridepublic voidshowAll() {//TODO 自动天生的方式存根
System.out.println("圆的面积为:" + area + ",周长为:" + per+",颜色:"+color);
}
}
packagezlm;public classTest1 {public static voidmain(String[] args) {
Circle circle= new Circle(2,"break");
Rectangle rectangle= new Rectangle(3,5,"red");
circle.s();
circle.c();
circle.showAll();
rectangle.s();
rectangle.c();
rectangle.showAll();
}
}
原文链接:https://www.cnblogs.com/Lucky-M/p/12887354.html
本站声明:网站内容来源于网络,若有侵权,请联系我们,我们将及时处理。