使用密封类封装用户信息

实现效果:

  

知识运用:

  使用sealed关键字声明密封类

  访问修饰符 sealed clsss 类名:基类或接口{ //l类成员 }

实现代码:

        private void button1_Click(object sender, EventArgs e)
        {
            Userinfo userinfo=new Userinfo ();
            userinfo.U="admin";
            userinfo.P="13596";
            if (textBox1.Text == userinfo.U & textBox2.Text == userinfo.P)
                MessageBox.Show("用户名:" + textBox1.Text + "密码:" + textBox2.Text, "登陆成功",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show("用户或密码不正确","错误");
        }
        /// <summary>
        /// 通过sealed关键字声明密封类 防止被继承 以保护重要信息
        /// </summary>
        sealed class Userinfo {
            private string user = "";  
            private string pass="";
            /// <summary>
            /// 用户名 密码
            /// </summary>
            public string U { get { return user; } set { user =value; } }
            public string P { get { return pass; } set { pass = value; } }
        }

补充说明:

  •  密封类不能作为基类被继承,但它可以继承其他类或接口
  • 在密封类中不能声明受保护的成员和虚方法
  • 因为密封类的不可继承性,因此不能声明为抽象的
原文地址:https://www.cnblogs.com/feiyucha/p/10080681.html