繁体   English   中英

什么是 C# 中的内部密封类?

[英]What is an internal sealed class in C#?

我正在浏览一些 C# 代码,用于在 VS2010 中扩展语言支持(好的例子)。 我看到了一些叫做internal sealed class

这些有什么作用? 一个人会使用它们吗?

这是一个类:

  • internal :只能从它定义的程序集(或朋友程序集) internal访问。
  • sealed :不能继承。

将类标记为internal类是一种防止程序集的外部用户使用它们的方法。 这确实是一种设计封装形式,恕我直言,将不属于预期公共 API\\object 模型的类型标记为internal是一种很好的做法。 从长远来看,这可以防止您的库的用户将自己耦合到您不希望他们使用的类型。 这种意外的耦合会损害您更改和改进库实现方式的能力,因为您无法在不破坏客户的情况下更改它们。 使用internal有助于将图书馆的公共和可用表面积保持在预期范围内。

将类标记为sealed可防止这些类被继承。 这是一个非常激烈的设计意图,如果一个类已经非常专业,以至于不应该通过继承直接或通过覆盖其行为向它添加其他功能是明智的,那么有时这很有用。

internalsealed修改类型以完全不同的方式,但它们可以一起使用。

注意您可以对internal进行一些进一步的范围控制,因为您可以将一组其他程序集定义为“朋友”。 这些友元程序集可以访问您的internal类型。 这对于定义协作组件集(例如生产和测试组件)非常有用。 通常希望测试程序集可以看到它正在测试的程序集中的所有类型。

  • internal:只能在同一个程序集中访问的类。

    程序集1.dll:

     namespace test { internal class InternalClass { } public class PublicClass { } }

    程序集2.dll:

     using test; ... InternalClass c1; // Error PublicClass c2; // OK
  • 密封:不能派生自的类

    sealed class SealedClass { ... } class ChildClass : SealedClass {} //ERROR

内部意味着该成员可被同一程序集中定义的其他类型访问。 Sealed 类是抽象的对立面。 它可以被实例化,但不能作为基类。 密封类的主要原因是为了防止您的用户摆弄它并破坏它。 密封类也允许某些非密封类无法实现的编译器优化。

internal sealed类是这样的:

internal - 只能从同一程序集中访问
sealed - 不能被子类化

换句话说,您无法直接使用它。

内部意味着它只能在同一个程序集中使用,

internal 关键字是类型和类型成员的访问修饰符。 内部类型或成员只能在同一程序集中的文件中访问

无法继承的密封

密封类不能被继承。 使用密封类作为基类是错误的。 在类声明中使用sealed 修饰符来防止类的继承。

内部

内部类型或成员只能在同一程序集中的文件中访问。

示例

// Assembly1.cs  
// Compile with: /target:library  
internal class BaseClass   
{  
   public static int intM = 0;  
} 
// Assembly1_a.cs  
// Compile with: /reference:Assembly1.dll  
class TestAccess   
{  
   static void Main()   
   {  
      var myBase = new BaseClass();   // compile error 
   }  
} 

密封

首先,让我们从定义开始; Sealed 是一个修饰符,如果应用于类,则使其不可继承,如果应用于虚拟方法或属性,则使其不可超越

public sealed class A { ... }
public class B 
{
    ...
    public sealed string Property { get; set; }
    public sealed void Method() { ... }
}

其用法的一个示例是特殊的类/方法或属性,其中潜在的更改可以使它们按预期停止工作(例如,System.Drawing 命名空间的 Pens 类)。

...
namespace System.Drawing
{
    //
    // Summary:
    //     Pens for all the standard colors. This class cannot be inherited.
    public sealed class Pens
    {
        public static Pen Transparent { get; }
        public static Pen Orchid { get; }
        public static Pen OrangeRed { get; }
        ...
    }
}

因为密封类不能被继承,它不能用作基类,因此抽象类不能使用密封修饰符 同样重要的是要提到结构是隐式密封的

示例

public class BaseClass {
    public virtual string ShowMessage()
    {
        return "Hello world";
    }
    public virtual int MathematicalOperation(int x, int y)
    {
        return x + y;
    }
}
public class DerivedClass : BaseClass {
    public override int MathematicalOperation(int x, int y) 
    {
        // since BaseClass has a method marked as virtual, DerivedClass can override it's behavior
        return x - y;
    }
    public override sealed string ShowMessage()
    {
        // since BaseClass has a method marked as virtual, DerivedClass can override it's behavior but because it's sealed prevent classes that derive from it to override the method
        return "Hello world sealed";
    }
}
public class DerivedDerivedClass : DerivedClass
{
    public override int MathematicalOperation(int x, int y)
    {
        // since BaseClass has a method marked as virtual, DerivedClass can override it's behavior
        return x * y;
    }
    public override  string ShowMessage() { ... } // compile error
}
public sealed class SealedClass: BaseClass {
    public override int MathematicalOperation(int x, int y)
    {
        // since BaseClass has a method marked as virtual, DerivedClass can override it's behavior
        return x * y;
    }
    public override string ShowMessage()
    {
        // since BaseClass has a method marked as virtual, DerivedClass can override it's behavior but because it's sealed prevent classes that derive from it to override the method
        return "Hello world";
    }
}
public class DerivedSealedClass : SealedClass
{
    // compile error
}

微软文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM