继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

使用abstract抽象类,构造器求矩形,圆形的周长和面积

690017359
关注TA
已关注
手记 2
粉丝 6
获赞 0

父类:
public abstract class Shape {
private double perimeter;
private double area;

    public double getPerimeter() {
        return perimeter;
    }
    public void setPerimeter(double perimeter) {
        this.perimeter = perimeter;
    }

    public double getArea() {
        return area;
    }
    public void setArea(double area) {
        this.area = area;
    }
    public Shape(double perimeter,double area){
        this.perimeter=perimeter;
        this.area=area;
    }
    public Shape(){

    }

}

子类:

package com.sun;

public class Rectangle extends Shape {

public Rectangle(double perimeter, double area) {
    super(perimeter, area);
    // TODO Auto-generated constructor stub
}

}

子类:

package com.sun;

public class Circle extends Shape {

public Circle(double perimeter, double area) {
    super(perimeter, area);
    // TODO Auto-generated constructor stub
}

}

测试类:

package com.sun;

public class Test {

public static void main(String[] args) {
    int x=3; int y=4; int r=5;  double π=3.14;
    Shape cc=new Rectangle((x+y)*2,x*y);
    Shape dd=new Circle(2*π*r,π*r*r);

    System.out.println("矩形的周长和面积为:"+cc.getPerimeter()+
            "厘米"+"  "+cc.getArea()+"平方米");
    System.out.println("圆形的周长和面积为:"+dd.getPerimeter()+
            "厘米"+"  "+dd.getArea()+"平方米");

}

}

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP