这个练习我用到了继承,多态和封装。
1.继承:
Animal 类是一个抽象类,它有两个子类 Dog 和 Cat。
Dog 和 Cat 分别继承自 Animal 类,因此它们可以使用 Animal 类中定义的属性和方法,同时也可以有自己特有的属性和方法。
2.多态:
在 Person 类的 keepPet 方法中,通过 instanceof 关键字判断传入的动物对象是 Dog 还是 Cat 类型,然后进行相应的操作。这就是多态的体现,即相同的方法调用可以根据不同对象的实际类型执行不同的操作。
3.封装:
Animal 类中的 color 和 age 属性被设置为私有(private),只能通过公共的 getter 和 setter 方法访问和修改。
package com.animal.java;
public abstract class Animal {
private String color;
private int age;
public Animal() {
}
public Animal(String color, int age) {
this.color = color;
this.age = age;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public abstract void eat(String str);
}
package com.animal.java;
public class Dog extends Animal{
public Dog() {
}
public Dog(String color, int age) {
super(color, age);
}
public void lookHouse(){
System.out.println("小狗正在看家");
}
@Override
public void eat(String str) {
System.out.println(getAge() + "岁的" + getColor() + "小狗正在猛吃" + str);
}
}
package com.animal.java;
public class Cat extends Animal{
public Cat() {
}
public Cat(String color, int age) {
super(color, age);
}
public void catchMouse(){
System.out.println( getColor() +"的猫正在抓小老鼠");
}
@Override
public void eat(String str) {
System.out.println(getAge() + "岁的" + getColor() + "小猫正在吃" + str);
}
}
package com.animal.java;
public class Person {
private String name;
private int age;
public Person() {
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
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 keepPet(Dog dog,String str){
System.out.println("年龄为" + this.age + "岁的" + this.name + "养了一只年龄为" +dog.getAge() +"岁的" + dog.getColor() +"小狗");
dog.eat(str);
}
饲养猫
public void keepPet(Cat cat,String str){
System.out.print("年龄为" + this.age + "岁的" + this.name + "养了一只年龄为" +cat.getAge() +"岁的" + cat.getColor() +"小猫");
cat.catchMouse();
}
*/
public void keepPet(Animal animal,String str) {
if(animal instanceof Dog){
Dog dog = (Dog) animal;
System.out.println("年龄为" + this.age + "岁的" + this.name + "养了一只年龄为" +dog.getAge() +"岁的" + dog.getColor() +"小狗");
dog.eat(str);
}
else if(animal instanceof Cat){
Cat cat = (Cat) animal;
System.out.println("年龄为" + this.age + "岁的" + this.name + "养了一只年龄为" +cat.getAge() +"岁的" + cat.getColor() +"小猫");
cat.eat(str);
}
else {
System.out.println("没有这种动物");
}
}
}
package com.animal;
import com.animal.java.Animal;
import com.animal.java.Cat;
import com.animal.java.Dog;
import com.animal.java.Person;
public class Test {
public static void main(String[] args) {
Person person1 = new Person("老王",30);
Animal dog = new Dog("黑色",2);
person1.keepPet(dog,"骨头");
Dog dog1 = new Dog("橘色",4);
System.out.print(dog1.getAge() + "岁的" + dog1.getColor());
dog1.lookHouse();
Person person2 = new Person("老李",25);
Cat cat = new Cat("灰色",3);
person2.keepPet(cat,"小鱼干");
cat.catchMouse();
}
}
结果展示: