70

我正在查看一些用于扩展 VS2010 中的语言支持的 C# 代码(Ook 示例)。我看到一些类叫做internal sealed class

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

4

6 回答 6

150

这是一个类:

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

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

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

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

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

于 2011-01-06T19:55:15.140 回答
18
  • 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
    
于 2011-01-06T19:55:59.923 回答
12

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

于 2011-01-06T19:54:22.910 回答
6

一个internal sealed类是:

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

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

于 2011-01-06T19:55:14.960 回答
4

内部意味着它只能在同一个组件中使用,

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

无法继承的封印

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

于 2011-01-06T19:55:07.520 回答
2

INTERNAL

Internal types or members are accessible only within files in the same assembly.

Example

// 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

First of all, let's start with a definition; sealed is a modifier which if applied to a class make it non-inheritable and if applied to virtual methods or properties makes them non-ovveridable.

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

An example of its usage is specialized class/method or property in which potential alterations can make them stop working as expected (for example, the Pens class of the System.Drawing namespace).

...
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; }
        ...
    }
}

Because a sealed class cannot be inherited, it cannot be used as base class and by consequence, an abstract class cannot use the sealed modifier. It's also important to mention that structs are implicitly sealed.

Example

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
}

Microsoft documentation

于 2020-03-02T16:04:02.053 回答