zoukankan      html  css  js  c++  java
  • 继承 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 }

  • 相关阅读:
    qt忙等与非忙等
    获得文件路径 _pgmptr, _makepath, _splitpath
    RGB2YCbCr RGB2Gray
    qt Cannot connect creator comm socket /tmp/qt_temp.S26613/stub-socket: No such
    64位Ubuntu系统安装OpenCV 2.4.x+ffmpeg 完美解决方案
    vim按下ctrl+s僵死
    win32程序应用mfc库
    error LNK2005: _DllMain@12 已经在 dllmain.obj 中定义
    JavaScript中的浅拷贝和深拷贝
    Set和Map
  • 原文地址:https://www.cnblogs.com/yg6405816/p/5518316.html
Copyright © 2011-2022 走看看