实验八

1.程序

package DYL;

interface Area

{

public abstract double area();

}

interface Volume

{

public abstract double volume();

}

public class yuanzhui extends Object implements Area,Volume

{

private double height;

private double raduis;

private double length;

public yuanzhui(double height,double raduis,double length)

{

this.height=height;

this.raduis=raduis;

this.length=length;

}

public double area()

{

return (Math.PI*this.raduis*this.length+Math.PI*this.raduis*2);

}

public double volume()

{

return this.height*Math.PI*this.raduis*2/3;

}

public static double max(yuanzhui X1,yuanzhui X2)

{

System.out.print("体积较大的圆锥为:");

if(X1.volume()>X2.volume())

return X1.volume();

else

return X2.volume();

}

 

public static void main(String[] args) {

yuanzhui YZ=new yuanzhui(1,4,6);

System.out.println("圆锥1的表面积为:"+YZ.area());

System.out.println("圆锥1的体积为:"+YZ.volume());

yuanzhui yz=new yuanzhui(2,6,1);

System.out.println("圆锥2的表面积为:"+yz.area());

System.out.println("圆锥2的体积为:"+yz.volume());

System.out.println("体积较大的圆锥为:"+Math.max(yz.volume(),YZ.volume()));

}

}

2.心得

一个类可以实现多个接口,类通过使用关键字声明自己实现一个或多个接口,如果一个非抽象类实现了某个接口,那么这个类必须重写该接口的所有方法。

现在我来概括一下抽象类和接口的区别:

1关键字 abstract class      

               interface

2 组成      构造方法,普通方法,抽象方法,static方法,常量,变量  。        

                抽象方法和全局常量 public static final 

3子类使用 class子类extends 抽象类

                 class子类 implements 接口

4关系  抽象类可以实现多个接口

           接口不能继承抽象类,却可以继承多个父类接口

5权限 可以使用各种权限 

          只能够使用public权限

 

posted @ 2019-05-20 08:23  辰御  阅读(111)  评论(0编辑  收藏  举报