父类作为方法的形参以及父类作为方法返回值

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

测试运行结果:

猜你喜欢

转载自www.cnblogs.com/Dean-0/p/11203434.html