zoukankan      html  css  js  c++  java
  • c# 第四课 interfaces

    An interface is a contract(协定) that guarantees to a client how a class or struct will behave.
    When a class implements an interface(实现一个接口), it tells any potential(可能的) client “I guarantee I’ll support all the methods, properties, events, and indexers of the named interface.”
    These contracts are made manifest(表明) using the interface keyword, which declares a reference type(引用类型) that encapsulates(封装) the contract.
    interface ICompressible
    {
        void Compress();
        void Decompress();
    }
    interface ILoggedCompressible 
    : ICompressible
    {
        void LogSavedBytes();
    }
    public class Document : ILoggedCompressible 
    {
        // 实现ICompressible
        public void Compress(){ …}
        public void Decompress() { …}
        // 实现ILoggedCompressible 
         public void LogSavedBytes() { …}
    // 要实现所有的接口成员

    as operator

    foreach (Document doc in folder)
    {
            IStorable isDoc = doc as IStorable;
            if (isDoc != null)
            {
                    isDoc.Read();
            }else{
                    Console.WriteLine("IStorable not supported");doc.Encrypt();
            }
    …
    }
    
    as 有左右两个操作数
    Casts the left operand to the type specified by the right operand.
    Returns null if the cast fails.
    Interface Vs. Abstract Class
    Interfaces are very similar to abstract classes.
    C# doesn’t allow multiple inheritance with classes.
    C# does allow to implement any number of interfaces and derive from one base class.
  • 相关阅读:
    现状和措施
    Nginx http升级到https
    搭建 git 服务器
    Vue + Springboot 开发的简单的用户管理系统
    Vue中的button事件
    Mvc多级Views目录 asp.net mvc4 路由重写及 修改view 的寻找视图的规则
    asp.net mvc 多级目录结构
    Asp.net下使用HttpModule模拟Filter,实现权限控制
    JavaScript事件冒泡简介及应用
    Rhino Mock
  • 原文地址:https://www.cnblogs.com/GSONG/p/4399062.html
Copyright © 2011-2022 走看看