• 20.明白接口实现和虚函数实现的区别


    接口实现没有多态功能, 以接口调用接口方法,其性质表现为实现接口的类的方法行为。

    若要让接口实现多态功能,需要用virtual 实现。如下:

    public interface IA
        {
            void Print();
        }

        public class MyClass:IA
        {
            public virtual void Print()
            {
                Console.WriteLine("MyClass");
            }
        }
        public class MyDerive : MyClass
        {
            public override void Print()
            {
                Console.WriteLine("MyDerive");
            }
        }

        static void Main(string[] args)
            {
                //Program.print();

                MyDerive myderve = new MyDerive();
                myderve.Print();
                IA ia = myderve as IA;
                ia.Print();
                MyClass myclass = myderve as MyClass;
                myclass.Print();
            }

  • 相关阅读:
    正则表达式 (记录中...)
    css 坑记
    WebApi 中使用 Token
    WebApi 中使用 Session
    微信小程序 入门
    .net EF监控 MiniProfiler
    css布局
    移动端1像素边框问题
    移动端页面自适应解决方案:rem 布局篇
    js重点知识总结
  • 原文地址:https://www.cnblogs.com/movemoon/p/2738353.html
Copyright © 2020-2023  润新知