首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

接口

2012-12-26 
接口。1.接口的作用:方法声明和方法实现相分离,每实现接口的类根据自身的实际情况,给出实际实现方法;?//实

接口。

1.接口的作用:方法声明和方法实现相分离,每实现接口的类根据自身的实际情况,给出实际实现方法;

?

//实现多个接口,实现平面图形接口和立体图形接口public class Globe1 implements PlaneGraphics2,SolidGraphics1{private double radius;public Globe1(double radius){this.radius=radius;}public Globe1(){this(0);}public double area(){  //计算表面积,覆盖PlaneGraphics2接口中的抽象方法return 4*Math.PI*this.radius*this.radius;}public double perimeter(){ //虽然球没有周长的概念,也必须覆盖接口中的抽象方法return 0;    }public double volume(){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[]){Globe1 g1=new Globe1(10);g1.print();}}
?

?

?

?

?

?

?

?

?

?

?

?

?

热点排行