本帖最後由 洪翊展 於 2019-9-16 21:05 編輯
- public class Ch71 {
- Square s1=new Square("方形一號","綠色",5);
- s1=showName();
- s1=showColor();
- s1=calArea();
- Tri t1=new Tri("三角形一號","白色",5,7);
- t1=showName();
- t1=showColor();
- t1=calArea();
-
- }
- abstract class Shape{
- String name;
- String color;
- Shape(String n,String c)
- {
- name=n;
- color=c;
- }
- void showName()
- {
- System.out.println("物件名稱 "+name);
- }
- void showColor()
- {
- System.out.println("顏色 "+color);
- }
- abstract void calArea();
- }
- class Square extends Shape{
-
- int j;
- Square(String n,String c,int j){
- super(n,c);
- this.j=j;
-
- }
- void calArea(){
- System.out.println("面積為 "+(j*j)+"平方公分");
- }
-
-
- }
- class Tri extends Shape{
-
- double x,y;
- Tri(String n,String c,double x,double y){
- super(n,c);
- this.x=x;
- this.y=y;
-
- }
- void calArea(){
- System.out.println("面積為 "+(x*y)/2+"平方公分");
- }
-
-
- }
複製代碼 |