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));
		}
	}
}

猜你喜欢

转载自blog.csdn.net/devil_net/article/details/79820620