返回列表 發帖
  1. package practice;

  2. public class practice02 {

  3.         public static void main(String[] args) {
  4.                 square q1=new square("方形1號","綠色",5);
  5.                 tri t1=new tri("三角形1號","粉紅色",7,5);
  6.                 q1.show();
  7.                 q1.calarea();
  8.                 t1.show();
  9.                 t1.calarea();
  10.         }

  11. }
  12. abstract class shape{
  13.         String name, color;
  14.         shape(String a,String b){
  15.                 name=a;
  16.                 color=b;
  17.         }
  18.         void show(){
  19.                 System.out.print("物件名稱:"+name+"\n"+"顏色:"+ color+"\n");
  20.         }
  21.         abstract void calarea();
  22. }
  23. class square extends shape
  24. {
  25.         int x;
  26.         square(String n,String c,int x){
  27.                 super(n,c);
  28.                 this.x=x;
  29.         }
  30.         void calarea(){
  31.                 System.out.print("面積為:"+(x*x)+"平方公分\n");
  32.         }
  33. }
  34. class tri extends shape
  35. {
  36.         float x;
  37.         float y;
  38.         tri(String n,String c,float x,float y){
  39.                 super(n,c);
  40.                 this.x=x;
  41.                 this.y=y;
  42.         }
  43.         void calarea(){
  44.                 System.out.print("面積為:"+(x*y)*0.5+"平方公分\n");
  45.         }
  46. }
複製代碼

TOP

返回列表