public class Animal {
public string name;
public int age;
}
class Dog : Animal {
public char gender;
}
class Hashiqi : Dog {
public string kind;
}
class wolf : Animal { }
class Tiger : Animal { }
class Lion : Animal { }
class Panda : Animal { }
class Monkey : Animal { }
class Snake : Animal { }
class Elephlent : Animal { }
public class Program
{
public static void Main(string[] args)
{
//首先实例化一个Dog对象
Dog d = new Dog();
d.name = "";
d.age = 10;
d.gender = ' ';
//向上转型:向上转型的对象将不再能够使用子类中特有的字段属性和方法
Animal a = d;
a.name = "";
a.age = 1;
//向下转型:向下转型后的对象就可以使用子类中特有的字段属性和方法,前提:转型成功
Dog dd = a as Dog;
dd.gender = ' ';
Hashiqi h = a as Hashiqi;
RecordAnimal(new Hashiqi());
}
//录入信息的方法,那么参数如何写??
//1、可以写子类,然后重载,每个子类都写一个方法。
//2、可以写父类Animal
public static void RecordAnimal(Animal a)
{
//如果这个动物是Hashiqi,打印hashiqi
//使用is关键字判断类型
if(a is Hashiqi)
{
Console.WriteLine("这是一个哈士奇,本园不收");
}
}
}