返回列表 發帖
  1. package ch76;
  2. public class ch76
  3. {
  4.         public static void main(String args[])
  5.         {
  6.             Square sq=new Square("方形體","綠色",5);
  7.             sq.showName();
  8.             sq.showColor();
  9.         }
  10. }
  11. abstract class Shape
  12. {
  13.     String name,color;
  14.     Shape(String name,String color)
  15.     {
  16.             this.name=name;
  17.             this.color=color;
  18.     }
  19.     void showName()
  20.     {
  21.             System.out.println("物件名稱為"+name);
  22.     }
  23.     void showColor()
  24.     {
  25.             System.out.println("物件顏色為"+color);
  26.     }
  27.     abstract void calArea();
  28. }
  29. class Square extends Shape
  30. {
  31.         int x;
  32.         Square(String name,String color,int x)
  33.         {
  34.         super(name,color);
  35.         this.x=x;
  36.         }
  37.     void calArea()
  38.     {
  39.             System.out.println("面積為:"+x*x);
  40.     }
  41. }
  42. class Tri extends Shape
  43. {
  44.         double x,y;
  45.         Tri(String name,String color,double x,double y)
  46.         {
  47.         super(name,color);
  48.         this.x=x;
  49.         this.y=y;
  50.         }
  51.     void calArea()
  52.     {
  53.             System.out.println("面積為:"+x*y/2);
  54.     }
  55. }
複製代碼

TOP

返回列表