返回列表 發帖
本帖最後由 洪翊展 於 2019-9-16 21:05 編輯
  1. public class Ch71 {
  2.         Square s1=new Square("方形一號","綠色",5);
  3.         s1=showName();
  4.         s1=showColor();
  5.         s1=calArea();
  6.         Tri t1=new Tri("三角形一號","白色",5,7);
  7.         t1=showName();
  8.         t1=showColor();
  9.         t1=calArea();
  10.        

  11. }
  12. abstract class Shape{
  13.         String name;
  14.         String color;
  15.         Shape(String n,String c)
  16.         {
  17.                 name=n;
  18.                 color=c;
  19.         }
  20.         void showName()
  21.         {
  22.                 System.out.println("物件名稱 "+name);
  23.         }
  24.         void showColor()
  25.         {
  26.                 System.out.println("顏色 "+color);
  27.         }
  28.         abstract void calArea();
  29. }
  30. class Square extends Shape{
  31.        
  32.         int j;
  33.         Square(String n,String c,int j){
  34.           super(n,c);

  35.                 this.j=j;
  36.           
  37.         }
  38.         void calArea(){
  39.                 System.out.println("面積為 "+(j*j)+"平方公分");
  40.         }
  41.        
  42.        
  43. }
  44. class Tri extends Shape{
  45.        
  46.         double x,y;
  47.         Tri(String n,String c,double x,double y){
  48.           super(n,c);
  49.         this.x=x;
  50.                 this.y=y;
  51.           
  52.         }
  53.         void calArea(){
  54.                 System.out.println("面積為 "+(x*y)/2+"平方公分");
  55.         }
  56.        
  57.        
  58. }
複製代碼

TOP

返回列表