interface Computerable
{
public double area();
}
class Rec implements Computerable
{
double a,b;
Rec(double a,double b)
{
this.a = a;
this.b = b;
}
public double area() {
return (a*b);
}
}
class Circle implements Computerable
{
double r;
Circle(double r)
{
this.r = r;
}
public double area() {
return (3.14*r*r);
}
}
class Volume
{
Computerable bottom;
double h;
Volume(Computerable bottom, double h)
{
this.bottom = bottom;
this.h = h;
}
public void changeBottome(Computerable bottom)
{
this.bottom = bottom;
}
public double volume()
{
return (this.bottom.area()*h/3.0);
}
}
public class InterfaceRecall {
public static void main(String[] args)
{
Volume v = null;
Computerable bottom = null;
//借口变量中存放着对对象中实现了该接口的方法的引用
bottom = new Rec(3,6);
System.out.println("矩形的面积是:"+bottom.area());
v = new Volume(bottom, 10);
//体积类实例的volum方法实际上计算的是矩形的体积,下同
System.out.println("棱柱的体积是:"+v.volume());
bottom = new Circle(5);
System.out.println("圆的面积是:"+bottom.area());
v.changeBottome(bottom);
System.out.println("圆柱的体积是:"+v.volume());
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有