我在查看一些扩展VS2010语言支持的C#代码(Ook示例)时,看到了一些称为internal sealed class
的类。
这些类是做什么用的?人们会使用它们吗?
我在查看一些扩展VS2010语言支持的C#代码(Ook示例)时,看到了一些称为internal sealed class
的类。
这些类是做什么用的?人们会使用它们吗?
这是一个类,具有以下特点:
internal
:只能从定义它的程序集(或友元程序集)中访问。sealed
:不能被继承。将类标记为internal
是防止程序集外部用户使用它们的一种方法。这实际上是一种设计封装的形式,我认为标记那些不属于预期公共API/对象模型的类型为internal
是一种好的实践。从长远来看,这可以防止你的库用户耦合到你没有打算让他们耦合的类型上。这种意外的耦合会影响你改变和演进库实现方式的能力,因为你不能更改它们而不破坏客户端。使用internal
有助于将库的公共和可用表面积限制在预期范围内。
将类标记为sealed
防止这些类被继承。这是一种相当激进的设计意图,有时候很有用,如果一个类已经专门化到不应该通过继承直接或通过覆盖其行为添加其他功能。
internal
和sealed
以非常不同的方式修改类型,但它们可以一起使用。
注意:你对internal
有进一步的作用域控制,因为你可以将一组其他程序集定义为'友元'。这些友元程序集可以访问你的internal
类型。这对于定义一组协同程序集(如生产和测试程序集)非常有用。通常情况下,测试程序集能够看到它正在测试的程序集中的所有类型是有益的。
InitializeObject
设置为sealed
不是更好吗? - Arijooninternal: 一个只能在同一程序集内访问的类。
Assembly1.dll:
namespace test {
internal class InternalClass {
}
public class PublicClass {
}
}
Assembly2.dll:
using test;
...
InternalClass c1; // Error
PublicClass c2; // OK
sealed: 一个无法被继承的类
sealed class SealedClass { ... }
class ChildClass : SealedClass {} //ERROR
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; }
...
}
}
由于封闭类无法被继承,因此无法用作基类,因此抽象类无法使用sealed修饰符。还需要注意的是,结构体默认是封闭的。
示例
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
}
微软文档