類別檔: Shape.java
public interface Shape { public double x=5, y=10; public double r=10; public void draw(); }類別檔: Circle.java
public class Circle implements Shape { public void draw() { System.out.println("圓面積 : " + r * r * Math.PI); } }類別檔: Rectangle.java
public class Rectangle implements Shape{ public void draw() { System.out.println("矩形面積 :" + x * y); } }類別檔: ShapeTest.java
public class ShapeTest { public static void main(String[] args) { Shape obj = new Rectangle(); obj.draw(); obj = new Circle(); obj.draw(); } }執行結果 :
沒有留言:
張貼留言