⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shapeexample2.java

📁 掌握抽象类、接口的基本设计和应用方法。掌握上转型对象和接口回调的应用方法。 题一:抽象类与上转型对象 题二:接口与接口回调
💻 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 + -