抽象类的继承 [英] Inheritance of abstract class

查看:111
本文介绍了抽象类的继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我对c#并不是全新的,但我仍然有一些覆盖方法和属性的问题。好吧,我想知道你是如何正确地覆盖它们的。让我们看一下这个例子:



Hello I am not completely new to c#, but I still have some problems with overriding methods and properties. Well I would like to know how you override them correctly. Let's look at this example:

public abstract class BaseClass {
   private string description;
   public virtual string Description { get.... }
   .......
   .......
}

public class SubClass : BaseClass {
   .......
   .......
   public override string Description {....}
   .......
}

public class SecondSubClass : SubClass {
   .......
   .......
   ???????
}





我现在的问题是:我知道你需要覆盖抽象类的所有成员,如果你错过了那么你需要将该类声明为抽象,但是SecondCubClass呢?规则是否包含抽象类的所有成员仅对该抽象类的直接子类有效?我想也许我不需要在SecondSubClass中包含该属性,因为我已经在SubClass中实现了它,因此可以在该类中使用它,因为它是子类的子类。但我会感谢一个明确的答案,因为我没有在网上找到任何明确的声明。



谢谢!



My question is now: I know that you need to override all members of an abstract class and if you miss one then you need to declare that class as abstract as well, but what about the SecondCubClass? Is the rule to include all members of the abstract class only valid for the direct subclass of that abstract class? I thought maybe I don't need to include the property in the SecondSubClass because I already implemented it in the SubClass and therefore can use it in that class because it is a subclass of the subclass. But I would be thankful for a clear answer because I did not find any clear statement on this online.

Thank you!

推荐答案

首先,让所有抽象成员覆盖任何非抽象类的规则不仅仅是关于直接派生类,这是关于所有派生类的。但是你是对的:一旦一个成员被实现并变成非抽象的,你可以在下一层次的层次结构中继承它而不是覆盖它。这取决于你的目的。



如果你了解它的目的,你将很容易理解这条规则。想象一下,您设法创建了一个非抽象类,其中至少有一个虚拟成员未被覆盖(在您所讨论的类的层次结构中未被覆盖),您将能够实例化该类。你最终会得到一个可以访问虚拟抽象成员的类的实例,你可以调用这个成员......带来致命的结果。



但是您的问题不同:在您的代码示例中,您没有任何抽象成员。只有当您将 Desription 标记为 abstract 时,所有约会才有效。没有它,您可以使用 BaseClass.Description 而不会覆盖它。您的代码示例仅适用于您的问题。此外,您的代码示例未显示 description 如何初始化和可能修改;这不是一个错误,因为它可能在......之下,但它是一个重要的细节。



-SA
First of all, the rule to have all abstract members overridden any any non-abstract class is not just about direct derived class, this is about all the derived classes. But you are right: once a member is implemented and made non-abstract, you can inherit it in next level of hierarchy instead of overriding. It depends on your purpose.

You will easily understand this rule if you understand its purpose. Imagine you managed to create a non-abstract class with at least one of the virtual members not overridden (not overridden anywhere in the part of hierarchy up from your class in question), you will be able to instantiate the class. And you would end up with the instance of the class having access to a virtual abstract member, and you would be able to call this member… with fatal results.

But your problem is different: in your code sample, you don't have any abstract members. All of the about would be valid only if you marked Desription as abstract. Without it, you are allowed to use BaseClass.Description without overriding it. Your code sample is just inadequate to your question. Also, your code sample does not show how description is initialized and possibly modified; this is not a mistake, because it could be under "…", but it's an important detail.

—SA


这篇关于抽象类的继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