Home > 类库下载 > net class library > Sealed seal type

Sealed seal type

高洛峰
Release: 2016-11-01 13:32:24
Original
1615 people have browsed it

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
#region Overview
//Using sealed in a class declaration prevents other classes from inheriting this class; using it in a method declaration The sealed modifier prevents extending classes from overriding this method.
//The sealed modifier is mainly used to prevent unintentional derivation, but it can also promote certain runtime optimizations. Specifically, since a sealed class will never have any derived classes, calls to virtual function members of instances of the sealed class can be converted to non-virtual calls for processing.

//密封类://密封类在声明中使用sealed 修饰符,这样就可以防止该类被其它类继承。如果试图将一个密封类作为其它类的基类,C#将提示出错。理所当然,密封类不能同时又是抽象类,因为抽象总是希望被继承的。//在哪些场合下使用密封类呢?实际上,密封类中不可能有派生类。如果密封类实例中存在虚成员函数,该成员函数可以转化为非虚的,函数修饰符virtual 不再生效。#endregionnamespace Sealed密封类
{    class NOSealed
    {        public static void OO()
        {
            Console.WriteLine("没有使用密封");
        }
    }

    sealed class YESSealed
    {        public static void OO()
        {
            Console.WriteLine("使用了密封");
        }
    }    class MyClass : NOSealed //YESSealed 那就错了
    {        public new void OO()
        {
            Console.WriteLine("没有继承密封");
        }
    }    //密封类不可以被继承,可以被调用
    sealed class mysealed   //声明为密封类  
    {        public int x;        public int y;
    }    class Program
    {        static void Main(string[] args)
        {
            NOSealed.OO();
            MyClass M = new MyClass();
            M.OO();            //调用密封类
            YESSealed.OO();
            mysealed m = new mysealed();
            m.x = 100;
            m.y = 200;
            Console.WriteLine("x={0}, y = {1}", m.x, m.y);
            Console.ReadLine();

            Console.ReadKey();
        }
    }
}
Copy after login


Related labels:
c#
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template