匿名類

class Cubic
{
    double getCubic(int n)
    {
        return 0;
    }
}
abstract class Sqrt
{
    public abstract double getSqrt(int x);
}
class A
{
    void f(Cubic cubic)
    {
        double result = cubic.getCubic(3);
        System.out.println(result);
    }
}
public class ExamNonclass
{
    public static void main(String args[])
    {
        A a = new A();
        a.f(new Cubic()
                {
                    double getCubic(int n)
                    {
                        return n*n*n;
                    }
                }
             );

        Sqrt ss = new Sqrt()
                 {
                     public double getSqrt(int x)
                     {
                         return Math.sqrt(x);
                     }
                 };
        double m = ss.getSqrt(5);
        System.out.println(m);
    }
}

發佈了54 篇原創文章 · 獲贊 4 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章