1. 程式人生 > >java寵物練習

java寵物練習

super() ng2 auto 使用 性別 isa extend happy line

先定一個寵物的抽象類,把所有共有的屬性方法放到次類中,用於子類去繼承實現。

package backing2;

abstract public class pet {
  private String name;
  private String sex;
  private int age;
  private int helthy;
  private int happy;
  private int hungry;
  private boolean alive;
  
public abstract void play();
public abstract void yiyuango();
public abstract void youeryuango();
public abstract void youlechanggo();
public abstract void eat();


public pet(String name, String sex) {
	super();
	this.name = name;
	this.sex = sex;
	this.age=1;
	this.helthy=100;
	this.happy=80;
	this.hungry=70;
	System.out.println("寵物名字:"+this.name+"\n"+" 性別:"+this.sex+"\t"+"年齡"+this.age+"健康值"+this.helthy+"饑餓值"+this.hungry);
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public String getSex() {
	return sex;
}
public void setSex(String sex) {
	this.sex = sex;
}
public int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}
public int getHelthy() {
	return helthy;
}
public void setHelthy(int helthy) {
	this.helthy = helthy;
}
public int getHappy() {
	return happy;
}
public void setHappy(int happy) {
	this.happy = happy;
}
public int getHungry() {
	return hungry;
}
public void setHungry(int hungry) {
	this.hungry = hungry;
}
public boolean isAlive() {
	return alive;
}
public void setAlive(boolean alive) {
	this.alive = alive;
}
}

  再分別定義寵物繼承小類,子類實現重寫父類的所有方法,註意:方法的權限修飾符不能小於父類的權限修飾符,在子類中再定義一個方法在每次使用每個方法的時候去判斷一下寵物的幾個屬性值

package backing2;

public class qie extends pet {
	public qie(String name, String sex) {
		super(name, sex);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void play() {
		System.out.println("小企鵝蹦蹦跳跳的玩去了");
		this.setHappy(getHappy()+5);
		check();
		System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饑餓值"+this.getHungry());
	}

	@Override
	public void yiyuango() {
		System.out.println("小企鵝蔫蔫的去醫院了");
		this.setHelthy(getHelthy()+20);
		check();
		System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饑餓值"+this.getHungry());
	}

	@Override
	public void youeryuango() {
		System.out.println("小企鵝高高興興去上學");
		this.setHungry(getHungry()+10);
		this.setHappy(getHappy()+5);
		check();
		System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饑餓值"+this.getHungry());
	}

	@Override
	public void youlechanggo() {
		System.out.println("小企鵝歡歡樂樂的去遊樂場");
		this.setHappy(getHappy()+10);
		check();
		System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饑餓值"+this.getHungry());
	}

	@Override
	public void eat() {
		System.out.println("小企鵝吃魚<。)#)))≦了");
		this.setHungry(getHungry()-30);
		System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饑餓值"+this.getHungry());
	}
	 public void check(){
	    	
	    	if(this.getHappy()<5||this.getHungry()>90||this.getHelthy()<5){
	    		System.out.println("您的寵物該去醫院了") ;  
	    		}
	    	this.getAge();
	    }
}

package backing2;

public class tuzi extends pet {

	public tuzi(String name, String sex) {
		super(name, sex);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void play() {
		check();
		System.out.println("小兔子蹦蹦跳跳的玩去了");
		this.setHappy(getHappy()+5);
		check();
		System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饑餓值"+this.getHungry());
	}

	@Override
	public void yiyuango() {
		check();
		System.out.println("小兔子蔫蔫的去醫院了");
		this.setHelthy(getHelthy()+20);
		check();
		System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饑餓值"+this.getHungry());
	}

	@Override
	public void youeryuango() {
		check();
		System.out.println("小兔子高高興興去上學");
		this.setHungry(getHungry()+10);
		this.setHappy(getHappy()+5);
		check();
		System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饑餓值"+this.getHungry());
	}

	@Override
	public void youlechanggo() {
		check();
		System.out.println("小兔子歡歡樂樂的去遊樂場");
		this.setHappy(getHappy()+10);
		check();
		System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饑餓值"+this.getHungry());
	}

	@Override
	public void eat() {
		check();
		System.out.println("小兔子吃蘿蔔了");
		this.setHungry(getHungry()-30);
		System.out.println("健康值"+this.getHelthy()+"心情值"+this.getHappy()+"饑餓值"+this.getHungry());
		
	}
    public void check(){
    	
    	if(this.getHappy()<5||this.getHungry()>90||this.getHelthy()<5){
    		System.out.println("您的寵物該去醫院了") ;  
    		}
    	this.getAge();
    }
}

 最後定義一個運行類,運行類裏定義一個靜態方法用來輸出提示信息 

package backing2;

import java.util.Scanner;

public class testpet {

	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		System.out.println("請輸入您要選擇的寵物");
		System.out.println("1--兔子");
		System.out.println("2--企鵝");
		pet p=null;
		String s1=s.nextLine();
		if("1".equals(s1)){
			System.out.print("請輸入您的寵物名字");
			String s2=s.nextLine();
			System.out.print("請輸入您的寵物性別");
			String s3=s.nextLine();
			p=new tuzi(s2,s3);
		}else if("2".equals(s1)){
			System.out.print("請輸入您的寵物名字");
			String s2=s.nextLine();
			System.out.print("請輸入您的寵物性別");
			String s3=s.nextLine();
			p=new tuzi(s2,s3);
		}else{
			System.out.println("其他寵物正在孵化中。。");
		}
		boolean falg=true;
		while(falg){
			testpet.tishi();
			String s5=s.nextLine();
			if("1".equals(s5)){
				p.play();
			}else if("2".equals(s5)){
				p.yiyuango();
			}else if("3".equals(s5)){
				p.youlechanggo();
			}else if("4".equals(s5)){
				p.eat();
			}else if("5".equals(s5)){
				p.youeryuango();
			}else if("exit".equals(s5)){
				falg=false;
			}else{
				System.out.println("請按要求來好嗎");
			}
		}s.close();
	}
   public static void tishi(){
	   System.out.println("跟寵物進行友好的交流。。");
	   System.out.println("1--與寵物玩");
	   System.out.println("2--陪寵物去醫院");
	   System.out.println("3--陪寵物去遊樂場");
	   System.out.println("4--陪寵物去吃飯飯");
	   System.out.println("5--送寵物去幼兒園");
	   System.out.println("exit--退出");
	   
   }
}

  

java寵物練習