实验八 接口与实现接口的类

声明圆锥体类,实现Area和Volume接口,计算表面积和体积,按体积比较大小

一.程序代码

public class yuanzhui extends Rectangle implements Area,Volume {

private double radius; 

private double length;

private double height;

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

{

this.radius = radius;

this.length = length;

this.height = height;

}

public yuanzhui()

{

this(0,0,0);

}

public double area() //计算圆锥的表面积,实现Area接口中的抽象方法

{

return Math.PI*this.radius*this.length+Math.PI*this.radius* this.radius ;

}

public double volume() //计算圆锥的体积,实现Volume接口中的抽象方法

{

return Math.PI * this.radius * this.radius * this.height/3;

}

public String toString()

{

return "一个圆锥,半径"+this.radius+",高"+this.height+",斜边,"+this.length+"表面积为"+this.area()+",体积为"+this.volume();

}

public static void main(String args[])

{

System.out.println(new yuanzhui(10,5,3).toString());

}

}

二.实验心得

        通过本次实验的调试,使我清楚的认识到接口在JAVA编程语言中是一个抽象类型,是抽象方法的集合,

接口通常以interface来声明。一个类通过继承接口的方式从而来继承接口的抽象方法。并且接口并不是类,

编写接口的方式和类很相似,但是它们属于不同的概念。类描述对象的属性和方法。接口则包含类要实现的方法。

猜你喜欢

转载自www.cnblogs.com/java-wyw/p/10888747.html