(1)定义接口A,里面包含值为3.14的常量PI和抽象方法double area()。 (2)定义接口B,里面包含抽象方法void setColor(String c)。

代码

/*第一题:
        (1)定义接口A,里面包含值为3.14的常量PI和抽象方法double area()。
        (2)定义接口B,里面包含抽象方法void setColor(String c)。
        (3)定义接口C,该接口继承了接口A和B,里面包含抽象方法void volume()。
        (4)定义圆柱体类Cylinder实现接口C,该类中包含三个成员变量:底圆半径radius、
        圆柱体的高height、颜色color。
        (5)创建主类来测试类Cylinder。*/
public class Homework1 {
    public static void main(String[] args) {
        C cylinder = new Cylinder(2,2,"黑色");
        System.out.println(cylinder);
    }
}

//定义接口A,里面包含值为3.14的常量PI和抽象方法double area()
interface A{
    double PI = 3.14;
    double area();
}

//定义接口B,里面包含抽象方法void setColor(String c)
interface B{
    void setColor(String c);
}

//定义接口C,该接口继承了接口A和B,里面包含抽象方法void volume()
interface C extends A, B{
    double volume();
}

//定义圆柱体类Cylinder实现接口C,该类中包含三个成员变量:
//底圆半径radius、圆柱体的高height、颜色color
class Cylinder implements C{
    //底圆半径
    private double radius;
    private double height;
    private String color;

    //constructor
    public Cylinder() {
    }

    public Cylinder(double radius, double height, String color) {
        this.radius = radius;
        this.height = height;
        this.color = color;
    }

    //setter and getter
    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public String getColor() {
        return color;
    }

    @Override
    public double area() {
        return this.getRadius()*this.getRadius()*PI;
    }

    @Override
    public void setColor(String c) {
        this.color = c;
    }

    @Override
    public double volume() {
        return this.area()*this.getHeight();
    }

    @Override
    public String toString() {
        return "Cylinder{" +
                "radius=" + radius +
                ", height=" + height +
                ", color='" + color + '\'' +
                '}';
    }
}


  • 20
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值