zoukankan      html  css  js  c++  java
  • Java-继承,多态练习0922-02

    创建如下三个类:(People类中的三个方法分别输出一些信息,ChinaPeople

    和AmericanPeople类重写父类的三个方法)。

    父类:

    package com.lianxi1;
    
    public class People {
        //属性
        protected double height;
        protected double weight;
        public double getHeight() {
            return height;
        }
        public void setHeight(double height) {
            this.height = height;
        }
        public double getWeight() {
            return weight;
        }
        public void setWeight(double weight) {
            this.weight = weight;
        }
        
        //构造方法
        public People(double height, double weight) {
            super();
            this.height = height;
            this.weight = weight;
        }
        public People() {
            super();
        }
        
        //成员方法
        public void speakHello()
        {
            System.out.println("你好");
        }
        public void averageHeight()
        {
            System.out.println("身高:"+height);
        }
        public void averageWeight()
        {
            System.out.println("体重:"+weight);
        }

    子类:

    package com.lianxi1;
    
    public class ChinaPeople extends People {
    
        private String gongfu;
        
        
        public String getGongfu() {
            return gongfu;
        }
    
    
        public void setGongfu(String gongfu) {
            this.gongfu = gongfu;
        }
    
    
        public void chinaGongfu()
        {
            System.out.println("功夫:"+gongfu);
        }
    }
    package com.lianxi1;
    
    public class AmericanPeople extends People {
        
        private String Boxing;
    
        public String getBoxing() {
            return Boxing;
        }
    
        public void setBoxing(String boxing) {
            Boxing = boxing;
        }
    
        public void icanBoxing()
        {
            System.out.println("拳击:"+Boxing);
        }
    }

    测试:

    package com.lianxi1;
    
    public class Test {
    
        public static void main(String[] args) {
            ChinaPeople ch=new ChinaPeople();
            AmericanPeople am=new AmericanPeople();
            ch.setHeight(173);
            ch.setWeight(65);
            ch.setGongfu("降龙十八掌");
            am.setBoxing("勾拳");
            ch.averageHeight();
            ch.averageWeight();
            ch.chinaGongfu();
            am.icanBoxing();
    
        }
    
    }

    结果:

  • 相关阅读:
    Python装饰器实现几类验证功能做法(续)
    10周年整
    年中review
    Javascript 模块化开发上线解决方案
    AMDJS编译工具
    第三方组件接入方案(演示文稿图片)
    代码打包预处理工具
    manifest资源提取工具
    也来山寨一版Flappy Bird (js版)
    2014 todo list
  • 原文地址:https://www.cnblogs.com/tfl-511/p/5898633.html
Copyright © 2011-2022 走看看