码迷,mamicode.com
首页 > 其他好文 > 详细

继承 2—people

时间:2016-05-23 00:27:36      阅读:326      评论:0      收藏:0      [点我收藏+]

标签:

 

 

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

标签:

原文地址:http://www.cnblogs.com/yg6405816/p/5518316.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!