题目如下
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());
}
}
运行结果
总结
1.要注意返回值的问题不然容易报错.
2.要了解继承的基本思想要为以后学习IO流和集合打基础.