zoukankan      html  css  js  c++  java
  • 扩展接口

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace WebApp2010
    {
    public class UserProfilerService : IUserProfiler
    {
    public string GetUserProfiler(string UserId)
    {
    if (UserId == "fasdfasdfasdfas")
    return "到北京";
    else return string.Empty;
    }
    }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace WebApp2010
    {
    public static class Extensions
    {
    public static string MyProfiler(this IUserProfiler iup)
    {
    string username = iup.GetUserProfiler("fasdfasdfasdfas");
    return "Welcome, " + username + "!";
    }
    }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace WebApp2010
    {
    public interface IUserProfiler
    {
    string GetUserProfiler(string UserId);
    }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Security.Cryptography;
    using System.Text;
    using System.Threading.Tasks;
    using WebApp2010;

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    IUserProfiler iup = new UserProfilerService();
    string name = iup.MyProfiler();
    Console.Write(name);
    string name1 = iup.GetUserProfiler("fasdfasdfasdfas");
    Console.Write(name1);
    string str1 = Console.ReadLine();

     假设有一种场景是我我无法修改接口和他的实现类,因为在某种情况下,如果接口的实现类很多的话,那么你就要一一修改实现类里面的代码。这个时候我们只需要扩展接口就可以很轻松的解决这件事情。

  • 相关阅读:
    原生JS 碰撞检测
    sdw
    无题
    什么是递归?递归大讲义
    C#中常用的经典文件操作方法
    rpm包时遇到Header V3 DSA signature: NOKEY时解决办法
    Linux vim命令
    Linux yum命令详解
    visual studio 2013 快捷键大全
    D_S 线性表的顺序表示和实现
  • 原文地址:https://www.cnblogs.com/BoYu045535/p/3683115.html
Copyright © 2011-2022 走看看