C#基础知识整理:C#类和结构(4)
发布日期:2021-11-07 06:40:41 浏览次数:9 分类:技术文章

本文共 5474 字,大约阅读时间需要 18 分钟。

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();        }    } 

结果:

(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
{ 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
testGI = new TestGeneric
(recordI); testGI.ShowRecord(); TestGeneric
testGB = new TestGeneric
(recordB); testGB.ShowRecord(); Console.ReadLine(); } }

 这样,当TestGeneric<int> testGI = new TestGeneric<int>(recordI)时,T就是int了,用不着装箱了。

 当然泛型不仅仅是要解决装箱问题,功能特性如下:
 a、避免装箱拆箱,提高了性能;
 b、提高了代码的重用性;
 c、类型安全的,因为在编译的时候会检测;
 d、可以创建自己的泛型接口、泛型类、泛型方法、泛型事件和泛型委托。
 

转载地址:https://blog.csdn.net/yysyangyangyangshan/article/details/7677355 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:C#基础知识整理:基础知识(1) Main方法
下一篇:C#基础知识整理:C#类和结构(3)

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月25日 00时03分58秒