package iceyung;
/**
* @description 父类
* @author iceyung
*
*/
public class People {
protected double weight;
protected double height;
public void speakHello(){
System.out.println("yayaya");
}
public void averageHeight(){
height = 173;
System.out.println("average height :"+height);
}
public void averageWeight(){
weight = 70;
System.out.println("average weight :"+weight);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
package iceyung;
/**
* @description 中国人,继承父类
* @author iceyung
*
*/
public class ChinaPeople extends People{
/**
* 重写父类的speakHello方法
*/
@Override
public void speakHello() {
System.out.println("你好");
}
/**
* 重写
*/
@Override
public void averageHeight() {
height = 168;
System.out.println("中国人的平均身高为:"+ height);
}
/**
* 重写
*/
@Override
public void averageWeight() {
weight = 65;
System.out.println("中国人的平均体重为:"+ weight);
}
/**
* 给子类添加新的特性
*/
public void chinaGongfu(){
System.out.println("中国功夫!");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
package iceyung;
/**
* @description 美国人,继承父类
* @author iceyung
*
*/
public class AmericanPeople extends People{
/**
* 重写父类的speakHello方法
*/
@Override
public void speakHello() {
System.out.println("How do you do ");
}
/**
* 重写
*/
@Override
public void averageHeight() {
height = 176;
System.out.println("American's average height:"+ height);
}
/**
* 重写
*/
@Override
public void averageWeight() {
weight = 75;
System.out.println("American's average weight:"+ weight);
}
/**
* 给子类添加新的特性
*/
public void americanBoxing(){
System.out.println("americanBoxing!");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
package iceyung;
/**
* @description 北京人,中国人的子类
* @author iceyung
*
*/
public class BeijingPeople extends ChinaPeople{
/**
* 重写
*/
@Override
public void averageHeight() {
height = 172.5;
System.out.println("北京人的平均身高为:"+height);
}
/**
* 重写
*/
@Override
public void averageWeight() {
weight = 70;
System.out.println("北京人的平均体重为:"+weight);
}
/**
* 添加新特性
*/
public void beijingOpera(){
System.out.println("京剧!");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
练习知识3:掌握重写的意义以及怎样使用super关键字
package iceyung;
/**
* @description 中国人,继承父类
* @author iceyung
*
*/
public class ChinaPeople extends People{
/**
* 重写父类的speakHello方法,重写是为了重新赋予该方法子类的实现
*/
@Override
public void speakHello() {
System.out.println("你好");
}
/**
* 重写,super关键字用来使用父类的参数或者方法,调用模式如下
*/
@Override
public void averageHeight() {
height = 168;
System.out.println("中国人的平均身高为:"+ height);
System.out.println("people 的标准身高是:" + super.height);
System.out.print("people 的sayHello:" );
super.speakHello();
}
/**
* 重写
*/
@Override
public void averageWeight() {
weight = 65;
System.out.println("中国人的平均体重为:"+ weight);
System.out.println("people 的标准体重是:" + super.weight);
}
/**
* 给子类添加新的特性
*/
public void chinaGongfu(){
System.out.println("中国功夫!");
}
}