C# 从类或抽象类继承

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/603661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 09:59:10  来源:igfitidea点击:

Inherit from a class or an abstract class

c#.netinheritanceclass

提问by Joan Venge

If you have several classes where you want them to inherit from a base class for common functionality, should you implement the base class using a class or an abstract class?

如果您有多个类,您希望它们从基类继承以实现通用功能,您应该使用类还是抽象类来实现基类?

采纳答案by Hawk Kroeger

That depends, if you never want to be able to instantiate the base class then make it abstract. Otherwise leave it as a normal class.

这取决于,如果您永远不想实例化基类,则将其设为抽象。否则将其保留为普通班级。

回答by Suroot

I would say if you are not planning on calling the base class by itself, the then you should define it as an abstract class.

我会说如果您不打算单独调用基类,那么您应该将其定义为抽象类。

回答by Trevor Bramble

The depends on whether you want the base class to be implemented on its own or not.

这取决于您是否希望自己实现基类。

As an abstract class, you can't make objects from it.

作为抽象类,您不能从中创建对象。

回答by casperOne

It depends, does it make sense for the base class in question to exist on it's own without being derived from? If the answer is yes, then it should be a regular class, otherwise, it should be an abstract class.

这取决于,有问题的基类在没有派生自的情况下独立存在是否有意义?如果答案是肯定的,那么它应该是一个普通类,否则它应该是一个抽象类。

回答by Andrew Hare

If the base class ought not to be instantiated then make it an abstract class - if the base class needs to be instantiated then don't make it abstract.

如果不应该实例化基类,则将其设为抽象类 - 如果需要实例化基类,则不要将其设为抽象类。

In this example it makes sense to make the base class abstract as the base class does not have any concrete meaning:

在这个例子中,使基类抽象是有意义的,因为基类没有任何具体含义:

abstract class WritingImplement
{
    public abstract void Write();
}

class Pencil : WritingImplement
{
    public override void Write() { }
}

However in this next example you can see how the base class does have concrete meaning:

但是,在下一个示例中,您可以看到基类如何具有具体含义:

class Dog
{
    public virtual void Bark() { }
}

class GoldenRetriever : Dog
{
    public override void Bark() { }
}

It is all pretty subjective really - you ought to be able to make a pretty good judgment call based on the needs of your particular domain.

这真的是非常主观的 - 您应该能够根据您的特定领域的需求做出非常好的判断。

回答by Kris

Abstract classes are great for predefined functionality, for example - when know the minimum exact behaviour a class should expose but not what data it should use to do it or the exact implementation.

抽象类非常适合预定义的功能,例如 - 当知道类应该公开的最小确切行为但不知道它应该使用什么数据或确切实现时。

abstract class ADataAccess
{
    abstract public void Save();
}

Normal (non abstract) classes can be great for similar things but you have to know the implementation specifics to be able to write them.

普通(非抽象)类可以很好地处理类似的事情,但您必须知道实现细节才能编写它们。

public class DataAccess
{
    public void Save()
    {
        if ( _is_new )
        {
            Insert();
        }
        else if ( _is_modified )
        {
            Update();
        }
    }
}

Also, you could use interfaces (individually or on classes, whether abstract or not) to define the same sort of prototype definition.

此外,您可以使用接口(单独或在类上,无论是否抽象)来定义相同类型的原型定义。

interface ISaveable
{
    void Save();
    void Insert();
    void Update();
}

class UserAccount : ISavable
{
    void ISavable.Save() { ... }
    void ISavable.Insert() { ... }
    void ISavable.Update() { ... }
}

Yet another option may be using generics

另一种选择可能是使用泛型

class GenDataAccess<T>
{
    public void Save()
    {
        ...
    }
}

All these methods can be used to define a certain prototype for classes to work with. Ways to make sure that code A can talk to code B. And of course you can mix and match all of the above to your liking. There is no definite right way but I like defining interfaces and abstract classes, then referring to the interfaces. That way eliminates some of the thought requirements for "plumbing" in higher level classes while keeping the maximum flexibility. (having interfaces takes away the requirement of using the abstract base class, but leaves it as an option).

