抽象类中有一个构造函数好吗?

发布于 2024-10-01 04:12:08 字数 431 浏览 2 评论 0原文

抽象类中有一个构造函数好吗?

创建抽象类的构造函数是一个好的编程习惯吗?由于抽象类无法初始化,因此它们的子类被初始化。

以下是我的班级结构。

public abstract class Scheduler
{
    private Storyboard timer;

    protected Scheduler()
    {
        // initialize the timer here.
        timer = new Storyboard();

        this.PollInterval = 60;
    }
}


public class TaskScheduler : Scheduler
{
    public TaskScheduler()
        : base()
    {

    }
}

Is it good to have a constructor in abstract class?

is it a good programming practice to create constructor of abstract class? since abstract classes can not be initialized, their child classes are initialized.

Following is my class structure.

public abstract class Scheduler
{
    private Storyboard timer;

    protected Scheduler()
    {
        // initialize the timer here.
        timer = new Storyboard();

        this.PollInterval = 60;
    }
}


public class TaskScheduler : Scheduler
{
    public TaskScheduler()
        : base()
    {

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

本王不退位尔等都是臣 2024-10-08 04:12:08

是的,绝对没问题。仅仅因为构造函数只能由派生类调用并不意味着它没有用。例如,您可能有一个抽象类,它表示某种命名实体 - 将名称作为构造函数参数是有意义的。

可能值得对构造函数进行保护,以使其更加明显地表明您不能从其他地方调用它。

请注意,抽象类中有一个构造函数(或多个构造函数)确实会强制派生类构造函数通过它,但它不会强制派生类具有相同的构造函数签名。例如:

public abstract class NamedFoo
{
    private readonly string name;
    public string Name { get { return name; } }

    protected NamedFoo(string name)
    {
        this.name = name;
    }
}

public class DerivedFooWithConstantName
{
    public DerivedFooWithConstantName() : base("constant name")
    {
    }
}

在这种情况下,派生类构造函数“删除”参数(通过提供常量值作为抽象类构造函数的参数),但在其他情况下,它可以“添加”所需的参数,或者混合使用。

Yes, it's absolutely fine. Just because the constructor can only be called by derived classes doesn't mean it won't be useful. For example, you might have an abstract class which represents a named entity of some kind - it would make sense to take the name as a constructor parameter.

It would probably be worth making the constructor protected, to make it even more obvious that you can't just call it from elsewhere.

Note that there being a constructor (or multiple constructors) in an abstract class does force derived class constructors to go through it, but it doesn't force the derived classes to have the same constructor signatures. For example:

public abstract class NamedFoo
{
    private readonly string name;
    public string Name { get { return name; } }

    protected NamedFoo(string name)
    {
        this.name = name;
    }
}

public class DerivedFooWithConstantName
{
    public DerivedFooWithConstantName() : base("constant name")
    {
    }
}

In this case the derived class constructor is "removing" a parameter (by providing a constant value as the argument to the abstract class constructor) but in other cases it could "add" parameters that it required, or have a mixture.

话少情深 2024-10-08 04:12:08

绝对没有理由不在抽象基类中包含构造函数。

抽象类的初始化和工作方式与任何其他类一样。 abstract 关键字仅执行以下操作:

  • 它阻止类本身被直接实例化。它只能通过实例化继承类来实例化。与非抽象基类相比,这不会改变初始化的行为;

  • 它允许您在类中拥有抽象方法、属性和事件。

例如,如果您没有抽象方法、属性或事件,则可以通过使类的构造函数受保护来实现完全相同的结果(就像您所做的那样)。这也阻止了类被直接实例化。然而,与抽象类相比,行为并没有改变。

主要区别在于将方法、属性和事件声明为抽象的能力,只有当类被标记为抽象时才能执行此操作。

There is absolutely no reason not to have a constructor in an abstract base class.

The abstract class is initialized and works just like any other class. The abstract keywords only do the following:

  • It prevents the class itself to be instantiated directly. It can only be instantiated by instantiating an inherited class. This does not change the behavior of initialization compared to a not abstract base class;

  • It allows you to have abstract methods, properties and events in the class.

If you e.g. do not have abstract methods, properties or events, exactly the same result can be accomplished by making the constructor of a class protected (like you did). This also prevents the class to be instantiated directly. The behavior does not change however compared to an abstract class.

The primary difference then becomes the ability to declare methods, properties and events as abstract which you can only do when the class is marked abstract.

凉城凉梦凉人心 2024-10-08 04:12:08

在抽象类中拥有构造函数有时会很有用。这个问题是重复的,并且在相关帖子中进行了大量讨论。尽管它专门引用了 JAVA,但从概念上讲它也适用于 C#。

抽象类可以有构造函数吗?

Having constructor in abstract class can be useful at times. This question is a duplicate, and dealt great deal in an related post. Even though it specifically reference JAVA, conceptually it applies to C# also.

Can an abstract class have a constructor?

菊凝晚露 2024-10-08 04:12:08

抽象类型的构造函数只能由派生类型调用。由于公共构造函数创建类型的实例,而您无法创建抽象类型的实例,因此具有公共构造函数的抽象类型的设计不正确。 CA1012:抽象类型不应具有构造函数

通过更改来修复违规问题构造函数的可访问性从公共到受保护

例子:

namespace Sample  
{        
    public abstract class Book      
    {          
        protected Book()           
        {          
        }      
    } 
} 

Constructors on abstract types can be called only by derived types. Because public constructors create instances of a type, and you cannot create instances of an abstract type, an abstract type that has a public constructor is incorrectly designed. CA1012: Abstract types should not have constructors

Fix the violation by changing the accessibility of the constructor from public to protected.

Example:

namespace Sample  
{        
    public abstract class Book      
    {          
        protected Book()           
        {          
        }      
    } 
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文