多态:
狗可以称为动物,猫可以称为动物。多态在于你可以不在意具体对象类型而使用它们共同的属性:
public class Animal {
public void eat() {
System.out.print("吃东西。。");
} }
public class Dog extends Animal {
@Override
public void eat() {
System.out.println("吃骨头");
} }
public class Cat extends Animal {
@Override
public void eat() {
System.out.println("吃鱼");
} }
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
Cat cat = new Cat();
eatSome(dog);
eatSome(cat);
}
static void eatSome(Animal animal) {
animal.eat();
} }