shapeexample2.java
来自「掌握抽象类、接口的基本设计和应用方法。掌握上转型对象和接口回调的应用方法。 题」· Java 代码 · 共 48 行
JAVA
48 行
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 + =
减小字号Ctrl + -
显示快捷键?