面向对象多态之接口

申明下:我也是新手,以下如有不对或不合适的地方望指出,一起交流O(∩_∩)O哈!

好了,转入正题

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace Interface
 7 {
 8     class InterfaceTest
 9     {
10         static void Main(string[] args)
11         {
12             #region 测试Cat,Monkey,Bear类
13             introduction test;
14             for (int i = 1; i<=3; i++)
15             {
16                 switch (i)
17                 {
18                     case 1:
19                         test = new Cat();
20                         test.speak();       //亲们,我的名字test有魅力么
21                         break;
22                     case 2:
23                         test = new Bear();
24                         test.speak();       //亲们,我的名字test有魅力么
25                         break;
26                     case 3:
27                         test = new Monkey();
28                         test.speak();       //亲们,我的名字test有魅力么
29                         break;
30                 }
31 
32             }
33             #endregion
34 
35 
36             #region 好了,不用多态的方式去达到上边的效果,Thinking,该如何实现
37             for (int i = 1; i <= 3; i++)
38             {
39                 switch (i)
40                 {
41                     case 1:
42                         Cat cat=new Cat();
43                         cat.speak();            //亲们,我的名字好别扭o(>﹏<)o呃呃呃
44                         break;
45                     case 2:
46                         Bear bear=new Bear();
47                         bear.speak();           //亲们,我的名字好别扭o(>﹏<)o呃呃呃
48                         break;          
49                     case 3:
50                         Monkey monkey=new Monkey();
51                         monkey.speak();         //亲们,我的名字好别扭o(>﹏<)o呃呃呃
52                         break;
53                 }
54             }
55             /*  好了,两种方式你发现了什么呢?神马,不会什么都没看出来么,想想,如果,有一天,我也一统天下,我的名字不再叫Cat,Bear和Monkey了,
56             那上面两种方式各需要做哪些改动呢?对于第二种我想说:天哪,我疯了,幸亏我只用了三次,用多了,这,哪改的完呢
57              */
58 
59             /*  重要:上面的方式之所以可以用接口去调用各个继承了它的类的方法,是因为,听着:父类引用指向子类对象!!!好了,你可以说我
60             Cat test=new Cat(),Bear test=new Bear(),亲,这,在一工程中,这实际么,可能,写着写着,自己都吐了,感受到接口的魅力了么,
61             感觉到面向对象爽之处了么,所以,要面向接口化编程!
62             其实,想想,第一种方式,如果有一天类名字改变了,要改的地也蛮多,这,这,工厂,你该出来了吧。。。
63              */
64             #endregion
65         }
66     }
67 }

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interface
{
    /// <summary>
    /// 假设所有的动物类都有一个自我介绍的方法,为其定义一个共同的接口(introduction:介绍)
    /// </summary>
   public interface introduction
    {
       void speak();
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interface
{
    /// <summary>
    /// Cat类继承introduction接口
    /// </summary>
    public class Cat:introduction
    {
        #region introduction 成员

        public void speak()
        {
            Console.WriteLine("我是机器猫,什么?不信,我给你变出好多吃的。。。");
        }

        #endregion
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interface
{
    /// <summary>
    /// Bear类继承introduction接口
    /// </summary>
    public class Bear:introduction
    {
        #region introduction 成员

        public void speak()
        {
            Console.WriteLine("知道火焰是我最好的玩具的一定知道我吧,那只可爱的小熊熊就是我啦啦。。。");
        }

        #endregion
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interface
{
    /// <summary>
    /// Monkey类实现introduction接口
    /// </summary>
    public class Monkey:introduction
    {
        #region introduction 成员

        public void speak()
        {
            Console.WriteLine("我是齐天大圣,记得吗,五百年前大闹天宫的就是俺老孙。。。");
        }

        #endregion
    }
}

 

转载于:https://www.cnblogs.com/xiaoyu-well-being/p/3768758.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值