简体   繁体   English

没有抽象方法的抽象类

[英]Abstract class with no abstract methods

借助C#,可以定义不包含抽象方法的抽象类,那么将类定义为ABSTRACT有什么用?

an abstract class with properties can be used to represent a base class in inheritance. 具有属性的abstract类可用于表示继承中的基类。 An abstract class can not be instantiated. 无法实例化抽象类。 that may fit well as we dont want to let otheres to create objects for our base class. 这可能很合适,因为我们不想让其他人为我们的基类创建对象。

You can define abstract class to define the abstract nature of the object. 您可以定义抽象类来定义对象的抽象性质。 Ex: Animal has Eyes. 例如:动物有眼睛。 Dog can be inherited from Animal so that it will have Eyes. 狗可以从动物继承而来,因此具有眼睛。 We may create a Dog object. 我们可以创建一个Dog对象。 But we don't want to create an Animal object. 但是我们不想创建Animal对象。

from msdn , 来自msdn

The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. 抽象类的目的是提供多个派生类可以共享的基类的通用定义。 For example, a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class. 例如,类库可以定义一个抽象类,用作其许多功能的参数,并要求使用该库的程序员通过创建派生类来提供自己的类实现。

With refrence to C#, it is possible to define abstract class containing no abstract methods 通过参考C#,可以定义不包含抽象方法的抽象类

Yes, absolutely 是的,一点没错

then whats the use of defining class as ABSTRACT? 那么将类定义为ABSTRACT有什么用?

Mostly to communicate the fact the the class should be derived. 主要是为了传达该类应该派生的事实。 A similar effect can be achieved by making the constructor protected: no one will be able to create an instance of the class without inheriting from it. 通过使构造函数受到保护,可以达到类似的效果:没有一个类的实例可以直接从其继承而来。

将类定义为抽象可防止直接实例化该类。

Basically, I use as a rule of thumb that you should always define classes abstract when they MUST be inherited to be instantiated. 基本上,我的经验法则是当必须继承要实例化的类时,应始终定义抽象类。 Because abstract classes themselves cannot. 因为抽象类本身不能。

For example. 例如。 Say you have a Building class, which is abstract. 假设您有一个建筑类,它是抽象的。 It must then be instantiated through a derived class, for example, a Bank or House. 然后必须通过派生类(例如,银行或房屋)来实例化它。 The fact that there are no abstract methods in the Building class has nothing to do with it being abstract or not. Building类中没有抽象方法的事实与它是否抽象无关。

A simple rule is that make abstract classes (no matter it does or does not contain abstract methods) whenever u feel that this particular class does not make sens if it stands alone ... 一个简单的规则是,只要您觉得这个特定的类单独存在而没有任何意义,就使它们成为抽象类(无论它是否包含抽象方法)

Thus use abstract class when 因此,当使用抽象类

  • u do not allow the instantiation of the object of an abstract class as its too general for that purpose u不允许实例化抽象类的对象,因为它太通用了
  • but at the same time allows you to give some concrete implementation of methods that can be inherited by other classes which is fruitful for your hierarchical structure that you have designed 但同时允许您给出一些方法的具体实现,这些方法可以被其他类继承,这对于您设计的层次结构很有用

您可以将一个类定义为抽象类,并且其中没有任何方法...但是,您将无法对其进行任何处理,因为它只能由其他类继承并且不能单独实例化。

  1. You can not create an instance of abstract class. 您不能创建抽象类的实例。
  2. The abstract keyword enables you to create classes and class members that are incomplete and must be implemented in a derived class. abstract关键字使您可以创建不完整的类和类成员,并且必须在派生类中实现。
  3. The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share. 抽象类的目的是提供多个派生类可以共享的基类的通用定义。

An abstract class can contain abstract methods as well as normal (concrete methods). 抽象类可以包含抽象方法也可以包含常规方法(具体方法)。

The purpose of an abstract class is to be used as a base class that can be inherited by other classes. 抽象类的目的是用作可以被其他类继承的基类。 The advantage of doing this is that multiple child classes can take advantage of methods inside the base class(abstract class) without having to rewrite the same methods again. 这样做的好处是,多个子类可以利用基类(抽象类)内部的方法,而不必再次重写相同的方法。

You define a class abstract to preserve a possibility to define an abstract members. 您定义一个类abstract保留定义抽象成员的可能性。

It's true you can avoid do that, but in that case it just meaning less. 的确可以避免这样做,但在那种情况下,它的意义就更少了。

Other question like this can be done: 这样的其他问题也可以做到:

Why can I define some function virtual , even if noone will override it. 为什么即使没有人覆盖它,我也可以定义一些virtual函数。 ?

These are different concepts, for sure, but the idea of asking the question is the same. 当然,这些是不同的概念,但是提出问题的想法是相同的。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM