C#属性和字段

来源:互联网 发布:it编程培训学校 编辑:程序博客网 时间:2024/03/29 16:56

1.属性set时完成一些逻辑,类似方法

       public const int STATE_FREE = 0;

        public const int STATE_SENDCALL = 1;
        public const int STATE_CALLING = 2;

        int _MonitorState = STATE_FREE;

        // 监视状态
        private int MonitorState
        {
            set
            {
                switch (value)
                {
                    case STATE_FREE:
                        this.btn_StartWatch.Enabled = true;
                        this.btn_StopWatch.Enabled = false;
                        break;
                    case STATE_SENDCALL:
                    case STATE_CALLING:
                        this.btn_StartWatch.Enabled = false;
                        this.btn_StopWatch.Enabled = true;
                        break;
                }
                _MonitorState = value;
            }
            get

            {

                ...........

                return _MonitorState;
            }

        }


2.用new重新创建与基类同名的属性

 public ManageDataService() {
            this.CookieContainer = new CookieContainer();
            this.Url = global::Haier.Uhome.HR91.Data.Properties.Settings.Default.Data_WebReference_DataService;
            if ((this.IsLocalFileSystemWebService(this.Url) == true)) {
                this.UseDefaultCredentials = true;
                this.useDefaultCredentialsSetExplicitly = false;
            }
            else {
                this.useDefaultCredentialsSetExplicitly = true;
            }
        }

        public new CookieContainer CookieContainer
        {
            get
            {
                return base.CookieContainer;
            }

            set
            {
                base.CookieContainer = value;
            }
        }

        public new string Url {
            get {
                return base.Url;
            }
            set {
                if ((((this.IsLocalFileSystemWebService(base.Url) == true)
                            && (this.useDefaultCredentialsSetExplicitly == false))
                            && (this.IsLocalFileSystemWebService(value) == false))) {
                    base.UseDefaultCredentials = false;
                }
                base.Url = value;
            }
        }


0 0
原创粉丝点击