zoukankan      html  css  js  c++  java
  • 父类作为方法的形参以及父类作为方法返回值

    1、父类作为方法的形参

    语句:修饰符 父类类型 方法名(){}

    2、父类作为方法返回值

    语句:修饰符 void/返回值类型 方法名(父类类型 形参名){}

    代码例子:

    动物类:

    /**
     * @author Mr.Wang
     * 宠物类
     *
     */
    public abstract class Animals {
        private int health;//健康值
        private int love;//亲密度
        private String name;//名字
        public int getHealth() {
            return health;
        }
        public void setHealth(int health) {
            if(health<0||health>100){
                //System.out.println("健康值应该在0至100之间,默认值为60。");
                this.health=60;
                return;
            }
            this.health = health;
        }
        public int getLove() {
            return love;
        }
        public void setLove(int love) {
            this.love = love;
            
        }
        
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        
        public Animals() {}
        public Animals(int health, int love, String name) {
            if(health<0||health>100){
                System.out.println("健康值应该在0至100之间,默认值为60。");
                this.health=60;
            }else {
                this.health = health;
            }
            this.love = 60;
            this.name = name;
        }
        //宠物自白
        public void print() {
            System.out.println("宠物的自白:");
            System.out.println("我的名字叫"+this.getName()+",健康值是"+this.getHealth()+",初始亲密度为"+this.getLove());
             
        }
        
        public void play() {
        };
        
        public void Bath() {
            System.out.println("主人正在给"+this.getName()+"洗澡");
        }
    }

    企鹅类:

    /**
     * @author Mr.Wang
     * 企鹅类
     *
     */
    public class Penguin extends Animals{
        private String sex;
        
        public Penguin() {}
        public Penguin(int health, int love, String name,String sex) {
            super(health, love, name);
            this.sex = sex;
        }
    
        public String getSex() {
            return sex;
        }
    
        public void setSex(String sex) {
            this.sex = sex;
        }
        
        public void print() {
            super.print();
            System.out.println("我是一只"+this.sex+this.getName());
            
        }
        
        public void play() {    
            System.out.println("主人在跟"+this.getName()+"玩水");
        }
    }

    狗狗类:

    /**
     * @author Mr.Wang
     * 狗狗类
     *
     */
    public class Dog extends Animals{
        private String type;
        
        public Dog() {}
        public Dog(int health, int love, String name,String type) {
            super(health, love, name);
            this.type = type;
        }
        public String getType() {
            return type;
        }
        public void setType(String type) {
            this.type = type;
        }
        public void print() {
            super.print();
            System.out.println("我是一只"+this.type);
            
        }
        
        public void play() {
            System.out.println("主人正在跟"+this.getName()+"玩飞盘");
        }
    }

    主人类:

    public class Master {
        public Animals toPlay(int num) {
            if(num == 1) {
                return new Dog(90,90,"皮蛋","拉布拉多");
            }else {
                return new Penguin(90,90,"小胡","Q仔");
            }
        }
        
        public void toBath(Animals animals) {
            animals.Bath();
        }
        
        
    }

    测试类:

    public class Text03 {
        public static void main(String[] args) {
            
            
            Master master = new Master();
            Animals animals = master.toPlay(1);
            animals.play();
            master.toBath(animals);
            
        }
    }

    测试运行结果:

  • 相关阅读:
    【IoT平台北向API调用】使用Postman调用Https接口
    【LiteOS】LiteOS移植常见问题
    搞IT产品,请谨记Mobile First
    Modelarts与无感识别技术生态总结(浅出版)
    高性能Web动画和渲染原理系列(1)——CSS动画和JS动画
    使用蓝图构建Flask项目目录
    一统江湖的大前端(8)- velocity.js 运动的姿势(上)
    只需两步手把手教你玩转图像识别
    秉承初心,砥砺奋进!华为云助力锦江都城开启云服务时代
    HBase 索引创建
  • 原文地址:https://www.cnblogs.com/Dean-0/p/11203434.html
Copyright © 2011-2022 走看看