• 接口的理解与使用《转》


    关键字interface 显式实现 隐式实现 多态 定义  抽象类

    接口就是定义一个协定,只是一个架子而已,必须由结构或者类来进行实现。

    接口的定义和使用

    例如:定义一个接口

    interface Border  

    {

        int weight //没有实现

        {

            set;        

     get;

        }

       int height //没有实现

        {

            set;

            get;

        }

    }//什么都没有做,只告诉大家两个属性weight,height,要使用这个接口,必须对它进行实现。

    class Square:Border //注意对接口的实现

    {

        private int x;

        private int y;

       public int weight

        {

            get

            {

                return x;

            }

            set

            {

                x = value;

            }

        }

       public int height

        {

            get

            {

                return y;

            }

            set

            {

                y = value;

            }

        }

    对接口的应用

    Border b = new Square();

    b.weight =2;

    b.height = 2;

    Console.WriteLine("heighe is {0,weight is {1}",b.height,b.weight);

    上述简单对接口进行了一个举例。运行之后相信对接口应该有个简单的概念。

    接口的注意事项

    1 实现类必须对接口的所有成员进行实现,例如上例中的weightheight属性都要实现。

    2 接口不能包括变量成员,只能是属性、事件、方法、索引器(上述例子只有两个属性)

    3 实现接口的类必须严格按照接口的定义来进行实现

    4 实现接口可以隐式和显式来实现

    例如:

    interface IControl //接口1

    {

        void Paint();

    }

    interface ISurface //接口 2

    {

        void Paint();

    }

    //注意:上述两个接口都包含方法Paint(),下面实现类中必须进行显式实现

    class SampleClass : IControl, ISurface

    {

        Void Paint()//如此隐式实现肯定容易出现歧义

        void IControl.Paint() //显式实现

        {

            System.Console.WriteLine("IControl.Paint");

        }

        void ISurface.Paint()//显式实现

        {

            System.Console.WriteLine("ISurface.Paint");

        }

    }

    5 接口中的成员显式实现后,必须通过接口调用,而不能直接通过类进行调用

     上述事例中 SampleClass sp = new SampleClass();

     Sp.Paint()这样是错误的,必须IControl I1 = (IControl) sp;然后I1. Paint()

    6 一个接口可以由几个类进行实现,也可以只有一个类进行实现,也可以叫做接口的多态

    7 一个实现类可以同时实现几个接口,也可以实现唯一一个接口

    8 已有实现类的接口定义后不能随意更改,否则容易打乱实现类。

    完成接口练习的例子:

    using System;

    using System.Collections.Generic;

    using System.Text;

    interface Border //定义接口

    {

        int weight

        {

            set;

            get;

        }

       int height

        {

            set;

            get;

        }

    }

    interface Area   //定义接口

    {

        int ReAreaValue();

    }

    interface Perimeter //定义接口

    {

        int RePerimeterValue();

    }

    interface px //定义接口

    {

        string pring();

    }

    interface IControl //定义接口

    {

        void Paint();

    }

    interface ISurface //定义接口

    {

        void Paint();

    }

    class Square:Border,Area,Perimeter //实现接口,同时实现Border,Area,Perimeter三个接口

    {

        private int x;

        private int y;

       public int weight

        {

            get

            {

                return x;

            }

            set

            {

                x = value;

            }

        }

       public int height

        {

            get

            {

                return y;

            }

            set

            {

                y = value;

            }

        }

        public int ReAreaValue()

        {

           return x * y;

        }

        public int RePerimeterValue()

        {

            return (x + y) * 2;

        }

    }

    class tempclass : Area //实现接口,再次实现Area接口 [多态性]

    {

        public int x = 3;

        public int ReAreaValue()

        {

            return x*x;

        }

    }

    class SampleClass : IControl, ISurface //实现接口 IControl, ISurface

    {

        void IControl.Paint() //显示实现

        {

            System.Console.WriteLine("IControl.Paint");

        }

        void ISurface.Paint()//显示实现

        {

            System.Console.WriteLine("ISurface.Paint");

        }

    }

    class MainClass

    {

        static void wrtieBorder(Border b)

        {

           Console.WriteLine("heighe is {0,weight is {1}",b.height,b.weight);

        }

        static void writeArea(Area a) 

        {

            Console.WriteLine("a.ReAreaValue() is {0}", a.ReAreaValue());

        }

        static void writePerimeter(Perimeter p)

        {

            Console.WriteLine("p.RePerimeterValue() is {0}", p.RePerimeterValue());

        }

        static void Main()

        {

            Square s1 = new Square();

            s1.weight =2;

            s1.height = 2;

            wrtieBorder(s1);

            writePerimeter(s1);

            writeArea(s1);

            tempclass temp = new tempclass();

            Console.WriteLine("Main tempclass object temp.ReAreaValue() is " + temp.ReAreaValue());

            writeArea(temp);

            Border b = new Square();

            b.weight = 5;

            b.height = 6;

            wrtieBorder(b);

            SampleClass obj = new SampleClass();

            IControl c = (IControl)obj;

            c.Paint();

            ISurface s = (ISurface)obj;

            s.Paint(); // Calls ISurface.Paint on SampleClass.

            Console.ReadLine();

        }

    }

    接口与抽象类

        接口与抽象类有很多相似的地方,也有区别的地方:如下

    抽象类:一种不能实例化而必须从中继承的类,抽象类可以提供实现,也可以不提供实现

            子类只能从一个抽象类继承

           抽象类应主要用于关系密切的对象

           如果要设计大的功能单元,则使用抽象类。

           如果预计要创建组件的多个版本,则创建抽象类

    接口:是完全抽象的成员集合,不提供认识实现。

    类或者结构可以继承几个接口。

    接口最适合为不相关的类提供通用功能

    如果要设计小而简练的功能块,则使用接口

     总结

        接口,以下是我们必须乱记于心的地方,接口只包含方法、委托或事件的签名。方法的实现是在实现接口的类中完成的,接口可以是命名空间或类的成员,并且可以包含下列成员的签名:方法、属性、索引器、事件,一个接口可从一个或多个基接口继承。当基类型列表包含基类和接口时,基类必须是列表中的第一项。

    实现接口的类可以显式实现该接口的成员。显式实现的成员不能通过类实例访问,而只能通过接口实例访问。

    一览众山小
  • 相关阅读:
    Storm
    Linux 网络配置
    .NET Remoting
    jwt
    new操作符
    元数据
    C# lock
    三十而立
    面试
    灯火
  • 原文地址:https://www.cnblogs.com/ZLGBloge/p/4119038.html
Copyright © 2020-2023  润新知