Java 实现接口计算圆柱的体积和面积并添加颜色
import java.util.Scanner; class Test { public static void main(String[] args) { fangfa i = new fangfa(); Cylinder s1 = new Cylinder(); Cylinder s2 = new Cylinder(); Cylinder s3 = new Cylinder(); System.out.print("容积:"); i.shixian(s1, 5.0, 6.0); System.out.print("面积:"); i.shixian1(s2, 5.0); System.out.println("你想要什么颜色的容器?"); Scanner scanner = new Scanner(System.in); //nextine中的L注意大写 String str1 = scanner.nextLine(); String color = str1; while (true) { if (str1.equals(null)) { System.out.println("重新输入"); continue; } else { System.out.print("颜色:"); //将str变量传给字符串,不需要用双引号将变量包起来 i.shixian2(s3, str1); break; } } } } class Cylinder implements C{ public double volume(double radius,double height) { return radius * radius * pi * height; } public double area(double radius1){ return radius1*radius1*pi; } public String setcolor(String color){ return color; } } class fangfa{ public double shixian(C per,double radius,double height){ System.out.println(per.volume(radius,height)); return 1; } public double shixian1(C per1,double radius1){ System.out.println(per1.area(radius1)); return 2; } public String shixian2(C per2,String color){ System.out.println(per2.setcolor(color)); return "hehe"; } } interface A{ public static double pi=3.14; double area(double radius1); } interface B{ String setcolor(String color); } interface C extends A,B{ double volume(double radius,double height); }
运行结果: