1、创建一个Shape形状类,包括一个getArea求面积方法,一个getC求周长方法
2、创建3个子类Circle圆、Rectangle矩形、Triangle三角形,分别有各自的构造方法,并重新父类的求面积、求周长方法
3、创建一个Test类,对以上的类创建对象进行测试
答案:
下面展示一些 代码。
//Shape类的创建
public class Shape {
//求面积的方法
public double getArea() {
return 0;
}
//求周长的方法
public double getC() {
return 0;
}
}
//子类圆的创建的创建
public class Circle extends Shape{
protected int r;
//有形参的构造方法
public Circle(int r) {
this.r=r;
}
public int getR() {
return r;
}
//方法的重写
public double getArea() {
return Math.PI*r*r;
}
public double getC() {
return Math.PI*2*r;
}
//子类矩形类的继承
public class Rectangle extends Shape {
private int weight;
private int height;
public Rectangle(int weight,int height) {
this.weight=weight;
this.height=height;
}
public int getweight() {
return weight;
}
public int getheight() {
return height;
}
//方法的重写
public double getArea() {
return weight*height;
}
public do


2241

被折叠的 条评论
为什么被折叠?



