一、源代码:
1 package 实验8; 2 3 public class jiekou { 4 public static void main (String[] arges){ 5 yuanzhui a=new yuanzhui(2,2,3); 6 yuanzhui b=new yuanzhui(4,5,6); 7 System.out.println(a.Area()); 8 System.out.println(b.Area()); 9 System.out.println(a.volume());10 System.out.println(b.volume());11 System.out.println("体积最大的是:"+Math.max(a.volume(), b.volume()));12 }13 }14 class yuanzhui implements Volume,Area{15 protected double r;16 protected double l;17 protected double h;18 public yuanzhui(double r,double l,double h){19 this.r=r;20 this.l=l;21 this.h=h;22 }23 public double volume(){24 return Math.PI*Math.pow(r,2)*h/3;25 }26 public double Area(){27 return Math.PI*this.r*this.r+this.r*this.l;28 29 }30 }31 interface Volume{32 public double volume();33 }34 interface Area{35 public double Area();36 }
二、实验结果
16.566370614359172
70.2654824574366912.566370614359172100.53096491487338体积较大的是:100.53096491487338三、实验总结:
1、因为Java不像C++一样支持多继承,所以Java可以通过实现接口来弥补这个局限。为了声明一个接口,我们使用interface这个关键字
2、接口被用来描述一种抽象。