c#基本语法 の 接口



1.什么是接口

接口是指定一组函数成员,而不实现他们的引用类型。
我个人感觉,他更是一种规范。


1.1 接口有什么用处?看下边例子


A类

 class AClass
    {
    
    
        public string Name;
        public int Age;
    }

B类

 class BClass
    {
    
    
        public string First;
        public string Last;
        public double PersonAge;
    }

program 类

 class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            AClass aClass = new AClass()
            {
    
    
                Name = "张三",
                Age = 6,
            };
            PrintInfo(aClass);
        }

        static void PrintInfo(AClass item)
        {
    
    
            Console.WriteLine($"名字是{item.Name},年龄是{item.Age}");
        }
    }

这种写法的问题在于,如果你想通过 Bclass 而不是 Aclass 来实现个人信息的录入,这种方法就没有办法解决,因为 AclassBclass 是两种类。你点出来的东西不一样。



1.2 使用接口


1.2.1 声明接口

 interface IInfo
    {
    
    
        string GetName();
        string GetAge();
    }

1.2.2 实现接口

  class AInfo : IInfo
    {
    
    
        public string Name {
    
     get; set; }
        public int Aget {
    
     get; set; }
        public string GetAge()
        {
    
    
            return Name;
        }

        public string GetName()
        {
    
    
            return Aget.ToString();
        }
    }
class BInfo : IInfo
    {
    
    
        public string First {
    
     get; set; }
        public string Last {
    
     get; set; }
        public double PersonAge {
    
     get; set; }
        public string GetAge()
        {
    
    
            return PersonAge.ToString();
        }

        public string GetName()
        {
    
    
            return First + Last;
        }
    }

1.2.3 在主函数里调用

 class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            AInfo aInfo = new AInfo() {
    
     Name = "John", Aget = 35 };
            BInfo bInfo = new BInfo() {
    
     First = "dehua", Last = "liu", PersonAge = 33.6 };
            PrintInfo(aInfo);
            PrintInfo(bInfo);
        }
        static void PrintInfo(IInfo item)
        {
    
    
            Console.WriteLine($"名字{item.GetName()},年龄是{item.GetAge()}");
        }
    }

注意:在上面引用的地方,我们传入的是接口,而不是具体实现某个接口的类





2. 接口的声明


2.1声明接口的注意事项

  • 接口只能包含非静态成员函数(方法,属性,事件,索引器)
  • 这些函数成员不能包含任何实现的代码,而且主体后面必须使用分号
  • 按照管理,接口的声明,必须使用大写 I 开头
  • 接口声明可以带任何修饰符。例如 public protected internal private。但是接口的成员,都是隐式的public ,不能带任何修饰符,包括 public
public interface IInfo
    {
    
    
        string GetName();
        string GetAge();
    }

2.2 实现接口的注意事项

  • 如果类实现了接口,那么它必须要实现接口的所有成员
  • 如果接口从基类继承并实现了接口,那么他必须要放在所有的几口之前。中间用逗号隔开。


2.3 接口和 as 运算符

接口是引用类型,可以实现强制类型转换(即用他儿子冒充他爸)。如下:

  static void Main(string[] args)
        {
    
    
            AInfo aInfo = new AInfo() {
    
     Name = "John", Aget = 35 };
            IInfo info = (IInfo)aInfo;
            PrintInfo(info);
        }

当时在此过程中,如果类没有实现接口,那么表达式应该会抛出一个异常,这个时候,可以使用 as 运算符来避免这个问题,(你应该极力的避免异常,因为他们会严重降低代码的速度,并将程序置为一种不一致的状态)

static void Main(string[] args)
        {
    
    
            AInfo aInfo = new AInfo() {
    
     Name = "John", Aget = 35 };
            IInfo info = aInfo as IInfo;
            if (info != null)
            {
    
    
                PrintInfo(info);
            }
         }


2.4 实现具有重复成员的接口

如果一个类实现了多个接口,并且其中一些接口有相同签名和返回类型的成员,那么类可以实现单个成员来满足所有包含重复成员的接口。

2.5 派生成员作为实现

如果派生类能实现了基类的成员,那么即使该类是空的,基类还是可以满足实现接口方法的需求

interface IIfc1 {
    
     void PrintOut(string s);}
interface IIfc1 {
    
     void PrintOut(string s); }

class AClass
    {
    
    
        public void PrintOut(string s)
        {
    
    
            Console.WriteLine("s");
        }
    }
    
class BClass : AClass, IIfc1
    {
    
      
    // 这里面是空的,也没事
    }



2.6 显式接口成员实现

如果单个类,可以实现多个接口所需要的成员,如果我们希望每个几口分离实现,那就应该使用 显示成员函数实现

声明

interface IIfc1
    {
    
    
        void PrintOut(string s);
    }
    
interface IIfc2
    {
    
    
        void PrintOut(string s);
    }
    class MyClass : IIfc1, IIfc2
    {
    
    
        void IIfc1.PrintOut(string s)
        {
    
    
            Console.WriteLine($"IIf1 输出 {s}");
        }

        void IIfc2.PrintOut(string s)
        {
    
    
            Console.WriteLine($"IIf2 输出 {s}");
        }
    }

调用:

static void Main(string[] args)
        {
    
    
            MyClass mc = new MyClass();
            IIfc1 ifc1 = mc as IIfc1;
            ifc1.PrintOut("你好");
        }

猜你喜欢

转载自blog.csdn.net/zhaozhao236/article/details/110853621
今日推荐