接口与实现接口的类

朱雀 2021-03-28 13:11 930阅读 0赞

一、源代码:

  1. package 实验8;
  2. public class jiekou {
  3. public static void main (String[] arges){
  4. yuanzhui a=new yuanzhui(2,2,3);
  5. yuanzhui b=new yuanzhui(4,5,6);
  6. System.out.println(a.Area());
  7. System.out.println(b.Area());
  8. System.out.println(a.volume());
  9. System.out.println(b.volume());
  10. System.out.println("体积最大的是:"+Math.max(a.volume(), b.volume()));
  11. }
  12. }
  13. class yuanzhui implements Volume,Area{
  14. protected double r;
  15. protected double l;
  16. protected double h;
  17. public yuanzhui(double r,double l,double h){
  18. this.r=r;
  19. this.l=l;
  20. this.h=h;
  21. }
  22. public double volume(){
  23. return Math.PI*Math.pow(r,2)*h/3;
  24. }
  25. public double Area(){
  26. return Math.PI*this.r*this.r+this.r*this.l;
  27. }
  28. }
  29. interface Volume{
  30. public double volume();
  31. }
  32. interface Area{
  33. public double Area();
  34. }

二、实验结果

16.566370614359172
70.26548245743669
12.566370614359172
100.53096491487338
体积较大的是:100.53096491487338

三、实验总结:

1、因为Java不像C++一样支持多继承,所以Java可以通过实现接口来弥补这个局限。为了声明一个接口,我们使用interface这个关键字

2、接口被用来描述一种抽象。

发表评论

表情:
评论列表 (有 0 条评论,930人围观)

还没有评论,来说两句吧...

相关阅读