sealed密封类

来源:互联网 发布:包头广电网络宽带资费 编辑:程序博客网 时间:2024/04/25 19:35

<1>

我知道string类型就是一个sealed类

string类型的定义是这样的:

public sealed class String : IComparable, ICloneable, IConvertible, IComparable<string>, IEnumerable<char>, IEnumerable, IEquatable<string>

那么为什么string类前面要加sealed关键字呢?

答案:

1,子类如果继承string类后可能会对string类进行修改,这样可能会改变string类的特性。

2,CLR对string提交了各种特殊的操作方式,如果有很多类继承了string类,则CLR需要对更多的类型提供特殊的操作,这样有可能降低性能。

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace sealed密封类{    public sealed class Person:Object //密封类不能被继承。但是密封类可以继承别的非密封类    {         //如果你的类不想被继承的话就写成密封类    }    /*    public class Student:Person //Student无法继承Person类    {             }    */    class Program    {        static void Main(string[] args)        {        }    }}


0 0
原创粉丝点击