zoukankan      html  css  js  c++  java
  • c#显示实现接口和隐式实现的区别

    一、显示实现接口和隐式实现接口

    1、实现接口过程中若有两个接口中的某成员有相同名字则需用显示实现来解决,显示实现接口的类不能直接调用继承于接口的成员,必须先转换才行。

    2、 隐式实现接口,一般情况下就用隐式显示。不用转成接口再调用方法。

    public static void Main(string[] args)
            {
             //显示实现的    
            Ipaintpeople people=new    Paint ();
            people.Paint();//此方法是显示实现的接口中的方法,必须转成 Ipaintpeople  才能访问此方法
            var people2=new    Paint ();
            people2.Paintpig();//此方法非显示实现的接口中的方法,不用转成 Ipaintpeople  后访问此方法
            //隐式实现的
            var  dog=new Paint2();
            dog.Paint();
              Console.ReadKey(true);
            } 
        } 
        public interface Ipaintpeople
        {
             void Paint();
        }
        public interface Ipaintdog
        {
             void Paint();
        } 
        //显示实现接口
        public class Paint:Ipaintdog,Ipaintpeople
        {
             void Ipaintdog.Paint()
            {
                 Console.Write("Ipaintdog,Press any key to continue . . . ");
            }
             void Ipaintpeople.Paint()
            {
             Console.Write("Ipaintpeople,Press any key to continue . . . ");
            }
             public void Paintpig()
            {
                 Console.Write("Paintpig,Press any key to continue . . . ");
            }
            
        }
        //非显示实现接口
        public class Paint2:Ipaintdog
        {
            public void Paint()
            {
                 Console.Write("Ipaintdog,Press any key to continue . . . ");
            }
            
        }
  • 相关阅读:
    裸裸的spfa~嘿嘿嘿!
    睡前1小时数学系列之-整除
    拓扑排序1.奖金
    拓扑排序
    SCU 1095运送物资(最短路)
    POJ1158 城市交通Traffic lights IOI 1999 (最短路)
    POI0109 POD (最短路)
    HN0I2000最优乘车 (最短路变形)
    FOJ1205 小鼠迷宫问题 (BFD+递推)
    CJOI 05新年好 (最短路+枚举)
  • 原文地址:https://www.cnblogs.com/musexiaoluo/p/6477059.html
Copyright © 2011-2022 走看看