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 }

  • 相关阅读:
    算法竞赛入门经典习题2-3 韩信点兵
    ios入门之c语言篇——基本函数——5——素数判断
    ios入门之c语言篇——基本函数——4——数值交换函数
    144. Binary Tree Preorder Traversal
    143. Reorder List
    142. Linked List Cycle II
    139. Word Break
    138. Copy List with Random Pointer
    137. Single Number II
    135. Candy
  • 原文地址:https://www.cnblogs.com/yg6405816/p/5518316.html
Copyright © 2011-2022 走看看