public abstract class TestTable { public abstract float getArea(float r);
}
public class A extends TestTable { // Rect public float getArea(float r) { float area = r * r; return area; } }
public class B extends TestTable { // Circle public float getArea(float r) { float area = (float) (Math.PI * r * r); return area; } }
------解决方案-------------------- 这么简单的作业都跑上论坛来发帖 唉。。。
------解决方案-------------------- 仅供参考,希望还是自己试着写写 public abstract class Shape{ public abstract float getArea(float r);
}
public class Rect extends Shape{ public float getArea(float r) { float area = r * r; return area; } }
public class Circle extends Shape{ public float getArea(float r) { float area = (float) (Math.PI * r * r); return area; } } public class Test { public static void main(String[] args){ Rect rt=new Rect(); System.out.println(rt.getArea(12)); Circle cl=new Circle(); System.out.println(cl.getArea(12)); }
}
------解决方案-------------------- public interface Shape { double Getarea(double length);
} public class Circle implements Shape { private final double PI = 3.14;
public double Getarea(double length) { return PI * length * length / 4; }
} public class Rect implements Shape { public double Getarea(double length) { return length * length; } } public class Test { public static void main(String[] args) { Rect Jie = new Rect(); Circle S = new Circle(); System.out.println(Jie.Getarea(2)); System.out.println(S.Getarea(6)); } }