java简易练习

题目如下

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());
    }
}

运行结果

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

总结

1.要注意返回值的问题不然容易报错.
2.要了解继承的基本思想要为以后学习IO流和集合打基础.

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容