目录

一、涉及到的知识点

1.密封类定义

2.何时使用密封类

3.使用密封类的注意事项

二、实例1

三、实例2

1.源码

2.生成效果


        在C#中,密封类(sealed class)是一种不能被其他类继承的类。它用于防止其他类继承它的功能和属性。 

一、涉及到的知识点

1.密封类定义

        密封类的突出特点是不能被继承,通过密封类封装用户信息可以增加用户信息的安全性。使用密封类密封用户的登录用户名和密码,以保证其安全性。

        密封类可以用来限制扩展性,如果密封了某个类,则其他类不能从该类继承;如果密封了某个成员,则派生类不能重写该成员的实现。默认情况下,不应该密封类型和成员。密封可以防止对库的类型和成员进行自定义,但也会影响某些开发人员对可用性的认识。

        在C#中声明密封类时需要使用sealed关键字,具体语法格式如下:

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

2.何时使用密封类

         C#中使用密封类时,如果类满足如下条件,则应将其密封:

  • 类是静态类。
  • 类包含带有安全敏感信息的继承的受保护成员。
  • 类继承多个虚成员,并且密封每个成员的开发和测试开销明显大于密封整个类。
  • 类是一个要求使用反射进行快速搜索的属性,密封属性可提高反射在检索属性时的性能。

3.使用密封类的注意事项

  •  密封类不能作为基类被继承,但它可以继承别的类或接口。
  • 在密封类中不能声明受保护成员或虚成员,因为受保护成员只能从派生类进行访问,而虚成员只能在派生类中重写。
  • 由于密封类的不可继承性,因此密封类不能声明为抽象的。

二、实例1

// 密封类
namespace _118_1
{
    sealed class SealedClass
    {
        public static void GetMessage()
        {
            Console.WriteLine("这是一个密封类。");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            ArgumentNullException.ThrowIfNull(args);

            SealedClass.GetMessage();

            // 尝试创建一个继承自密封类的类
            // 这将导致编译错误,因为密封类不能被继承
            // class SealedChildClass : SealedClass
            // {
            //     public void ChildMethod()
            //     {
            //         Console.WriteLine("这是密封类的子类。");
            //     }
            // }

            Console.ReadKey();
        }
    }
}
//运行结果:
/*
这是一个密封类。

 */

三、实例2

        使用密封类密封用户的登录用户名和密码,以保证其安全性。

1.源码

// 使用密封类密封用户信息
namespace _118
{
    public partial class Form1 : Form
    {
        private GroupBox? groupBox1;
        private Button? button2;
        private Button? button1;
        private TextBox? textBox2;
        private TextBox? textBox1;
        private Label? label2;
        private Label? label1;

        public Form1()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            Load += Form1_Load;
        }

        private void Form1_Load(object? sender, EventArgs e)
        {
            // 
            // label1
            // 
            label1 = new Label
            {
                AutoSize = true,
                Location = new Point(64, 37),
                Name = "label1",
                Size = new Size(56, 17),
                TabIndex = 0,
                Text = "用户名:"
            };
            // 
            // label2
            // 
            label2 = new Label
            {
                AutoSize = true,
                Location = new Point(64, 69),
                Name = "label2",
                Size = new Size(56, 17),
                TabIndex = 1,
                Text = "密   码:"
            };
            // 
            // textBox1
            // 
            textBox1 = new TextBox
            {
                Location = new Point(129, 31),
                Name = "textBox1",
                Size = new Size(100, 23),
                TabIndex = 2
            };
            // 
            // textBox2
            // 
            textBox2 = new TextBox
            {
                Location = new Point(129, 66),
                Name = "textBox2",
                Size = new Size(100, 23),
                TabIndex = 3
            };
            // 
            // button1
            // 
            button1 = new Button
            {
                Location = new Point(64, 101),
                Name = "button1",
                Size = new Size(75, 23),
                TabIndex = 4,
                Text = "确定",
                UseVisualStyleBackColor = true
            };
            button1.Click += Button1_Click;
            // 
            // button2
            // 
            button2 = new Button
            {
                Location = new Point(154, 101),
                Name = "button2",
                Size = new Size(75, 23),
                TabIndex = 5,
                Text = "取消",
                UseVisualStyleBackColor = true
            };
            button2.Click += Button2_Click;
            // 
            // groupBox1
            // 
            groupBox1 = new GroupBox
            {
                Location = new Point(12, 12),
                Name = "groupBox1",
                Size = new Size(300, 152),
                TabIndex = 0,
                TabStop = false,
                Text = "登录窗口"
            };
            groupBox1.Controls.Add(button2);
            groupBox1.Controls.Add(button1);
            groupBox1.Controls.Add(textBox2);
            groupBox1.Controls.Add(textBox1);
            groupBox1.Controls.Add(label2);
            groupBox1.Controls.Add(label1);
            groupBox1.SuspendLayout();

            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            ClientSize = new Size(324, 176);
            Controls.Add(groupBox1);
            Name = "Form1";
            Text = "使用密封类密封用户信息";
            groupBox1.ResumeLayout(false);
            groupBox1.PerformLayout();
        }
        /// <summary>
        /// 按钮事件:登录
        /// 调用密封类
        /// </summary>
        private void Button1_Click(object? sender, EventArgs e)
        {
            if (textBox1!.Text == "" || textBox2!.Text == "")
            {
                MessageBox.Show("用户名和密码不能为空", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                MyClass myclass = new()
                {
                    Name = textBox1.Text,            //为密封类中的编号属性赋值
                    Pwd = textBox2.Text              //为密封类中的名称属性赋值
                };	                                 //实例化密封类对象
                MessageBox.Show("登录成功,用户名:" + myclass.Name + " 密码:" + myclass.Pwd, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// <summary>
        /// 退出当前应用程序
        /// </summary>
        private void Button2_Click(object? sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// 通过sealed关键字声明密封类,防止类被继承,有效保护重要信息
        /// </summary>
        public sealed class MyClass
        {
            private string name = "";   //string类型变量,用来记录用户名
            private string pwd = "";    //string类型变量,用来记录密码
            /// <summary>
            /// 用户名,属性
            /// </summary>
            public string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    name = value;
                }
            }
            /// <summary>
            /// 密码,属性
            /// </summary>
            public string Pwd
            {
                get
                {
                    return pwd;
                }
                set
                {
                    pwd = value;
                }
            }
        }
    }
}

2.生成效果

 C#使用密封类密封用户信息-LMLPHP

02-14 12:24