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

程序代码:

package shiyan;
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()));
}
}

实验结果:

实验心得:JAVA通过实验接口来弥补不支持多重继承的缺陷,为了声明一个接口,我们使用interface这个关键字。在实验中还有许多不懂得问题,不断调试后才完成的实验,以后还的多加调试程序。

原文地址:https://www.cnblogs.com/Java199-wfx/p/10896903.html