第三部分 类和结构5---------------

   如果你希望确保一个类不被作为基类使用,那么就在定义这个类时使用sealed关键字.唯一的限制是抽象类不能作为封闭的类使用,因为抽象类的本质决定它们必须被作为基类使用.封闭类的用途是防止意外的派生操作,但将类定义为封闭的也会启用某些运行时优化功能.具体的说,因为编译器确定这个类没有任何派生类,所以可以将封闭类实例上的虚拟函数成员调用转换为非虚拟调用.下面是一个封闭类的例子:

  

using System;

 

sealed class Point

    {

          public Point(int x,int y)

                      {

                                  X=x;

                                  Y=y;

           }

          public int X;

          public int Y;

}

/*

  Uncommenting this class and attempting to build the application will result in a compiler error ,as sealed classes cannot be derived from.

class MyPoint:Point

  {]

*/

class SealedApp

    {

           public static void Main()

                           {

                                     point pt=new Point(6,16);

                                     Console.WriteLine("x-{0},y={1}",pt.X,pt.Y);

                          }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值