用java编的实现宠物店领养宠物的功能

一:

    public class Pet {
     private String name;
     private int health;
     private int love;
     PetShop(String name,int health,int love){
    	 this.name=name;
    	 this.health=health;
    	 this.love=love;
     }
     public String getName(){
    	 return name;
     }
     public void setName(String name){
    	 this.name=name;
     }
     public int getHealth(){
    	 return health;

} public void setHealth(int health){ if(health>100||health<0) { System.out.println("健康值应在0到100之间,默认值是40");} else this.health=health; } public int getLove(){ return love; } public void setLove(int love){ this.love=love; } public void speak(){ System.out.println("Hi,主人,我的名字是"+name+"我的健康值是"+health+"我和主人的亲密度是" +love); }

public void Petshop
	 public static void main(Sting[]args)
	{Scanner input =new Scanner(System.in);
	 System.outprintln("欢迎来到宠物店");
	 //1.输入宠物名称
	 System.out.println("请输入要领养宠物的名字:");
	 String name=input.next();
	 //2.选择宠物
	 System.out.prinln("请输入要领养的宠物类型:(1.狗狗2.企鹅)");
	 swith(input.nextInt())
{    case  1:
	    // 2.1 如果是狗狗
        //2.1.1 选择狗狗的品种
    	System.out.println(“请选择狗狗的品种:(1.聪明的拉布拉多犬” + “ 2.酷酷的雪纳瑞)”);
    	String strain = null;
    	if(input.nextInt() == 1){
	    	strain = ”聪明的拉布拉多犬”;
} else {
    strain = “酷酷的雪纳瑞”;
}

// 2.1.2 创建狗狗对象并赋值
Dog dog = new Dog();
dog.name = name;
dog.strain = strain;
// 2.1.3 输入狗狗信息
dog.print();
break;
case 2:
	//2.2 如果是企鹅
    //2.2.1 选择企鹅性别
    System.out.println(“请选择企鹅的性别:(1.Q仔 2.Q妹)”);
    String sex = null;
    if(input.nextInt() == 1)
        sex = ” Q仔”;
    else 
        sex = “Q妹”
//2.2.2 创建企鹅对象并赋值
    Penguin pgn = new Penguin();
    pgn.name = name;
    pgn.sex = sex;
    //2.2.3 输出企鹅信息
    pgn.print();
}
}
}
class Dog extends Pet{private String breed;
public Dog(String name,int health,int love,Strint breed)
	{super(name,health,love);
this.breed=breed;}
 public String getBreed(){
    return breed;
     }
     public void setBreed(String breed){
    	 this.breed=breed;
     }
public void speak(){
	super.speak();
 System.out.println("我的品种是"+breed);
     }
	 }
	 class Penguin extends pet 
    {private String sex;
public Penguin(String name,int health,int love,Strint breed)
	{super(name,health,love);
this.sex=sex;}
 public String getsex(){
    return sex;
     }
     public void setsex(String sex){
    	 this.sex=sex;
     }
public void speak(){
	super.speak();
 System.out.println("我的性别是"+sex);
     }
	 }


又好几天没有写博客啦,尽量以后少打游戏,多抽点时间写点文章。

这篇代码主要是一个宠物店类,主要用啦面向对象中继承的思想。先定义啦一个基类pet类,Dog类和pengguin类继承啦petl类。这是学java以来第一次用到面向对象的思想。并学到啦与c++不同的地方。继承的关键字extends,和supper()。


二:老师给的代码如下:

petshop

package jsj.java.exam.test05;

import java.util.Scanner;


public class PetShop {

	public static void main(String[] args) {
          System.out.println("欢迎来到宠物商店");
          System.out.println("请输入要领养的宠物的名字");
          Scanner input=new Scanner(System.in);
          String name=input.next();
          System.out.println("请输入你要领养的宠物的类型:(1 狗狗 2 企鹅)");
          int type=input.nextInt();
          Master master=new Master();
          switch(type){
          case 1:System.out.println("请输入狗的品种(1 聪明的拉布拉多犬 2苦苦的雪纳瑞)");
                 int breed=input.nextInt();
                 System.out.println("请输入狗的健康值");
                 int health=input.nextInt();
                 String s="";
                 if(breed==1){
                	 s="聪明的拉布拉多犬";
                 }else{
                	 s="苦苦的雪纳瑞";
                 }
                 Dog dog=new Dog(name,s,health,20);
                 System.out.println("喂养前:");
                 dog.speak();
                
                 master.feed(dog);
                 System.out.println("喂养后:");
                 dog.speak();
                 master.play(dog);
                 System.out.println("与主人玩耍后");
                 dog.speak();
                 
                 break;
          case 2:
        	  System.out.println("请输入企鹅的性别(1Q仔 2Q妹)");
              int sex=input.nextInt();
              System.out.println("请输入企鹅得健康值");
              health=input.nextInt();
              s="";
              if(sex==1){
             	 s="Q仔";
              }else{
             	 s="Q妹";
              }
              Penguin penguin=new Penguin(name, s, health, 30);
              penguin.speak();
              master.feed(penguin);
              System.out.println("喂养后:");
              penguin.speak();
              master.play(penguin);
              System.out.println("与主人玩耍后");
              penguin.speak();
                 
          }
          
	}

}

