简体   繁体   English

从抽象类继承的类是否需要具有相同签名的方法?

[英]Do Classes Inherited from Abstract Classes require methods having the same signature?

I'm a bit confused at some code I recently came across. 我对最近遇到的一些代码有些困惑。 Here is a snippet. 这是一个片段。 First of the Abstract Class Definition and then of the Class that inherits from it: 首先是抽象类定义,然后是从其继承的类:

public abstract class BaseClass
{
    protected static void MapEntityToModel(string paramOne, List<TypeDef> types)
    {
        // Some Logic Here
    }
    protected static void MapModelToEntity(ModelType model, ResultType result)
    {
        // Some Logic Here
    }
} 

public class BaseExtension : BaseClass
{
    public static ViewModel MapModelToViewModel(Model m)
    {
        var result = new ViewModel();
        // Some Logic Here
        return result;
    }
    public static List<ViewModel> MapModelsToViewModels(List<TModel> models)
    {
        return models.Select(m => MapModelToViewModel(m)).ToList();
    }

    public static Model MapViewModelToModel(ViewModel v)
    {
        var result = new Model();
        // Some Logic Here
        return result;
    }
}

So my understanding and usage of an Abstract Class has always been that any abstract methods within an Abstract Class must be overridden in the inherited Classes. 因此,我对Abstract类的理解和使用始终是,必须在继承的类中重写Abstract类中的任何抽象方法。 If a method within an Abstract Class is not declared abstract, the derived Class can create an instance of itself within a method and directly call the non-abstract method of the Abstract Class. 如果Abstract类中的方法未声明为abstract,则派生类可以在方法中创建其自身的实例,并直接调用Abstract类的非抽象方法。

But in either case the methods of the Abstract Class are used in the Derived Class. 但无论哪种情况,派生类都使用Abstract类的方法。 However, given the previous code snippets the Derived Class has no directly mapped signature or usage. 但是,鉴于先前的代码片段,派生类没有直接映射的签名或用法。

What then is the purpose of the Abstract Class in this particular scenario and why does it compile without error? 那么,在这种特定情况下,抽象类的目的是什么?为什么没有错误地进行编译? I obviously am missing some concept of the Abstract Class and its appropriate implementation. 我显然缺少一些抽象类的概念及其适当的实现。

static methods are not part of instances of the class 静态方法不是类实例的一部分

when you are saying a class is abstract you are saying that you cannot create instances of it. 当您说一个类是抽象的时,您说您不能创建它的实例。

Abstract on those classes serves no purpose apart from highlighting that creating instances of them is pointless as all methods are static anyway. 这些类的抽象没有什么用,除了强调创建它们的实例是没有意义的之外,因为所有方法都是静态的。

Also inheriting from them seems a little pointless as nothing is inherited - all the members are static. 从它们那里继承也似乎毫无意义,因为没有东西被继承-所有成员都是静态的。 You may as well have only the inherited class and make the base class an empty interface and move the static methods in it into the inherited class (an interface is in effect an abstract class with no method implementations - but you would normally have what are in effect abstract method definitions though) 您也可能只拥有继承的类,并使基类成为一个空接口,并将其中的静态方法移到继承的类中(一个接口实际上是一个没有方法实现的抽象类,但是您通常会拥有其中的内容)虽然影响抽象方法定义)

abstract class a
{
    public  abstract string look();

    public static string lookStatic()
    {
        return "look";
    }
}

class b : a
{
    public override string look()
    {
        return "look member";
    }
}

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(b.lookStatic());
        var test = new b();
        Console.WriteLine(test.look());
        var c = (a) test;
        Console.WriteLine(c.look());
        Console.ReadLine();
    }
}

So my understanding and usage of an Abstract Class has always been that any abstract methods within an Abstract Class must be overridden in the inherited Classes 因此,我对Abstract类的理解和用法一直是:Abstract类中的任何抽象方法都必须在继承的类中重写

That is true, but the methods are not abstract, they are concrete (and static). 是的,但是方法不是抽象的,而是具体的(并且是静态的)。 Only virtual or abstract instance methods can be overridden. 只能重写虚拟或抽象实例方法。

What then is the purpose of the Abstract Class in this particular scenario and why does it compile without error? 那么,在这种特定情况下,抽象类的目的是什么?为什么没有错误地进行编译?

Since the class has no abstract methods or properties I do not see why it is abstract, other than the author doesn't instances created for some reason. 由于该类没有抽象方法或属性,因此我看不到它为什么是抽象的,除了作者出于某种原因未创建实例。

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

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