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;
    }
    }
    }

  • 相关阅读:
    Java修饰符
    java中接口的定义
    抽象类
    final关键字的特点
    hdu6489 2018 黑龙江省大学生程序设计竞赛j题
    POJ 3268 (dijkstra变形)
    poj 2253 floyd最短路
    poj1681 Network
    bzoj1202 狡猾的商人
    Nastya Is Buying Lunch
  • 原文地址:https://www.cnblogs.com/sulong/p/4919654.html
Copyright © 2011-2022 走看看