pet.java

package jsj.java.exam.test05;

public abstract class Pet {
	protected String name;
	protected int health;
	protected int love;

	/**
	 * 
	 * @param name
	 * @param health
	 * @param love
	 */
	public Pet(String name, int health, int love) {
		super();
		this.name = name;
		this.health = health;
		this.love = love;
	}

	/**
	 * 修改名字
	 * @param newName
	 */
	public void setName(String newName) {
		name = newName;
	}

	/**
	 * 取得名字
	 * 
	 * @return
	 */
	public String getName() {
		return name;
	}

	public int getHealth() {
		return health;
	}

	public void setHealth(int health) {
		if (health >= 1 && health <= 100) {
			this.health = health;
		} else {
			this.health = 10;
		}
	}

	public int getLove() {
		return love;
	}

	public void setLove(int love) {
		this.love = love;
	}
    
	
	protected abstract void speak();//宠物自白的行为
	protected abstract void eat();// 吃的行为
}

Penguin.java

package jsj.java.exam.test05;

public  class Penguin extends Pet {
	
	private String sex;
	
	
	public Penguin(String name, String sex, int health, int love) {
	
	    super(name,health,love);

		this.sex = sex;
	
	}

	/**
	 * 宠物的自白
	 */
	public final void speak() {
		System.out.println("宠物的自白");
		System.out.println("我的名字叫" + getName() + "我的健康状况是" + getHealth() + "我的性别是"
				+ sex + "我与主人的亲密度为" + getLove());
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}
	
	
	 //TODO 重写父类的eat方法,健康值增加6
	
	@Override
	protected void eat() {//--1
       super.health=super.health+6;//--1		
	}
	
	//TODO 定义子类特有的方法,游泳swimming
	//输出"正在游泳",健康值减少3,与主人的亲密度增加5

	public void swimming(){//--1
		System.out.println("游泳");
		super.health=super.health-3;//--1
		super.love=super.love+5;//--1
	}

	

}

Dog.java

package jsj.java.exam.test05;
//TODO 将Dog类定义完整
public class Dog extends Pet {//--1

	private String breed;

	public Dog(String name, String breed, int health, int love) {
		//调用父类的构造方法,完成name,health,love属性的初始化
		super(name,health,love);
		this.breed = breed;
	}

	public String getBreed() {
		return breed;
	}

	public void setBreed(String breed) {
		this.breed = breed;
	}

	/**
	 * 宠物的自白
	 */
	public void speak() {
		System.out.println("宠物的自白");
		System.out.println("我的名字叫" + getName() + "我的健康状况是" + getHealth()
				+ "我的品种是" + breed + "我与主人的亲密度为" + getLove());
	}

	

    //TODO 重写父类的eat方法,健康值增加3
	@Override
	protected void eat() {
       super.health=super.health+3;		
	}
	
	
	//TODO 定义子类特有的方法,接飞盘游戏catchFlyingDisk
	//输出"正在玩接飞盘的游戏",健康值减少5,与主人的亲密度增加5
	public void catchFlyingDisk(){
		System.out.println("正在玩接飞盘的游戏");
		super.health=super.health-5;
		super.love=super.love+5;
	}
	

}
Master.java

package jsj.java.exam.test05;

/**
 * 主人类

 */
public class Master {
   
	//喂养动物的方法feed
	public void feed(Pet pet){
		pet.eat();
	}
	
    //TODO 定义与宠物玩耍的方法play
	//如果是狗,则玩接飞盘游戏
	//如果是企鹅,则游泳
	
	public void play(Pet pet){
		if(pet instanceof Dog){
			((Dog) pet).catchFlyingDisk();
		}else if(pet instanceof Penguin){
			((Penguin) pet).swimming();
		}
	}
	
	
}

 






   

你可能感兴趣的:(java,游戏,String,null,Class,input)