package Month.JUL.JUL12; public class Pet { private String name; private int age; private int health; private int love; public int getHealth() { return health; } public void setHealth(int health) { 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } // 宠物自白 public void show() { System.out.println("Pet name: " + the this .getName () + " , Age: " + the this .getAge () + " , the health value: " + the this .getHealth () + " , the degree of intimacy: " + the this .getLove ()); } }
package Month.JUL.JUL12; //子类 狗狗类 public class Dog extends Pet { private String pinZhong; public String getPinZhong() { return pinZhong; } public void setPinZhong(String pinZhong) { this.pinZhong = pinZhong; } public void show() { super.show(); System.out.println("宠物品种:" + this.getPinZhong()); } public void dogPlay() { The System.OUT .println ( " dog playing " ); the this .setHealth (getHealth () + . 5 ); } }
package Month.JUL.JUL12; //子类 小猫类 public class Cat extends Pet { private String sex; public Cat() { } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public void show() { super.show(); System.out.println("性别是:" + this.getSex()); } public void catPlay() { . The System OUT .println ( " Cat play " ); the this .setLove (getLove () + . 3 ); } }
Month.JUL.JUL12 Package; // primary human public class Master { // to the pet a bath public void xiZao (the Pet PET) { IF (the instanceof Cat PET) { the System. OUT .println ( " a " + pet.getName () + " bath .... " ); pet.setHealth (pet.getHealth () + . 3 ); } the else IF (the instanceof Dog PET) { the System. OUT .println ( " a " + pet.getName () + "洗澡。。。。"); pet.setHealth(pet.getHealth() + 5); } } // 陪宠物玩耍 public void play(Pet pet) { if (pet instanceof Cat) { Cat p = (Cat) pet; p.catPlay(); } else if (pet instanceof Dog) { Dog d = (Dog) pet; d.dogPlay(); } } public static void main(String[] args) { Master ma = new Master(); Dog dog = new Dog(); dog.setAge(3); dog.setName("小黄"); dog.setPinZhong("杜宾犬"); dog.show(); ma.xiZao(dog); ma.play(dog); System.out.println("*************"); dog.show(); System.out.println("*************"); Cat cat = new Cat(); cat.setSex("公"); cat.setAge(2); cat.setName("小白"); cat.show(); ma.play(cat); ma.xiZao(cat); System.out.println("***************"); cat.show(); } }