zoukankan      html  css  js  c++  java
  • C# 接口

    声明接口

    public interface Iinterface
     {
         void GetUserName();
     }

        同上以上示例可以看出,接口中只能包含方法、属性、索引器和事件的声明。不允许声明成员上的修饰符。

        因为接口成员是暴露的,所以添加其他修饰符毫无意思,甚至连public都不用加。

    接口的派生

    public interface Iinterface
    {
       string  GetUserName();
    }
    
    class Test1 : Iinterface
    {
       public string GetUserName()
       {
           return "name";
       }
    }

        如果一个派生类继承了该接口,就必须要实现这个接口的所有方法,否则会编译错误。

    继承接口

        接口是可以继承的,并且支持多个继承。

        但是要注意,派生类中要实现接口与继承接口的所有方法。

        示例如下:

        public interface IBaseInterface
        {
            int GetAge();
        }
    
        public interface Iinterface : IBaseInterface
        {
            string  GetUserName();
        }
    
        class Test1 : Iinterface
        {
            public string GetUserName()
            {
                return "name";
            }
    
            public int GetAge() 
            {
                return 3;
            }
        }
  • 相关阅读:
    牛客小白月赛29 种树 题解(思维)
    E
    D
    方格取数(number) 题解(dp)
    csust T1097 “是时候表演真正的技术了” 题解(虚点跑最短路)
    poj 2352 Stars
    poj 3321 Apple Tree
    poj 3067 Japan
    poj 1195 Mobile phones
    poj 2155 Matrix
  • 原文地址:https://www.cnblogs.com/nonkicat/p/2784253.html
Copyright © 2011-2022 走看看