java簡易練習(xí)

題目如下

d6d519f82354c18b4e0a9129d47bbec.png

eba4eb8cb97f7989ece851dab510fc9.png
package MyApplication1;

class People{
    public double weight;
    public double height;
    public People(double weight, double height){
        this.weight = weight;
        this.height = height;
    }
    public void speakHello(){
        System.out.println("我是人");
        return;
    }
    public double averageHeight(){
        height=170;
        return height;
    }
    public double averageWeight(){
        height=70;
        return weight;
    }
}
class ChainPeople extends People{
    private String skin;

    public ChainPeople(double weight,double height,String skin){
        super(weight,height);
        this.skin=skin;
    }

    public String getSkin(){
        return skin;
    }
    //覆蓋父類
    public void speakHello() {

        System.out.println("我是中國人");
    }

    public double averageHeight() {
        height=175.0;
        return height;
    }

    public double averageWeight() {
        weight=65.0;
        return  weight;
    }
}
public class MyApplication2 {
    public static void main(String[] args) {
        People p1 = new People(0.0,0.0);
        ChainPeople p2 = new ChainPeople(0.0, 0.0, "yellow");
        p1.speakHello();
        System.out.println("平均身高:"+p1.averageHeight());
        System.out.println("平均體重:"+p1.averageWeight());
        p2.speakHello();
        System.out.println("平均身高:"+p2.averageHeight());
        System.out.println("平均體重:"+p2.averageWeight());
        System.out.println("中國人的皮膚是:"+p2.getSkin());
    }
}

運行結(jié)果

屏幕截圖 2021-05-14 151054.png

總結(jié)

1.要注意返回值的問題不然容易報錯.
2.要了解繼承的基本思想要為以后學(xué)習(xí)IO流和集合打基礎(chǔ).

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容