/*
引用类型数据转换:
小数据类型 ----> 大数据类型 自动转换
大数据类型 --------> 小数据类型 强制类型转换
示例代码:多态请款下,通过强制类型转换,可以使用子类独有的成员
*/
class Animal
{
String name; //名字
public Animal(String name)
{
this.name = name;
}
public void run()
{
System.out.println(name + "在跑");
}
}
//mouse
class Mouse extends Animal
{
String country;
public Mouse(String name, String country)
{
super(name);
this.country = country;
}
public void dig()
{
System.out.println(name + "老鼠在打动!!!");
}
}
//FIsh
class Fish extends Animal
{
public Fish(String name)
{
super(name);
}
public void bubble()
{
System.out.println(name + "于在冒泡");
}
}
class Demo1
{
public static void main(String[] args)
{
Animal a1 = new Fish("大鱼海棠");
print(a1);
Animal f1 = new Mouse("袋鼠", "澳大利亚");
print(f1);
}
public static void print(Animal animal)
{
if (animal instanceof Fish)
{
Fish f1 = (Fish)animal;
f1.bubble();
}
else if (animal instanceof Mouse)
{
Mouse m1 = (Mouse)animal;
m1.dig();
System.out.println(m1.country);
}
}
}