package demo1;
public class Animal {
public String name;
public int age;
public Animal(String name, int age) {
this.name = name;
this.age = age;
}
public void eat() {
System.out.println(this.name +" 正在吃饭.....");
}
}
package demo1;
public class Bird extends Animal{
public Bird(String name, int age) {
super(name, age);
}
@Override
public void eat() {
System.out.println(this.name +" 正在吃鸟粮...");
}
public void fly() {
System.out.println(this.name +" 正在飞.....");
}
}
package demo1;
public class Dog extends Animal{
public Dog(String name, int age) {
super(name, age);
}
public void bark() {
System.out.println(this.name+" 正在汪汪汪叫...");
}
@Override
public void eat() {
System.out.println(this.name +" 正在吃狗粮...");
}
}
package demo1;
public class Test {
public static void main(String[] args) {
Animal animal = new Bird("小鸟",2);
Bird bird = (Bird)animal;
bird.fly();
System.out.println("=============");
Animal animal2 = new Dog("单身狗",10);
//animal2 这个引用 是否引用了 Bird这个对象
if(animal2 instanceof Bird) {
Bird bird2 = (Bird) animal2;
bird2.fly();
}else {
System.out.println("animal2 instanceof Bird not!!!!");
}
}
public static void func(Animal animal) {
animal.eat();
}
public static int add(int a,int b) {
return a+b;
}
public static int add(int a,int b,int c) {
return a+b+c;
}
public static void main3(String[] args) {
add(1,2);
add(1,2,3);
}
public static void main2(String[] args) {
Dog dog = new Dog("旺财",10);
func(dog);
System.out.println("======");
Bird bird = new Bird("小鸟",2);
func(bird);
}
public static void main1(String[] args) {
Animal animal = new Dog("旺财",10);
animal.eat();
}
}