静态 Class 与受保护的构造函数
Static Class vs Protected Constructor
我在 class 中收到一条警告消息,比如
Add a Protected
constructor or the static
keyword to the class declaration
解决方案
我尝试了以下两种方法后错误消失了,
static
class 没有 constructor
public static class Program {
}
非 static
class with protected
using constructor
public class Program
{
protected Program() { }
}
问题:
那么我上面的解决方案中提到的 Static Class 与 Protected Constructor 之间有什么区别?哪个最好用?
实例化 class 类型时调用静态构造函数。当创建 class 的实例时,将调用受保护的构造函数。受保护的部分意味着只有继承 class 的 classes 才能调用它。
您可能在 class 中只有静态成员,并且代码分析器假定您的意图是无法创建 class 的实例,因此它要求您要么class 静态
public static class Program {
//...static members
}
或者放一个 protected/private 构造器
public class Program {
protected Program { //OR private
}
//...static members
}
以防止 class 的实例被初始化。
A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated.
引用Static Classes and Static Class Members (C# Programming Guide)
受保护的构造函数意味着只有派生的classes可以调用构造函数
并且私有构造函数不允许任何其他 classes 使用私有构造函数
初始化 class
A static
class 不需要实例来访问其成员。 static
class 不能有实例成员(例如,静态 class 上不允许 public int MyNumber;
,因为静态 class 上只允许静态成员)。不过,非静态 class 允许实例成员和静态成员。具有 protected
构造函数的 class 只能拥有由其自身创建的实例或从其继承的实例。
public class Program
{
protected Program()
{
// Do something.
}
public static Program Create()
{
// 100% Allowed.
return new Program();
}
public void DoSomething()
{
}
}
public static class AnotherClass
{
public static Program CreateProgram()
{
// Not allowed since Program's constructor is protected.
return new Program();
}
}
public class SubProgram : Program
{
protected SubProgram()
{
// Calls Program() then SubProgram().
}
public new static Program Create()
{
// return new Program(); // We would need to move the SubProgram class INSIDE the Program class in order for this line to work.
return new SubProgram();
}
}
Program.Create(); // Can be called since Create is public and static function.
Program.DoSomething() // Can't be called because an instance has not been instantiated.
var test = Program.Create();
test.DoSomething(); // Can be called since there is now an instance of Program (i.e. 'test').
AnotherClass.CreateProgram(); // Can't be called since Program's constructor is protected.
SubProgram.Create(); // Can be called since SubProgram inherits from Program.
至于性能,这种区别与性能并没有太大关系。
Static Constructor:class类型实例化时调用一次,用于初始化静态成员。不创建 class 的实例。
受保护构造函数:只能由class或继承它的class调用的构造函数。
最好的做法是,如果您只希望继承的 classes 能够创建您的 class.你可以两者兼得。
public class MyClass
{
static readonly long _someStaticMember;
private bool _param;
static MyClass()
{
//Do Some Logic
_someStaticMember = SomeValueCalculated;
}
protected MyClass(bool param)
{
_param = param;
}
}
public class ChildClass: MyClass
{
public ChildClass(bool param) : base(param);
}
public class NotChildClass
{
public MyClass someObject = new MyClass(true); //Will Fail
}
我在 class 中收到一条警告消息,比如
Add a
Protected
constructor or thestatic
keyword to the class declaration
解决方案
我尝试了以下两种方法后错误消失了,
static
class 没有 constructor
public static class Program {
}
非 static
class with protected
using constructor
public class Program
{
protected Program() { }
}
问题:
那么我上面的解决方案中提到的 Static Class 与 Protected Constructor 之间有什么区别?哪个最好用?
实例化 class 类型时调用静态构造函数。当创建 class 的实例时,将调用受保护的构造函数。受保护的部分意味着只有继承 class 的 classes 才能调用它。
您可能在 class 中只有静态成员,并且代码分析器假定您的意图是无法创建 class 的实例,因此它要求您要么class 静态
public static class Program {
//...static members
}
或者放一个 protected/private 构造器
public class Program {
protected Program { //OR private
}
//...static members
}
以防止 class 的实例被初始化。
A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated.
引用Static Classes and Static Class Members (C# Programming Guide)
受保护的构造函数意味着只有派生的classes可以调用构造函数
并且私有构造函数不允许任何其他 classes 使用私有构造函数
初始化 classA static
class 不需要实例来访问其成员。 static
class 不能有实例成员(例如,静态 class 上不允许 public int MyNumber;
,因为静态 class 上只允许静态成员)。不过,非静态 class 允许实例成员和静态成员。具有 protected
构造函数的 class 只能拥有由其自身创建的实例或从其继承的实例。
public class Program
{
protected Program()
{
// Do something.
}
public static Program Create()
{
// 100% Allowed.
return new Program();
}
public void DoSomething()
{
}
}
public static class AnotherClass
{
public static Program CreateProgram()
{
// Not allowed since Program's constructor is protected.
return new Program();
}
}
public class SubProgram : Program
{
protected SubProgram()
{
// Calls Program() then SubProgram().
}
public new static Program Create()
{
// return new Program(); // We would need to move the SubProgram class INSIDE the Program class in order for this line to work.
return new SubProgram();
}
}
Program.Create(); // Can be called since Create is public and static function.
Program.DoSomething() // Can't be called because an instance has not been instantiated.
var test = Program.Create();
test.DoSomething(); // Can be called since there is now an instance of Program (i.e. 'test').
AnotherClass.CreateProgram(); // Can't be called since Program's constructor is protected.
SubProgram.Create(); // Can be called since SubProgram inherits from Program.
至于性能,这种区别与性能并没有太大关系。
Static Constructor:class类型实例化时调用一次,用于初始化静态成员。不创建 class 的实例。
受保护构造函数:只能由class或继承它的class调用的构造函数。
最好的做法是,如果您只希望继承的 classes 能够创建您的 class.你可以两者兼得。
public class MyClass
{
static readonly long _someStaticMember;
private bool _param;
static MyClass()
{
//Do Some Logic
_someStaticMember = SomeValueCalculated;
}
protected MyClass(bool param)
{
_param = param;
}
}
public class ChildClass: MyClass
{
public ChildClass(bool param) : base(param);
}
public class NotChildClass
{
public MyClass someObject = new MyClass(true); //Will Fail
}