首页 > 代码库 > 接口的实现

接口的实现

计算图形面积和周长

interface tuxing{
double PI=3.1415926;
abstract double mianji();
abstract double zhouchang();
}
class yuan implements tuxing{
double r;
yuan(double r){this.r=r;}
public double mianji(){return PI*r*r;}
public double zhouchang(){return PI*2*r;}
}
class juxing implements tuxing{
double aa,bb;
juxing (double aa,double bb){
this.aa=aa; this.bb=bb;
}
public double mianji(){
return aa*bb;
}
public double zhouchang(){
return (aa+bb)*2;
}
}
class sanjiao implements tuxing{
double d,e,f;
sanjiao (double d,double e,double f){
this.d=d; this.e=e;this.f=f;
}
public double mianji(){
double p=(d+e+f)/2.0; return Math.sqrt(p*(p-d)*(p-e)*(p-f));
}
public double zhouchang(){
return d+e+f;
}
}
public class Ex5_14 {
public static void main(String[] args){
yuan o1=new yuan(10);
System.out.println("圆面积:"+o1.mianji()+",周长:"+o1.zhouchang());
juxing o2=new juxing(10,20);
System.out.println("矩形面积:"+o2.mianji()+", 矩形周长:"+o2.zhouchang());
sanjiao o3=new sanjiao(3,4,5);
System.out.println("三角形面积:"+o3.mianji()+", 三角形周长:"+o3.zhouchang());
}
}

技术分享

 

技术分享

运行结果:

圆面积:314.15926,周长:62.831852
矩形面积:200.0, 矩形周长:60.0
三角形面积:6.0, 三角形周长:12.0

 

接口的实现