• C#--接口的实现


    接口:

      1. 不允许使用访问修饰符,所有接口成员都是公共的.
      2. 接口成员不能包含代码体.
      3. 接口不能定义字段成员.
      4. 接口成员不能使用关键字static,vritual,abstract,sealed来定义.
      5. 类型定义成员是禁止的.

    如果要隐藏继承了接口中的成员,可以用关键字new来定义它们.

    public interface IMyInterface
    {
        void DoSomething();
    }
    
    public interface IMyDeInterface : IMyInterface
    {
        new void DoSomething();
    }

    在接口中定义的属性可以确认访问块get和set中的哪一个能用于该属性.

    public interface IMyInterface
    {
        int myNum
        {
            get;
            set;
        }
    }

    注意:接口中不能指定字段.

    在类中实现接口:

    当一个类实现一个接口时,可以使用abstract或virtual来执行接口的成员,而不能使用static或const.

    一个类的如果实现了一个借口,那么这个类的派生类隐式的支持该接口.

    显式执行接口成员

    接口成员可以由类显式的执行.如果这么做,那么这个成员只能通过接口来访问.而不能通过类来访问.

    public interface IMyInterface
    {
        void DoSomeThing();
    }
    
    public class MyClass : IMyInterface
    {    
        void IMyInterface.DoSomeThing()
        {
            throw new NotImplementedException();
        }
    }
     
        protected void Page_Load(object sender, EventArgs e)
        {
            //此时mc是没有方法的.
            MyClass mc = new MyClass();
    
            //此时my有个方法DoSomeThing()
            IMyInterface my = mc;
            my.DoSomeThing();
        }
     
    隐式执行接口成员
    默认都是隐式执行接口成员.
    public interface IMyInterface
    {
        void DoSomeThing();
    }
    
    public class MyClass : IMyInterface
    {    
        public void DoSomeThing()
        {
            throw new NotImplementedException();
        }
    }
     
        protected void Page_Load(object sender, EventArgs e)
        {
            MyClass mc = new MyClass();
            mc.DoSomeThing();
        }

    类实现接口属性

    public interface IMyInterface
    {
        int MyNum
        {
            get;
        }
    }
    
    public class MyClass : IMyInterface
    {
        protected int myNum;
        public int MyNum
        {
            get
            { 
                return myNum; 
            }
            set
            {
                myNum = value;
            }
        }
    }
     
        protected void Page_Load(object sender, EventArgs e)
        {
            MyClass mc = new MyClass();
            mc.MyNum = 12;
        }
  • 相关阅读:
    Spring-IOC容器
    VUE 过滤器
    axios.post参数问题
    Stylus| vue项目中stylus和stylus-loader版本兼容问题
    SPA
    Options API 和 Composition API 的对比
    【ES6学习笔记之】Object.assign()
    vue element-ui 常用组件
    Vue调试工具
    组件
  • 原文地址:https://www.cnblogs.com/loveYN/p/4509693.html
Copyright © 2020-2023  润新知