As a method for the return value of the parent class as the parent class parameter and methods

1, shaped as a parent process parameters

Statement: Modifier type parent class method name () {}

2, as the parent class method returns a value

Statement: Modifier void / return type method name (parent class type parameter name) {}

Code examples:

Animal categories:

/ * * 
 * @Author Mr.Wang 
 * pet class 
 * 
 * / 
public  abstract  class Animals {
     Private  int Health; // health value 
    Private  int Love; // intimacy 
    Private String name; // name 
    public  int getHealth () {
         return Health; 
    } 
    public  void setHealth ( int Health) {
         IF (Health < 0 || Health> 100 ) {
             //System.out.println ( "health value should be between 0 to 100, the default value is 60. A"); 
            the this .health = 60 ;
             return ; 
        } 
        the this .health = Health; 
    } 
    public  int getLove () {
         return Love; 
    } 
    public  void setLove ( int Love) {
         the this .love = Love; 
        
    } 
    
    public String getName () {
         return name; 
    } 
    public  void the setName (String name) {
         the this .name = name; 
    }
    
    public Animals () {}
     public Animals ( int Health, int Love, String name) {
         IF (Health < 0 || Health> 100 ) { 
            the System. OUT .println ( " health value should be between 0 to 100, the default value is 60. A " );
             the this .health = 60 ; 
        } the else {
             the this .health = Health; 
        } 
        the this .love = 60 ;
         the this .name = name; 
    } 
    // pets confession
    public  void Print () { 
        System. OUT .println ( " Pet Confessions: " ); 
        . System OUT .println ( " My name is " + the this .getName () + " , the value is healthy " + the this .getHealth ( ) + " , the initial intimacy of " + the this .getLove ()); 
         
    } 
    
    public  void Play () { 
    }; 
    
    public  void Bath () { 
        . the System OUT .println ( " master is to " +the this .getName () + " bath " ); 
    } 
}

Penguin categories:

/**
 * @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);
        
    }
}

测试运行结果:

 

Guess you like

Origin www.cnblogs.com/Dean-0/p/11203434.html