package forth; interface ShapeArea { double PI =3.1415926; double area (); } interface ShapeColor { int BLACK =0, BLUE =1, GREEN =2, CYAN =3, RED =4, MAGENTA =5, LIGHT_YELLOW =6, LIGHT_GRAY =7, DARK_GRAY =8, LIGHT_BLUE =9, LIGH_GREEN =19, LIGHT_CYAN =11, LIGHT_RED =12, LIGHT_MAGENTA =13, YELLOW =14, WHITE =15; } class Circle implements ShapeArea , ShapeColor { double radius ; int color ; Circle ( double r , int c) { radius =( r >=0.0f ? r :0.0f); color = c ; } public double area () { return PI * radius * radius ;} } public class five{ static String [] colArray ={ " BLACK ","BLUE ","GREEN ","CYAN "," RED "," MAGENTA ", " LIGHT _ YELLOW "," LIGHT _ GRAY "," DARK _ GRAY "," LIGHT _ BLUE "," LIGHT _ GREEN "," LIGHT CYAN "," LIGHT RED ", " LIGHT MAGENTA "," YELLOW "," WHITE "}; public static void main ( String [] args ){ Circle cir1 = new Circle (5, ShapeColor . BLUE ); Circle cir2= new Circle (7, ShapeColor . GREEN ); Circle cir3 = new Circle (4, ShapeColor . MAGENTA ); System.out.println (" cir1's Area ="+cir1.area()+",color ="+ colArray [cir1.color]); System.out.println ("cir2's Area ="+cir2.area()+", color ="+ colArray [cir2.color]); System.out.println ("cir3's Area ="+cir3.area()+", color ="+ colArray [cir3.color]); } }简单解释一下
时间: 2024-01-23 15:02:14 浏览: 135
java基础练习题.docx
这是一个 Java 代码示例,定义了两个接口 ShapeArea 和 ShapeColor,以及一个 Circle 类实现了这两个接口。ShapeArea 接口定义了一个常量 PI 和一个抽象方法 area(),表示计算图形的面积。ShapeColor 接口定义了一些常量,表示颜色。Circle 类有两个属性:半径和颜色,以及一个构造方法初始化这两个属性。Circle 类还实现了 ShapeArea 和 ShapeColor 接口,并实现了 area() 方法。five 类中包含一个 main 方法,创建了三个 Circle 对象,并输出它们的面积和颜色。
阅读全文