chenliezhong

导航

this()无参构造函数的小技巧

在开发中,类中的this()是对应无参构造函数。所以在调用有参构造函数的时候,想加载默认的无参构造函数可在方法体后面添加:this()。
public partial class MessageWindow : Window
    {
        public MessageWindow()
        {
            InitializeComponent();
            this.Loaded += IssueMessageWindow_Loaded;
        }
        private void IssueMessageWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //实现默认代码
        }
        public MessageWindow(EasyButton easy, string msgInfo) 
        {
            InitializeComponent();
            this.Loaded += IssueMessageWindow_Loaded;
            //实现逻辑
        }
        public MessageWindow(EasyButton easy, string msgInfo, int timeout)
        {
            InitializeComponent();
            this.Loaded += IssueMessageWindow_Loaded;
            //实现逻辑
        }
        public MessageWindow(string msgInfo, int timeout)
        {
            InitializeComponent();
            this.Loaded += IssueMessageWindow_Loaded;
            //实现逻辑
        }
  }
 
 
更改为:
 public partial class MessageWindow : Window
    {
        public MessageWindow()
        {
            InitializeComponent();
            this.Loaded += IssueMessageWindow_Loaded;
        }
        private void IssueMessageWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //实现默认代码
        }
        public MessageWindow(EasyButton easy, string msgInfo) : this()
        {
            //实现逻辑
        }
        public MessageWindow(EasyButton easy, string msgInfo, int timeout) : this()
        {
            //实现逻辑
        }
        public MessageWindow(string msgInfo, int timeout) : this()
        {
            //实现逻辑
        }
  }
 
 
 

posted on 2019-08-21 15:45  chenliezhong  阅读(678)  评论(0编辑  收藏  举报