- package practice;
- public class practice02 {
- public static void main(String[] args) {
- square q1=new square("方形1號","綠色",5);
- tri t1=new tri("三角形1號","粉紅色",7,5);
- q1.show();
- q1.calarea();
- t1.show();
- t1.calarea();
- }
- }
- abstract class shape{
- String name, color;
- shape(String a,String b){
- name=a;
- color=b;
- }
- void show(){
- System.out.print("物件名稱:"+name+"\n"+"顏色:"+ color+"\n");
- }
- abstract void calarea();
- }
- class square extends shape
- {
- int x;
- square(String n,String c,int x){
- super(n,c);
- this.x=x;
- }
- void calarea(){
- System.out.print("面積為:"+(x*x)+"平方公分\n");
- }
- }
- class tri extends shape
- {
- float x;
- float y;
- tri(String n,String c,float x,float y){
- super(n,c);
- this.x=x;
- this.y=y;
- }
- void calarea(){
- System.out.print("面積為:"+(x*y)*0.5+"平方公分\n");
- }
- }
複製代碼 |