继承 2—people
Posted 曦~妍
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了继承 2—people相关的知识,希望对你有一定的参考价值。
19.创建如下三个类:(People类中的三个方法分别输出一些信息,ChinaPeople
和AmericanPeople类重写父类的三个方法)。
1 package text; 2 3 public class People { 4 private double height; 5 private double weight; 6 public double getHeight() { 7 return height; 8 } 9 public void setHeight(double height) { 10 this.height = height; 11 } 12 public double getWeight() { 13 return weight; 14 } 15 public void setWeight(double weight) { 16 this.weight = weight; 17 } 18 //方法 19 //打招呼 20 public void speakHello() 21 { 22 System.out.println("Hello!"); 23 } 24 public void averageheight(double highestheight,double minheight) 25 { 26 height=( highestheight-minheight)/2; 27 System.out.println("平均身高为:"+height); 28 } 29 public void averageweight(double highestweight,double lowestweight) 30 { 31 weight=( highestweight-lowestweight)/2; 32 System.out.println("平均身高为:"+weight); 33 } 34 public static void main(String[] args) { 35 // TODO 自动生成的方法存根 36 People p=new People(); 37 p.speakHello(); 38 p.averageheight(190.0,155.0); 39 p.averageweight(120.0,90.0); 40 } 41 42 }
1 package text; 2 //extends 继承 3 //Object 是所有类的父类 4 public class Chinapeople extends People { 5 public void gongFu() 6 { 7 System.out.println("坐如钟,站如松,睡如弓"); 8 } 9 //重写 覆盖 10 public void speakHello() 11 { 12 System.out.println("你好!"); 13 } 14 public static void main(String[] args) { 15 16 Chinapeople cp=new Chinapeople(); 17 cp.setHeight(100); 18 cp.setWeight(50); 19 cp.speakHello(); 20 cp.gongFu(); 21 22 } 23 }
1 package text; 2 3 public class AmericanPeople extends People { 4 public void Boxing() 5 { 6 System.out.println("直拳,勾拳"); 7 } 8 public void SpeakHello() 9 { 10 System.out.println("Hello"); 11 } 12 public static void main(String[] args) { 13 // TODO 自动生成的方法存根 14 AmericanPeople ap=new AmericanPeople(); 15 ap.setHeight(180); 16 ap.setWeight(100); 17 ap.SpeakHello(); 18 ap.Boxing(); 19 } 20 21 }
以上是关于继承 2—people的主要内容,如果未能解决你的问题,请参考以下文章