類別檔: Shape.java
public class Shape { public double x=5, y=10; public double r=10; public void draw() { System.out.println("圖形繪畫"); } }類別檔: Circle.java
public class Circle extends Shape { public void draw() { System.out.println("圓面積 : " + r * r * Math.PI); } }類別檔: Rectangle.java
public class Rectangle extends 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(); } }執行結果 :
沒有留言:
張貼留言