• 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();
    
        }
    
    }

    结果:

  • 相关阅读:
    关于背景图片的处理
    node ,npm和nvm 版本的管理
    验证码倒计时
    css效果文字多了就...
    git 的入门使用到团队协作
    ajax模拟获取json
    angularjs入门(二)
    重新学习angularjs--第一篇(入门)
    html的结构-厂子型的布局
    分享html5的一个拖拽手法
  • 原文地址:https://www.cnblogs.com/tfl-511/p/5898633.html
Copyright © 2020-2023  润新知