zoukankan      html  css  js  c++  java
  • C#----Get和Set在属性中的使用

    Get和Set在属性中的作用:

    第一个作用:保证数据的安全性,对字段进行了有效的保护。

    第二个作用:起到监视作用

    private int width=0;
    public int Width
    {
        get{return width;}
        set{width=value*2;}
    }
    
    可以监视字段的变化,在Set中使用
    private int width=0;
    public int Width
    {
        get{return width;}
        set
        {
            //Do something
            width=value;
        }
    }
    
            private int width = 1;
            public int Width
            {
                get { return width*2; }
                set {
                    Console.WriteLine("变化前:{0}", width); //1
                    width = value + 2;
                    Console.WriteLine("变化后:{0}", width); //7=5+2
                }
            }
            static void Main(string[] args)
            {
                Program p = new Program();
                Console.WriteLine(p.Width);
                p.Width = 5;//这里width的值不会是5的
                Console.WriteLine(p.Width);//14=2*7
                Console.WriteLine(p.Width);//14
                Console.Read();
            }


  • 相关阅读:
    veil-evasion免杀处理
    对程序"加料"
    堆栈溢出
    ICMP隧道 ptunnle
    socat流量操控
    DNS隧道 iodns
    dns隧道 dns2tcpd
    ssh X协议转发
    ssh动态端口转发
    ssh远程端口转发
  • 原文地址:https://www.cnblogs.com/ddx-deng/p/3755829.html
Copyright © 2011-2022 走看看