关于C#中的OOP的问题 [英] Question about OOP in C#

查看:88
本文介绍了关于C#中的OOP的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

请确保我能很好地理解这些概念,所以请告诉我我是否正确:

-我们在类中声明了一个虚拟方法,因此我们可以在其中编写代码,然后在子类中重写它,或者通过new关键字将其隐藏.

-我们在类中声明了一个抽象方法,但无法在其中编写代码,但可以在子类中重写它,也可以使用new关键字将其隐藏.

-一个问题在这里:我不能在父类中声明一个虚函数和抽象函数而不重写它吗?

谢谢.

Hi all,

Please i want to make sure that i understand those concepts well so please tell me if i am right :

-We declare a virtual method in a class so we can write a code in it and override it in a child class or hiding it by new keyword.

-We declare an abstract method in a class and we cant write a code in it but we can override it in a child class or hiding it by new keyword.

-A question goes here : cant i override a function without declaring it as virtual and abstract in the parent class ?

Thank you.

推荐答案

您编写了一个虚拟方法来提供重写它的能力,而不是隐藏它.

抽象方法-这次您必须重写它(实际上,您正在提供实现)-您不会隐藏它.

严格来说,如果该方法存在于非密封类中,则可以使用new关键字更改实现.但是,这样做的问题是,您最终可能没有设置或更改该类的其他方法所依赖的内容.这种方法将方法隐藏在派生自该类的方法中,因此您无权对其进行设置.

请记住,如果某个方法未声明为虚方法,则有其原因.
您要在这里实现什么?
You write a virtual method to provide the ability to override it, not to hide it.

Abstract method - this time you must override it (actually, you''re providing an implementation) - you don''t hide it.

Strictly speaking, if the method exists in a non sealed class, you could changed the implementation using the new keyword. The problem with this, though, is that you could end up not setting something or changing something that further methods on that class rely on. This approach hides the method in the class being derived from, so you have no access to set it.

Remember, if a method wasn''t declared as virtual, there was a reason for this.

What are you trying to achieve here?


首先,没有子类之类的东西.该关系可以是基类(祖先)-派生类").也称为一般化".

关键字"new"很重要,以避免与隐藏基类的某些成员这一事实有关的编译警告.考虑一下:

First of all, there is no such thing as child class. The relationship can be "base class (ancestor) — derived class"). It is also called "generalization".

The keyword "new" is important to avoid compilation warning related to the fact that you hide some member of the base class. Consider this:

class Base {
    internal virtual void Shaded() {}
}
class Derived : Base {
    internal new void Shaded() { }
}





如果基类中的方法为abstract:
,则完全相同,这是有效的





Exact same thing is valid if the method in a base class is abstract:

abstract class Base {
    internal abstract void Shaded();
}
abstract class Derived : Base {
    internal new void Shaded() {}
}



[END EDIT]

如果删除new,则警告为:警告2" Derived.Shaded()"隐藏继承的成员"Base.Shaded()".要使当前成员重写该实现,请添加override关键字.否则,请添加新关键字."

通过说new,您的意思是:是的,我隐藏了该成员,但并非偶然.我知道我在做什么.不用担心,同志编译器,我会很好的,而不会发出您的愚蠢警告! "

不用说,在大多数情况下(确实存在)最好避免使用.最好不要弄乱virtual(如上所示),但是原则上这是可能的.

现在,如果从Base.Shaded中删除virtual或将虚拟添加到Derived.Shaded,效果将相同.在这些情况下,不会发生面向对象的情况.当第一种方法标记为virtualabstract,第二种方法标记为override时,唯一的面向对象的效果(后期绑定)是可能的.这样,即使您的编译时间类型为Base,也会动态进行调用的调度.

这是您的最后一个问题.答案是:否.

—SA



[END EDIT]

If you remove new, the warning is: "Warning 2 ''Derived.Shaded()'' hides inherited member ''Base.Shaded()''. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword."

By saying new you say: "Yes, I hide the member, but not accidentally. I know what I''m doing. Don''t worry, comrade compiler, I''ll be just fine without your silly warning!"

Needless to say, it should be best avoided in most cases (which do exist). It''s even better not to mess up with virtual (as shown above), but it principle this is possible.

Now, if you remove virtual from Base.Shaded or add virtual to Derived.Shaded, the effect will be the same. In these cases, nothing object-oriented happens. The only object-oriented effect (late binding) is possible when first method is marked virtual or abstract, and the second one — override. Then the dynamic dispatch of the call happens even if you compile-time type is Base.

That comes to your last question. The answer is: no.

—SA


这篇关于关于C#中的OOP的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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