Beispiel #1
0
        static void Main(string[] args)
        {
            Duck d = new Duck();

            //Duck对象d可以使用3种方法:
            //1.自身定义的;
            //2.父类定义的
            //3.接口定义的
            d.Fly();
            d.Cook();
            d.Swim();
            //将子类(Duck)对象赋给基类变量
            Bird b = d;

            //现在只能使用基类定义的Fly()方法
            b.Fly();
            //将Duck对象赋给ISwin接口变量
            ISwim s = d;

            //现在只能使用接口定义的Swim()方法
            s.Swim();
            //将Duck对象赋给另一个实现的接口IFood接口变量
            IFood f = d;

            //现在只能使用接口定义的Cook()方法
            f.Cook();
            Console.ReadKey();
        }
Beispiel #2
0
    void Act(GameObject animal)
    {
        switch (input)
        {
        case InputType.LMB:
        {
            ISpeak speak = animal.GetComponent <ISpeak>();
            if (speak != null)
            {
                speak.Speak();
                speakCount++;
            }
            break;
        }

        case InputType.RMB:
        {
            IWalk walk = animal.GetComponent <IWalk>();
            if (walk != null)
            {
                walk.Walk();
                walkCount++;
            }
            break;
        }

        case InputType.Space:
        {
            ISwim swim = animal.GetComponent <ISwim>();
            if (swim != null)
            {
                swim.Swim();
                swimCount++;
            }
            break;
        }

        case InputType.I:
        {
            IID id = animal.GetComponent <IID>();

            if (id != null)
            {
                id.Identify();
            }

            break;
        }

        default: break;
        }
    }
Beispiel #3
0
        static void Main(string[] args)
        {
            Duck duck = new Duck();

            duck.Fly();
            duck.Swim();
            duck.Cook();
            Bird bird = duck;

            bird.Fly();
            //将Duck对象赋给ISwin接口变量
            ISwim s = duck;

            //只会游泳
            s.Swim();
            //将Duck对象赋给另一个实现的接口IFood接口变量
            IFood food = duck;

            //只会Cook
            food.Cook();
            Console.ReadKey();
        }
Beispiel #4
0
 public void DoSwim()
 {
     swimAction.Swim();
 }
 public void Swim(ISwim swim)
 {
     Console.WriteLine("Person lives " + this.GetType().Name + " swim in " + swim.GetType().Name);
       swim.Swim();
 }
Beispiel #6
0
 public void Welcome(ISwim obj) => obj.Swim();
Beispiel #7
0
 public void MakeSwim()
 {
     _swim.Swim();
 }
Beispiel #8
0
 public void Swim()
 {
     _swim.Swim();
 }
Beispiel #9
0
 public virtual void SPropFunc()
 {
     _swim.Swim();
 }
Beispiel #10
0
 public virtual void Swim()
 {
     _swim?.Swim();
 }
Beispiel #11
0
 public virtual void MakeSwim()
 {
     _swim.Swim();
 }
Beispiel #12
0
 public void Swim()
 {
     Console.WriteLine("Please tell me that you know how to swim!");
     _swimBehaviour.Swim();
     Console.WriteLine();
 }
Beispiel #13
0
 public void Swim()
 {
     iswim.Swim();
 }