所有这些方法都可用于定义要使用的类的特定原型。确保代码 A 可以与代码 B 对话的方法。当然,您可以根据自己的喜好混合和匹配上述所有内容。没有明确的正确方法,但我喜欢定义接口和抽象类,然后引用接口。这种方式消除了更高级别课程中“管道”的一些思想要求,同时保持最大的灵活性。(拥有接口消除了使用抽象基类的要求,但将其作为一个选项)。

回答by Ben S

Abstract classes are for partially implemented classes.

抽象类用于部分实现的类。

By itself doesn't make sense to have an instance of an abstract class, it needs to be derived. If you would like to be able to create the base class it cannot be abstract.

拥有抽象类的实例本身没有意义,它需要派生。如果您希望能够创建基类,则它不能是抽象的。

I like to think of abstract classes as interfaces which have some members pre-defined since they are common to all sub-classes.

我喜欢将抽象类视为具有一些预定义成员的接口,因为它们对所有子类都是通用的。

回答by JaredPar

Think of this a different way

以不同的方式思考这个

Is my a base class a complete object on it's own?

我的基类本身就是一个完整的对象吗?

If the answer is no, then make it abstract. If it's yes then you likely want to make it a concrete class.

如果答案是否定的,那么把它抽象化。如果是,那么您可能想让它成为一个具体的类。

回答by Steven A. Lowe

I suggest:

我建议:

  • Make an interface.
  • Implement the interface in your base class.
  • Make the base class a real class, not abstract (see below for why).
  • 做一个接口。
  • 在基类中实现接口。
  • 使基类成为真正的类,而不是抽象类(原因见下文)。

The reason I prefer real classes instead of abstract classes is that abstract classes cannot be instantiated, which limits future options unnecessarily. For example, later on I may need the state and methods provided by the base class but cannot inherit and do not need to implement the interface; if the base class is abstract I am out of luck, but if the base class is a regular class then I can create an instance of the base class and hold it as a component of my other class, and delegate to the instance to reuse the state/methods provided.

我更喜欢真正的类而不是抽象类的原因是抽象类不能被实例化,这不必要地限制了未来的选择。比如后面我可能需要基类提供的状态和方法,但是不能继承,不需要实现接口;如果基类是抽象的,我就不走运了,但是如果基类是常规类,那么我可以创建基类的实例并将其作为其他类的组件,并委托给该实例以重用提供的状态/方法。

Yes this does not happen often, but the point is: making the base class abstract prevents this kind of reuse/solution, when there is no reason to do so.

是的,这并不经常发生,但关键是:在没有理由的情况下,使基类抽象可以防止这种重用/解决方案。

Now, if instantiating the base class would somehow be dangerous, then make it abstract - or preferably make it less dangerous, if possible ;-)

现在,如果实例化基类在某种程度上是危险的,那么让它抽象 - 或者最好让它不那么危险,如果可能;-)

回答by Jamie Keeling

Think of it like a bank account:

把它想象成一个银行账户:

You can make a generic abstract base account called "Account", this holds basic information such as customer details.

您可以创建一个名为“帐户”的通用抽象基本帐户,其中包含基本信息,例如客户详细信息。

You can then create two derived classes called "SavingAccount" or "DebitAccount" which can have their own specific behaviour whilst benefiting from the base class behaviour.

然后,您可以创建两个名为“SavingAccount”或“DebitAccount”的派生类,它们可以拥有自己的特定行为,同时受益于基类行为。

This is a situation where the customer must have either a Savings Account or a Debit Account, a generic "Account" is not allowed as it is not very popular in the real world to have just an account of no description.

在这种情况下,客户必须拥有储蓄账户或借记账户,一般的“账户”是不允许的,因为在现实世界中只有一个没有描述的账户并不是很流行。

If you can create a similar scenario for your needs, abstract is the way to go.

如果您可以为您的需求创建类似的场景,那么抽象就是您要走的路。