读书人

接口

发布时间: 2012-12-26 14:39:28 作者: rapoo

接口。

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

?

?

?

?

?

?

?

?

?

?

?

?

读书人网 >编程

热点推荐