1 //公共接口: "动物"
2 public interface IAnimal
3 {
4 void Behavior(); //行为方法,描述各种动物的特性
5 }
6
7 //类: 狗
8 public class Dog : IAnimal
9 {
10 public void Behavior()
11 {
12 Console.WriteLine(name() + "我晚上睡觉,白天活动");
13 }
14 string name()
15 {
16 return "狗";
17 }
18 }
19
20 //类: 猫
21 public class Cat : IAnimal
22 {
23 string info = "猫";
24 public void Behavior()
25 {
26 Console.WriteLine(info + "我白天睡觉,晚上活动");
27 }
28 }
29 //简单的应用:
30 static void Main(string[] args)
31 {
32 IAnimal myDog = new Dog();
33 myDog.Behavior(); //输出: "狗我晚上睡觉,白天活动"
34 IAnimal myCat = new Cat();
35 myCat.Behavior(); //输出: "猫我白天睡觉,晚上活动"
36
37 }