• 技术文章 >后端开发 >C#.Net教程

    C#基础知识整理:C#类和结构(4)

    黄舟黄舟2017-02-10 15:25:57原创695
    1、什么是接口? 功能特性? 实现代码?
    接口就是使用interface关键字定义的,由类的成员的组合组成的,描述一些功能的一组规范。在C#中可以看到,系统的一些接口都是这样命名的:IComparable(类型的比较方法)、ICloneable(支持克隆)、IDisposable(释放资源)等等,I表示接口,able则反映了接口的特性:“能... ...”,表明这一组规范能干什么。
    (1)、接口实现

       public interface IPrintAble
        {
            void PrintString();
    
            void PrintInt();
    
            void PrintBool();
        }
    
        public interface IComputeAble
        {
            void HandlerString();
    
            void HandlerInt();
    
            void HandlerBool();
        }
    
        public class MyInplementInterface : IPrintAble, IComputeAble
        {
            //隐式实现
            public void PrintString()
            {
                Console.WriteLine(@"1");
            }
    
            public void PrintInt()
            {
                Console.WriteLine(1);
            }
    
            public void PrintBool()
            {
                Console.WriteLine(true);
            }
    
            public void HandlerString()
            {
                Console.WriteLine(@"1" + "1");
            }
    
            public void HandlerInt()
            {
                Console.WriteLine(1 + 1);
            }
    
            public void HandlerBool()
            {
                Console.WriteLine(true || false);
            }
    
            //显示实现
            //void IComputeAble.HandlerString()
            //{
            //    throw new NotImplementedException();
            //}
    
            //void IComputeAble.HandlerInt()
            //{
            //    throw new NotImplementedException();
            //}
    
            //void IComputeAble.HandlerBool()
            //{
            //    throw new NotImplementedException();
            //}
        }
          class Program
        {
            static void Main(string[] args)
            {
                MyInplementInterface imple = new MyInplementInterface();
    
                imple.PrintString();
    
                imple.PrintInt();
    
                imple.PrintBool();
    
                imple.HandlerString();
    
                imple.HandlerInt();
    
                imple.HandlerBool();
    
                Console.ReadLine();
            }
        }

    结果:

    php入门到就业线上直播课:进入学习


    (2)实现专用接口,即C#已经定义好的接口
    例:

        public class ImplementSysInterface : IComparable
        {
            public int CompareTo(object obj)
            {
                //可以根据需要实现自己的比较方法
                return 0;
            }
    
            private void UsingMenthod()
            {
                //报错,因为NoIDisposeableClass没有实现IDisposable接口,所以不支持using
                //using (NoIDisposeableClass my = new NoIDisposeableClass())
                //{
    
                //}
                //实现IDisposable接口后,可以使用using
                using (IDisposeableClass my = new IDisposeableClass())
                {
    
                }
            }
        }
    
        public class NoIDisposeableClass
        {
    
        }
    
        public class IDisposeableClass : IDisposable
        {
            #region IDisposable 成员
    
            public void Dispose()
            {
                
            }
    
            #endregion
        }

    接口有如下特性:
    a、接口类似于抽象基类,不能直接实例化接口;接口中的方法都是抽象方法,实现接口的任何非抽象类型都必须实现接口的所有成员:
    b、当显式实现该接口的成员时,实现的成员不能通过类实例访问,只能通过接口实例访问。
    例如:

       public class MyInplementInterface2 : IComputeAble
        {
            void IComputeAble.HandlerString()
            {
                Console.WriteLine(@"1" + "1");
            }
    
            void IComputeAble.HandlerInt()
            {
                Console.WriteLine(true || false);
            }
    
            void IComputeAble.HandlerBool()
            {
                Console.WriteLine(true || false);
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                IComputeAble imple2 = new MyInplementInterface2();
    
                imple2.HandlerString();
    
                Console.ReadLine();
            }
        }

    c、当隐式实现该接口的成员时,实现的成员可以通过类实例访问,也可以通过接口实例访问,但是实现的成员必须是公有的。
    d、接口不能包含常量、字段、运算符、实例构造函数、析构函数或类型、不能包含静态成员。
    e、接口成员是自动公开的,且不能包含任何访问修饰符。
    f、接口自身可从多个接口继承,类和结构可继承多个接口,但接口不能继承类。

    2、什么是泛型? 泛型有哪些优点?
    所谓泛型,是将类型参数的概念引入到.NET,通过参数化类型来实现在同一份代码上操作多种数据类型。是引用类型,是堆对象。
    其实,一开始学泛型,是在学习java的时候,当时没有搞明白,我一直都觉得泛型纯属多此一举,用object一样可以搞定。比如,如下,比如,有人以类型的值,都要打印出来,于是object实现:

        public class Test
        {
            private object model;
    
            public object Model
            {
                get
                {
                    return model;
                }
    
                set
                {
                    model = value;
                }
            }
    
            public Test(object model)
            {
                this.Model = model;
            }
    
            public void ShowRecord()
            {
                Console.WriteLine(model);
            }
        }
       class Program
        {
            static void Main(string[] args)
            {
                int recordI = 2;
    
                bool recordB = true;
    
                Test testI = new Test(recordI);
    
                testI.ShowRecord();
    
                Test testB = new Test(recordB);
    
                testB.ShowRecord();
    
                Console.ReadLine();
            }
        }

    但是当学的多了,就会发现还是有一定的问题的。首先,就是装箱问题,int是值类型,赋值给object类型时,要完成一次装箱操作。什么是装箱?就是把recordI值复制到新的object分配的空间。浪费了时间和性能。所以泛型还是有作用的,那么,用泛型来实现:

      public class TestGeneric<T>
        {
            private T model;
    
            public T Model
            {
                get
                {
                    return model;
                }
    
                set
                {
                    model = value;
                }
            }
    
            public TestGeneric(T model)
            {
                this.Model = model;
            }
    
            public void ShowRecord()
            {
                Console.WriteLine(model);
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                int recordI = 2;
    
                bool recordB = true;
    
                TestGeneric<int> testGI = new TestGeneric<int>(recordI);
    
                testGI.ShowRecord();
    
                TestGeneric<bool> testGB = new TestGeneric<bool>(recordB);
    
                testGB.ShowRecord();
    
                Console.ReadLine();
            }
        }

    这样,当TestGeneric<int> testGI = new TestGeneric<int>(recordI)时,T就是int了,用不着装箱了。
    当然泛型不仅仅是要解决装箱问题,功能特性如下:
    a、避免装箱拆箱,提高了性能;
    b、提高了代码的重用性;
    c、类型安全的,因为在编译的时候会检测;
    d、可以创建自己的泛型接口、泛型类、泛型方法、泛型事件和泛型委托。
    以上就是C#基础知识整理:C#类和结构(4)的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    快捷开发Web应用及小程序:点击使用

    支持亿级表,高并发,自动生成可视化后台。

    上一篇:C#基础知识整理:C#类和结构(3) 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• asp.net 图片验证码的HtmlHelper• C# 动态加载Dll• 解决asp.net中“从客户端中检测到有潜在危险的Request.Form值”的错误• C#的多线程机制初探(1)• C++设计模式浅识策略模式
    1/1

    PHP中文网