Java教程

java封装-猫狗宠物店案例

本文主要是介绍java封装-猫狗宠物店案例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 宠物类(父类):

public class Pet {
    private String name;
    private int health;
    private int 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 < 0 || health > 100) {
			System.out.println("您输入有误!已设置为默认值!");
			health = 60;
		}
		this.health = health;
	}
	public int getLove() {
		return love;
	}
	public void setLove(int love) {
		if (love < 0 || love > 100) {
			System.out.println("您输入有误!已设置为默认值!");
			love = 60;
		}
		this.love = love;
	}

    public void showInfo() {
		System.out.println("名:" + name);
		System.out.println("健康值:" + health);
		System.out.println("亲密度:" + love);
	}

    public Pet() {
		super();
	}
	public Pet(String name, int health, int love) {
		super();
		this.setHealth(health);
		this.setName(name);
		this.setLove(love);
	}
}

猫类(子类):

public class Cat extends Pet{
	private String gender;
	
	public String getgender() {
		return gender;
	}
	public void setgender(String gender) {
		this.gender = gender;
	}

	@Override
	public void showInfo() {
		System.out.println("名:" + super.getName());
		System.out.println("健康值:" + super.getHealth());
		System.out.println("亲密度:" + super.getLove());
		System.out.println("性别:" + gender);
	}
	public Cat() {
		super();
	}
	public Cat(String gender, String name, int health, int love) {
		super(name, health, love);
		this.setgender(gender);
	}
	
}

 狗类(子类):

public class Dog extends Pet{
	private String breed;
	
	public String getBreed() {
		return breed;
	}
	public void setBreed(String breed) {
		this.breed = breed;
	}

	public Dog() {
		super();
	}
	public Dog(String name, int health, int love, String breed) {
		super(name, health, love);
		this.breed = breed;
	}
	
	@Override
	public void showInfo() {
		super.showInfo();
		System.out.println("品种:" + breed);
	}
	public Dog(String name, int health, int love) {
		this(name, health, love, null);
	}
}

 测试类:

public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("欢迎来到宠物店");
		System.out.println("请选择:1.狗2.猫");
		String choice = input.next();
		
		System.out.println("请输入宠物的名字:");
		String name = input.next();
		System.out.println("请输入宠物健康值:");
		int health = input.nextInt();
		System.out.println("请输入宠物亲密度:");
		int love = input.nextInt();
		
		switch (choice) {
		case "1":
			System.out.println("请输入狗的品种:1.柴犬2.拉布拉多");
			int breed = input.nextInt();
			
//			Dog dog = new Dog(name, health, love, breed == 1 ? "柴犬" : "拉布拉多");
//			dog.showInfo();
			Dog dog1 = new Dog(name, health, love);
			dog1.setBreed(breed == 1 ? "柴犬" : "拉布拉多");
			dog1.showInfo();
			break;
		case "2":
			System.out.println("请输入猫的性别:1.公2.母");
			int gender = input.nextInt();
			
			new Cat(gender == 1 ? "公" : "母", name, health, love).showInfo();
			break;
		default:
			System.out.println("输入错误!");
			break;
		}
	}
这篇关于java封装-猫狗宠物店案例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!