返回列表 發帖
  1. public class JPA01 {

  2.         public static void main(String[] args) {
  3.            Square q1=new Square("方形1","綠色",5);
  4.            q1.showName();
  5.            q1.showColor();
  6.            Tri t1=new Tri("三角形1","粉紅色",7,5);
  7.            t1.calName();
  8.            t1.calColor();
  9.         }

  10. }
  11. abstract class shape
  12. {
  13.         String name, color;
  14.         shape(String n, String c)
  15.         {
  16.                 name =n;
  17.                 color =c;
  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 n, String c, int x)
  33.         {
  34.                 super(n, c);
  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

返回列表