简体   繁体   English

抽象类没有任何抽象方法

[英]Abstract class without any abstract method

I am surprised to know that an abstract class in C# is possible with no abstract methods also. 我很惊讶地知道C#中的抽象类是可能的,也没有抽象方法。

abstract class AbstractDemo
{
 public void show()
  {
    Console.WriteLine("In Show Method"); 
   }
}
class MainDemo:AbstractDemo
{
 public static void Main()
 {
    Console.WriteLine("In Main Method");
 }
}

Any explaination ? 任何解释?

Sometimes you don't want to give the possibility to instantiate a class but you need this class as a base class for other classes. 有时您不希望提供实例化类的可能性,但您需要将此类作为其他类的基类。

The reason for choosing abstract classes over interfaces is that you can provide some basic implementation. 在接口上选择抽象类的原因是您可以提供一些基本实现。

This is entirely valid, and occasionally useful if you want to provide event-like behaviour: provide an abstract class with all the "event handlers" implemented as virtual methods with a default behaviour of doing nothing. 这是完全有效的,如果你想提供类似事件的行为,偶尔会有用:提供一个抽象类,其中所有“事件处理程序”都是作为虚方法实现的,默认行为是什么都不做。

A derived class can then override some of the methods, but doesn't have to override any specific one, because nothing's abstract. 然后派生类可以覆盖某些方法,但不必覆盖任何特定方法,因为没有什么是抽象的。 It still makes sense for the class to be abstract because an instance of the base class would be pointless (as everything would be a no-op). 这个类仍然是抽象的,因为基类的实例是没有意义的(因为一切都是无操作的)。

This pattern is much more common in Java than C#, however - as in C# you'd normally just use "proper" events. 这种模式在Java中比C#更常见 - 但是在C#中你通常只使用“正确的”事件。

I think you're confusing abstract classes with interfaces. 我认为你把抽象类与接口混淆了。 Interfaces can't have methods with body, abstract classes can. 接口不能有body的方法,抽象类也可以。 There are times when you want to prevent user from instantiating an object of a specific class; 有时您希望阻止用户实例化特定类的对象; but still provide some base functionality for the classes that derive from it; 但仍然为从它派生的类提供一些基本功能; this is what an abstract class is useful for. 这是抽象类有用的东西。

An abstract class is a class that must be extended before it can be used. 抽象类是一个必须在可以使用之前进行扩展的类。 This does not it any way mean that the function themselves must be abstract. 这并不意味着函数本身必须是抽象的。

Take for example an Animal class 以动物类为例

public abstract class Animal
{
    void Move()
    {
        //whatever
    }
}

public class Fish : Animal
{
    void Swim()
    {

    }
}

public class Dog : Animal
{
    void Bark()
    {

    }
}

All animals can move but only the fish can swim and the dog can bark. 所有动物都可以移动,但只有鱼可以游泳,狗可以吠叫。

Or for a real life example. 或者为现实生活中的例子。 I have an Asp.net MVC base controller I use in my application. 我有一个我在我的应用程序中使用的Asp.net MVC基本控制器。 It has some basic methods I need very often like GetCurrentUser() and a function I wrote to help with localization. 它有一些我经常需要的基本方法,比如GetCurrentUser()和我写的一个帮助本地化的函数。 It also takes care of tracking so I don't have to rewrite that code in all of my controllers. 它还负责跟踪,因此我不必在所有控制器中重写该代码。 The class has about 200 lines of code but not a single abstract method. 该类有大约200行代码,但没有一个抽象方法。

If your class is just a base for other classes and it does not have an full usablility - in other words as a base itselfe is not usable at all then you want to prevent from creating instances of it. 如果你的类只是其他类的基础并且它没有完整的可用性 - 换句话说作为基础itselfe根本不可用,那么你想要阻止创建它的实例。 In this case you can make abstract class without abstract members. 在这种情况下,您可以创建没有抽象成员的抽象类。

You could use abstract keyword on a class just to signal the compiler that it can only used inheriting from it, and not directly; 你可以在一个类上使用abstract关键字来告诉编译器它只能继承它,而不是直接; In this case you are not oblied to put abstract member on the class. 在这种情况下,您不会将抽象成员放在课堂上。

This is equivalent to put in the class only one protected constructor, but using abstract is more clear and understandable. 这相当于只在类中放入一个受保护的构造函数,但使用抽象更清晰易懂。

No better explanation than MSDN it self http://msdn.microsoft.com/en-us/library/aa645615(v=VS.71).aspx 没有比自己的MSDN更好的解释http://msdn.microsoft.com/en-us/library/aa645615(v=VS.71).aspx

  • An abstract class cannot be instantiated directly, and it is a compile-time error to use the new operator on an abstract class. 抽象类不能直接实例化,在抽象类上使用new运算符是编译时错误。 While it is possible to have variables and values whose compile-time types are abstract, such variables and values will necessarily either be null or contain references to instances of non-abstract classes derived from the abstract types. 虽然可以使编译时类型为抽象的变量和值,但这些变量和值必须为null或包含对从抽象类型派生的非抽象类的实例的引用。
  • An abstract class is permitted (but not required) to contain abstract members. 允许(但不是必需)抽象类来包含抽象成员。
  • An abstract class cannot be sealed. 抽象类不能被密封。

We have heard that in abstract class, there must be an abstarct member. 我们听说在抽象类中,必须有一个abstarct成员。 But when I compile the abstarct class without an abstract method, it compiles. 但是当我在没有抽象方法的情况下编译abstarct类时,它会编译。 It gives me surprise. 这给了我惊喜。 Now I am unable to find the article which explain exact behavior of an abstarct class. 现在我无法找到解释abstarct类的确切行为的文章。

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

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