📄 globe2.java
字号:
//【例4.2】 用接口实现多重继承。
public class Globe2 implements SolidGraphics2 //球类,只有声明与Globe1不同,其他都一样
{
private double radius; //半径
public Globe2(double radius)
{
this.radius = radius;
}
public Globe2()
{
this(0);
}
public double area() //计算球的表面积,实现PlaneGraphics2接口中的抽象方法
{
return 4 * Math.PI * this.radius * this.radius;
}
public final double perimeter() //虽然球没有周长的概念,但必须实现接口中的抽象方法
{
return 0;
}
public double volume() //计算球的体积,实现SolidGraphics1接口中的抽象方法
{
return Math.PI * this.radius * this.radius * this.radius * 4 / 3;
}
public void print()
{
System.out.println("一个球,半径为 "+this.radius+",表面积为 "+this.area()+",体积为 "+this.volume());
}
public static void main(String args[])
{
Globe2 g1 = new Globe2(10);
g1.print();
}
}
/*
程序运行结果如下:
一个球,半径为 10.0,表面积为 1256.6370614359173,体积为 4188.790204786391
*/
/*
程序错误:
1、虽然球没有周长的概念,但必须实现接口中的抽象方法,否则,会产生编译错:
Globe1 is not abstract and does not override abstract method perimeter() in PlaneGraphics2
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -