- package ch76;
- public class ch76
- {
- public static void main(String args[])
- {
- Square sq=new Square("方形體","綠色",5);
- sq.showName();
- sq.showColor();
- }
- }
- abstract class Shape
- {
- String name,color;
- Shape(String name,String color)
- {
- this.name=name;
- this.color=color;
- }
- void showName()
- {
- System.out.println("物件名稱為"+name);
- }
- void showColor()
- {
- System.out.println("物件顏色為"+color);
- }
- abstract void calArea();
- }
- class Square extends Shape
- {
- int x;
- Square(String name,String color,int x)
- {
- super(name,color);
- this.x=x;
- }
- void calArea()
- {
- System.out.println("面積為:"+x*x);
- }
- }
- class Tri extends Shape
- {
- double x,y;
- Tri(String name,String color,double x,double y)
- {
- super(name,color);
- this.x=x;
- this.y=y;
- }
- void calArea()
- {
- System.out.println("面積為:"+x*y/2);
- }
- }
複製代碼 |