zoukankan      html  css  js  c++  java
  • 使用set时需要考虑的一点

    看源代码刚学到的,当属性的set块中还有其它调用或较复杂的操作时,不要重复设置对象的属性。道理很简单啊,还是贴段代码吧,代码演示的是怎样做是否重复的判断而不是重现上面描述的情景.
        class EntryPoint
        {
            [STAThread]
            
    static void Main(string[] args)
            {
                Test test 
    = new Test();
                test.Name 
    = "yicone";
                test.Name 
    = "yicone";
                test.Name 
    = "yic";
                Console.ReadLine();
            }
        }

        
    public class Test
        {
            
    int i = 1;
            
    public string name;
            
    public string Name
            {
                
    get{return name;}
                
    set
                {
                    
    if(value == Name)
                    {
                        Console.WriteLine(
    "第{0}次set name属性失败!", i++);
                        Console.WriteLine(
    "原因:与当前Test类的实例的Name属性值相同,不需要更改");
                        
    return;
                    }
                    name 
    = value;
                    Console.WriteLine(
    "第{0}次set name属性成功", i++);
                }
            }
        }

    p.s. 调整了措词,以避免不必要的误会.

  • 相关阅读:
    python自动华 (十七)
    vue 链接
    hexo博客相关
    nodejs 安装 cnpm 命令
    centos7 node express项目 将http接口升级为https接口的解决方法
    centos7 showdoc 手动安装部署
    centos7 showdoc 安装部署
    npm install -d
    AngularJs出现错误Error: [ng:areq]
    AngularJs1使用中出现错误 Error: [ng:areq]
  • 原文地址:https://www.cnblogs.com/yicone/p/234199.html
Copyright © 2011-2022 走看看