📄 shapeexample2.java
字号:
interface shape //图形接口shape
{ public abstract double area();
public double PI=Math.PI;
}
class tuoyuan implements shape //椭圆形类
{ double a,b;
tuoyuan(double a,double b) //初使化圆椭圆长短轴
{ this.a=a;
this.b=b;
}
tuoyuan(double a) //初使化圆半径
{ this.a=a;
b=a;
}
public double area() //实现面积方法
{ double s=PI*a*b;
return s;
}
}
class yuanzhui //圆锥类
{ shape di; //声明一个接口变量
double h,r;
yuanzhui(shape di,double h)
{ this.di=di;
this.h=h;
}
public double tiji() //圆锥体积计算方法
{ double v=di.area()*h/3;
return v;
}
}
public class shapeExample2
{ public static void main(String args[])
{ shape bottom; //声明接口变量
yuanzhui zhui;
tuoyuan tuo=new tuoyuan(3,3);
System.out.println(tuo.area());
tuoyuan yuan=new tuoyuan(2);
System.out.println(yuan.area());
bottom=new tuoyuan(2,4); //接口变量对象引用
System.out.println(bottom.area());
bottom=new tuoyuan(1);
System.out.println(bottom.area());
zhui=new yuanzhui(bottom,10);
System.out.println(zhui.tiji());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -