按如下要求编写Java程序:

(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。

定义接口A

package 实验六2;

public interface A{
    double PI = 3.14;
    double area();
}

定义接口B

package 实验六2;

public interface B {
    void setColor(String c);
}

定义接口C

package 实验六2;

public interface C extends A, B{
    double volume();
}

定义圆柱体类Cylinder

package 实验六2;

public class Cylinder implements C{
    private double radius;
    private double height;
    private String color;
    public Cylinder(double radius, double height, String color) {
        super();
        this.radius = radius;
        this.height = height;
        this.color = color;
    }
    @Override
    public double area() {
        return PI*radius*radius;
    }
    @Override
    public void setColor(String color) {
        System.out.println("圆柱体的颜色是:"+this.color);
    }
    @Override
    public double volume() {
        System.out.println("圆柱体的体积是:"+PI*radius*radius*height);
        return 0;
    }
}

创建主类

package 实验六2;


public class test {
    public static void main(String[] args) {
        Cylinder cylinder=new Cylinder(6,3,"black");
        System.out.println("圆柱的面积是:"+cylinder.area());//圆柱的面积
        cylinder.volume();//圆柱的体积
        cylinder.setColor("black");
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值