zoukankan      html  css  js  c++  java
  • 空对象模式和扩展方法的NULL验证

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    //空对象模式和扩展方法的NULL验证
    namespace Chap2_3
    {
    public class NULLObject
    {
    public void Test()
    {
    Promation promation = PromationFactory.Create("水果促销");
    Promation promation1 = PromationFactory.Create(null);
    if (promation.IsNullOrEmpty())
    {
    //扩展方法,判断为NULL
    Console.WriteLine("promation是NULL");
    }
    else
    {
    Console.WriteLine("promation不是NULL");
    }
    if (promation.IsNull)
    {
    Console.WriteLine("promation是NULL");
    }
    else
    {
    Console.WriteLine("promation不是NULL");
    }
    if (promation1.IsNull)
    {
    Console.WriteLine("promation1是NULL");
    }
    else
    {
    Console.WriteLine("promation1不是NULL");
    }
    Console.ReadKey();
    }

    }
    public class Promation
    {
    public virtual bool IsNull
    {
    get { return false; }
    }
    }
    public class NullPromation : Promation
    {
    public override bool IsNull
    {
    get { return true; }
    }
    }
    public class PromationFactory
    {
    public static Promation Create(string promationName)
    {
    if (string.IsNullOrEmpty(promationName))
    {
    return new NullPromation();
    }
    else
    {
    return new Promation();
    }
    }

    }
    //扩展方法
    public static class ObjectIsNUllOrEmpty
    {
    public static bool IsNullOrEmpty(this object obj)
    {
    return obj == null ? true : false;
    }
    }
    }

  • 相关阅读:
    第01组 Alpha冲刺 (1/6)
    第01组(17) 需求分析报告
    第01组(17) 团队展示
    结对编程作业
    05 RDD编程
    第九次作业
    HDFS
    Hadoop安装 与 HDFS体系结构
    第三次作业
    作业2
  • 原文地址:https://www.cnblogs.com/sulong/p/4919654.html
Copyright © 2011-2022 走看看