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

TOP

返回列表