java代码---初识接口2

按如下要求编写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。

public class TestIo{
	public static void main(String[] args) {
		TestIo.Cylinder v = new TestIo().new Cylinder();
		// Cylinder v = new Cylinder();
		// System.out.println(v.Cylinder);

		
		System.out.println("area = "+v.area());
		// System.out.println(v);
		System.out.println("volume = "+v.volume());

	}
	interface A{
		double PI = 3.14;
		abstract double area();
	}
	interface B{
		abstract void setColor(String c);
	}
	interface C extends A,B{
		abstract double volume();
	}
	public class Cylinder implements C{
		public double radius = 2.0;
		public double height = 4.0;
		public String color = "red";

		public double area(){
			return ((this.radius)*(this.radius)*(this.PI));
		}
		public void setColor(String c){
			System.out.println("red");
		}
		public double volume(){
			 return ((this.radius)*(this.radius)*(this.PI)*(this.height));
		}
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值