2、设计2个类,要求如下:(知识点:类的继承 方法的覆
盖) [必做题]
(String类型)和速度speed(duble类型)。
初始化为任意值,但速度的初始值必须为0)。
之后不能修改。
功能
的汽车。
,载人数为2人的轿车。
public class vehicle { protected String brand; protected String clr; protected double speed; public vehicle(String brand, String clr, double speed) { super(); this.brand = brand; this.clr = clr; this.speed = 0; } public vehicle() { super(); // TODO Auto-generated constructor stub } public String getBrand() { return brand; } public String getClr() { return clr; } public void setClr(String clr) { this.clr = clr; } public double getSpeed() { return speed; } public void setSpeed(double speed) { this.speed = speed; } public void run(){ System.out.println("品牌是"+brand +"颜色是"+clr +"速度是"+speed); } public static void main(String[] args) { vehicle a=new vehicle("benzll","blackll",80); a.run(); } }
public class car extends vehicle{ int lader; public car() { super(); // TODO Auto-generated constructor stub } public car(String brand, String clr, double speed) { super(brand, clr, speed); // TODO Auto-generated constructor stub } public car(String brand, String clr, double speed, int lader) { super(brand, clr, speed); this.lader = lader; } public int getLader() { return lader; } public void setLader(int lader) { this.lader = lader; } public void run(){ System.out.println("品牌是"+brand +"颜色是"+clr +"速度是"+speed +"人数是"+lader); } public static void main(String[] args) { // TODO Auto-generated method stub car b=new car("hndall","redll",100,2); b.run(); } }
) [必做题]
颜色属性clr,有两个构造方法(一个是默认的、一个是
为颜色赋值的),还有3个抽象方法,分别是:getArea计算
面积、getPer计算周长、shwAll输出所有信息,还有一个求
颜色的方法getClr。
、height表示宽度,重写getPer、getArea和shwAll三个方法
,另外又增加一个构造方法(一个是默认的、一个是为高度
、宽度、颜色赋值的)。
getPer、getArea和shwAll三个方法,另外又增加两个构造
方法(为半径、颜色赋值的)。
子类的shwAll方法。
public abstract class Shape { protected double area; protected double per; protected String color; public Shape() { super(); // TODO Auto-generated constructor stub } public Shape(String color) { super(); this.color = color; } protected abstract double getper(); protected abstract double getarea(); protected abstract void showall(); public String getcolor(){ return color; } }
public class rectangle extends Shape { double width; double height; protected double getper(){ return (width+height)*2; } protected double getarea(){ return (width*height); } protected void showall(){ System.out.println("矩形的长是"+height+ "矩形的宽是"+width +"颜色是"+color+"周长是"+getper() +"面积是"+getarea()); } public rectangle() { super(); // TODO Auto-generated constructor stub } public rectangle(double width, double height,String color) { super(); this.width = width; this.height = height; this.color=color; } }
public class circle extends Shape { double radius; protected double getper(){ return 2*radius*3.14; } protected double getarea(){ return 3.14*radius*radius; } protected void showall(){ System.out.println("圆半径是"+radius+"圆周长是"+getper()+"圆颜色是"+color+"圆面积是"+getarea()); } public circle() { super(); // TODO Auto-generated constructor stub } public circle(double radius ,String color) { super(); this.radius = radius; this.color=color; } }
public class test { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub rectangle a=new rectangle(5,8,"粉色"); a.showall(); circle b=new circle(4,"绿色"); b.showall(); } }
原文:https://www.cnblogs.com/songchi/p/14862519.html