/**建立一个Po(点)类,包含数据成员X,Y(坐标点),构造器;
以Po为父类,实现一个Circle(圆)类作为子类,增加数据成员R(半径),构造器、求圆面积方法getArea(),
求圆周长方法getCircumference();
再以Cicle类为父类,实现出一个Cylinder (圆柱体〕子类,增加数据成员H(高),构造器,求圆柱体积方法getVolumn()、
求圆柱表面积方法getArea(),
请编写程序实现。(圆周率取3.14)
要求:
自定义各个类的toString方法,
要求当上面三个类的对象创建完毕后,我们直接打印这个三个对象可以显示这个对象的完整信息。
如打印Circle类的对象会输出:
Circle{X=1,Y=2,R=1,circumference=6.28,area=3.14}
友情提示X,Y,R应均为double类型;
*
*/
题目如上所示
public class day03 {
public static void main(String[] args) {
Po p = new Po(1, 2);
Circle c = new Circle(1,1,2);
Cylinder d = new Cylinder(1,1,2,1);
System.out.println(p.toString());
System.out.println(c.toString());
System.out.println(d.toString()); }
}
class Po {
private double X;
private double Y;
public Po(int X, int Y) {
this.X = X;
this.Y = Y;
}
public double getX() {
return X;
}
public double getY() {
return Y;
}
public void setX(int X) {
this.X = X;
}
public void setY(int Y) {
this.Y = Y;
}
@Override
public String toString(){
return "Po{X="+ getX() + ",Y="+getY() + "}";
}
}
class Circle extends Po{
private double R;
public double getR() {
return R;
}
public Circle(int X, int Y, int R) {
super(X, Y);
this.R = R;
}
public double getArea(){
return R * R * 3.14;
}
public double getCircumference(){
return 2 * 3.14 * R;
}
@Override
public String toString(){
return "Circle{"+ "X=" + getX() + ",Y=" + getX() + ",R=" + getR() +
",circumference=" + getCircumference() + ",area = " + getArea() +'}';
}
}
class Cylinder extends Circle{
private int H;
public Cylinder (int X, int Y, int R, int H) {
super(X,Y,R);
this.H = H;
}
public double getVolumn(){
return getArea() * H;
}
@Override
public String toString(){
return "Cylinder{"+"X="+ getX() + ",Y=" + getX() + ",R=" + getR() +
",H=" + H + ",Volumn=" + getVolumn() + ",area=" + getArea() + '}';
}
}
代码如上所示
结果如上所示