java上機 第八周 任務1 繼承的練習 – JAVA編程語言程序開發技術文章

[java]
/* 
* 程序頭部註釋開始   
* 程序的版權和版本聲明部分   
* Copyright (c) 2011, 煙臺大學計算機學院學生   
* All rights reserved.   
* 文件名稱:繼承的練習                           
* 作    者:薛廣晨                               
* 完成日期:2012  年 10 月  17  日   
* 版 本號:x1.0            
   
* 對任務及求解方法的描述部分   
* 輸入描述:  
* 問題描述: (1)封裝一個People類型,具有height和weight屬性,具有speakHello、averageHeight、averageWeight功能。
 
       (2)封裝一類ChinaPeople類型是People的子類,新增chinaMartial功能,override超類的speakHello、averageHeight、averageWeight功能。
 
(3)封裝一類AmericanPeople類型是People的子類,新增AmericanBoxing功能,override超類的speakHello、averageHeight、averageWeight功能。
 
(4)封裝一類BeijingPeople類型是ChinaPeople的子類,新增BeijingOpera功能,override超類的speakHello、averageHeight、averageWeight功能。
 
(5)用一個程序執行入口Test測試上述對象
* 程序輸出:   
* 程序頭部的註釋結束 
*/ 
//People類 
 
package xgc; 
 
public class People { 
    private double height; 
    private double weight; 
     
    public People() 
    { 
        this.height = 10; 
        this.weight = 10; 
    } 
     
    public People(double height, double weight) 
    { 
        this.height = height; 
        this.weight = weight; 
    } 
     
    public double getHeight() { 
        return height; 
    } 
    public void setHeight(double height) { 
        this.height = height; 
    } 
    public double getWeight() { 
        return weight; 
    } 
    public void setWeight(double weight) { 
        this.weight = weight; 
    } 
     
    public void speakHello() 
    { 
        System.out.println("Hello"); 
    } 
     
    public void averageHeight() 
    { 
    } 
     
    public void averageWeight() 
    { 
    } 

 
//ChinaPeople 
package xgc; 
 
public class ChinaPeople extends People { 
     
    public void chinaMartial() 
    { 
        System.out.println("ChinaPeople 太極 very good"); 
    } 
     
    public void speakHello() 
    { 
        System.out.println("ChinaPeople 說 你好"); 
    } 
     
    public void averageHeight() 
    { 
        setHeight(1.7);   
        System.out.println("ChinaPeople averageHeight = "+ getHeight()); 
    } 
     
    public void averageWeight() 
    { 
        setWeight(70);   
        System.out.println("ChinaPeople averageWeight = "+ getWeight()); 
    } 
 

 
//AmericanPeople 
package xgc; 
 
public class AmericanPeople extends People{ 
    public void AmericanBoxing(){ 
        System.out.println("AmericanPeople Boxing very good"); 
    } 
 
    public void speakHello() 
    { 
        System.out.println("AmericanPeople say Hello"); 
    } 
     
    public void averageHeight() 
    { 
        setHeight(1.8);   
        System.out.println("AmericanPeople averageHeight = "+ getHeight());  
    } 
     
    public void averageWeight() 
    { 
        setWeight(75);   
        System.out.println("AmericanPeople averageWeight = "+ getWeight());  
    } 

 
//BeijingPeople 
package xgc; 
 
public class BeijingPeople extends ChinaPeople{ 
    public void BeijingOpera(){ 
        System.out.println("BeijingOpera very good"); 
    } 
     
    public void speakHello() 
    { 
        System.out.println("BeijingPeople 說 你好"); 
    } 
     
    public void averageHeight() 
    { 
        setHeight(1.75);   
        System.out.println("ChinaPeople averageHeight = "+ getHeight()); 
    } 
     
    public void averageWeight() 
    { 
        setWeight(73);   
        System.out.println("AmericanPeople averageWeight = "+ getWeight()); 
    } 
 

 
//測試類Test 
package xgc; 
 
public class Test { 
 
    /**
     * @param args
     */ 
    public static void main(String[] args) { 
        // TODO Auto-generated method stub 
        ChinaPeople cp = new ChinaPeople(); 
        cp.speakHello(); 
        cp.chinaMartial(); 
        cp.averageHeight(); 
        cp.averageWeight(); 
        System.out.println(); 
         
        AmericanPeople ap = new AmericanPeople(); 
        ap.speakHello(); 
        ap.AmericanBoxing(); 
        ap.averageHeight(); 
        ap.averageWeight(); 
        System.out.println(); 
         
        BeijingPeople bp = new BeijingPeople(); 
        bp.speakHello(); 
        bp.BeijingOpera(); 
        bp.averageHeight(); 
        bp.averageWeight(); 
        System.out.println(); 
 
    } 
 

運行結果:

 

發佈留言