虚拟和覆盖方法 [英] virtual and override methods

查看:67
本文介绍了虚拟和覆盖方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我有一个关于虚拟和覆盖方法的问题。请

原谅基本性质!


首先,让我引用C#键中的编程:任何虚拟

用''覆盖'覆盖的方法仍然是进一步

后代类的虚拟方法。


现在这里是我的问题:让'假设您有基类A,并且

子类B和C.类A包含虚方法,B包含

重写方法。如果C没有覆盖,这是否意味着

从A继承虚拟方法?这是上面的引用是什么?
说什么?


如果是这样,这里有另一个引用:如有必要,方法或属性可以

通过在

方法或属性名称前面添加关键字''base'来调用基类中被覆盖的成员。'"


换句话说,如果基类A包含public virtual int Method(),

和B包含公共重写int Method(),那么对于C,如果我想要

使用A类中的方法,我必须把它写成:

base.Method()?如果是这样,如果C已经从B继承了所有公共方法,为什么我需要这样做呢?难道我不能只使用方法()

没有基本前缀吗?


书中的例子包含一个基类和几个子类。 />
除了一个子类之外的所有子类都会覆盖基类中的虚方法,并且

另一个子类(没有覆盖的子类)使用这个

' 'base.Method()''代码,用于引用基类中的方法。如果这个子类已经继承了该方法,那么
似乎是不必要的。


谢谢!

解决方案
>首先,让我引用C#键中的编程:使用''覆盖'覆盖的任何虚拟

方法仍然是进一步后续类的虚拟方法。

现在这里是我的问题:假设你有基类A,而B / C是子类.A类包含一个虚方法,B包含
覆盖方法。如果C没有覆盖,这是否意味着它从A继承虚拟方法?那是上面的引用是什么意思?


由于C和B都来自A,它们都将继承

A,是(其中一个B覆盖)的方法。然而,B超越A'的
方法之一将与C无关。


引用的内容是如果你有一个一组类,其中C

派生自B和B派生自A,当你在B中编写覆盖方法时,它仍然是C中的虚拟方法(从而可以被

覆盖)。


如果是这样,这里有另一个引用:如果有必要,方法或属性可以制作通过使用关键字base来表示
方法或属性名称来调用基类中的重写成员。

换句话说,如果基类A包含公共虚拟int方法(),
和B包含公共覆盖int Method(),然后对于C,如果我想要使用A类中的方法,我是否必须将其写为:
base.Method()?如果是这样,如果C已经从A继承了所有公共方法,为什么我需要这样做呢?难道我不能只使用方法()
没有基本前缀吗?


覆盖该方法。调用base.Method()基本上是说嘿,我是

正好调用A :: Method()而只是调用Method()说我是

调用方法的最新覆盖方法()"

本书中的示例包含一个基类和几个子类。
除了一个子类之外的所有子类都覆盖了基类中的虚方法,而另一个子类(没有重写的子类)使用了这个基础.Method ()''代码用于引用基类中的方法。它似乎是不必要的,如果这个子类已经继承了该方法。




我必须看到代码才能真正起作用。我很抱歉,但是我不太喜欢你欠你的意思。


Daniel O''Connell [C#MVP]写道:

如果是这样,这里是另一个引用:如果有必要,方法或属性可以调用被覆盖的成员在基类中,通过使用关键字''base。'来表示
方法或属性名称。''

换句话说,如果基类A包含public virtual int Method(),
和B包含公共覆盖int Method(),然后对于C,如果我想在A类中使用该方法,我是否必须将其写为:
base.Method ()?如果是这样,如果C已经从A继承了所有公共方法,为什么我需要这样做呢?难道我不能只使用没有基本前缀的方法()
吗?



是的,你可以在C中,但只有你不介意的话后代
覆盖了方法。调用base.Method()基本上是说嘿,我是
正好调用A :: Method()而只是调用Method()说我是
调用方法的最新覆盖方法()




所以,如果我是使用方法()在C类中,它将从B类调用

覆盖方法而不是A中的原始方法?


>> < blockquote class =post_quotes>


是的,您可以在C中,但前提是您不介意进一步的后代是否覆盖该方法。调用base.Method()基本上是说嘿,我是
正好调用A :: Method()而只是调用Method()就是说
我正在调用方法的最新覆盖方法()"



所以,如果我要使用 ;方法()"在C类中,它会从B类调用
覆盖方法而不是A中的原始方法吗?




不,如果我理解你的话,至少没有无论如何,它都会调用

A中的方法。如果你从一个类中派生出两个类,那么这两个类中的每一个都完全独立于另外,其中一个是重载,其中一个是完全无关的。


我的意思是说,鉴于这些课程,


A级

虚拟方法()

B类来自A


类C来源于B

覆盖方法


如果B中定义的方法调用base.Method()A :: Method *将被调用

总是。但是如果B中的方法只调用Method(),则该方法将被虚拟调用,并且将选择派生最多的方法。调用哪种方法

取决于它所调用的实例。


如果实例属于B类,则调用A :: Method()。

如果实例是C类,那么将调用C :: Method(),因为在

类C方法被覆盖。


另一个我觉得我应该给的点是

A级

虚拟方法

B级来自A

覆盖方法

C类派生自A


类C将使用A :: Method(),因为B类与C外部没有任何关系

普通血统。 C是独立的,并且在B和任何类中都是覆盖从B派生的
不以任何方式影响C.用家族的话来想想这个问题

你可以说它和你妹妹买船一样:只是

因为你有同样的父母不会这意味着船也是你的。


Hi all. I have a question about virtual and override methods. Please
forgive the elementary nature!

First off, let me quote Programming in the Key of C#: "Any virtual
method overridden with ''override'' remains a virtual method for further
descendent classes."

Now here''s my question: Let''s say you have base class A, and
subclasses B and C. Class A contains a virtual method, and B contains
an override method. If C didn''t have an override, does that mean it
inherits the virtual method from A? Is that what the above quote is
saying?

If so, here''s another quote: "If necessary, methods or properties can
make calls to overridden members in the base class by prefacing the
method or property name with the keyword ''base.''"

In other words, if base class A contained public virtual int Method(),
and B contained public override int Method(), then for C, if I want to
make use of the method in class A, do I have to write it as:
base.Method()? If so, why would I need to do that if C already
inherits all public methods from A? Couldn''t I just use Method()
without the base prefix?

The example in the book contains a base class and several subclasses.
All but one subclass overrides a virtual method in the base class, and
the other subclass (the one that has no override) uses this
''base.Method()'' code to refer to the method in the base class. It
seems unnecessary, if this subclass has inherited the method already.

Thanks!

解决方案

> First off, let me quote Programming in the Key of C#: "Any virtual

method overridden with ''override'' remains a virtual method for further
descendent classes."

Now here''s my question: Let''s say you have base class A, and
subclasses B and C. Class A contains a virtual method, and B contains
an override method. If C didn''t have an override, does that mean it
inherits the virtual method from A? Is that what the above quote is
saying?
Since C and B are both derived from A, they will both inherit the methods of
A, yes(one of which B overrides). However, that B overrode one of A''s
methods will have no bearing on C.

What that quote is saying is that if you had a set of classes where C
derived from B and B derived from A, when you wrote the override method in B
it would still be a virtual method in C(and thereby available to be
overridden).

If so, here''s another quote: "If necessary, methods or properties can
make calls to overridden members in the base class by prefacing the
method or property name with the keyword ''base.''"

In other words, if base class A contained public virtual int Method(),
and B contained public override int Method(), then for C, if I want to
make use of the method in class A, do I have to write it as:
base.Method()? If so, why would I need to do that if C already
inherits all public methods from A? Couldn''t I just use Method()
without the base prefix?

Yes, you could, in C, but only if you don''t mind if further descendents
override the method. Calling base.Method() is basically saying "Hey, I''m
calling A::Method() precisely" whereas just calling Method() is saying "I''m
calling the most recent override of the method Method()"
The example in the book contains a base class and several subclasses.
All but one subclass overrides a virtual method in the base class, and
the other subclass (the one that has no override) uses this
''base.Method()'' code to refer to the method in the base class. It
seems unnecessary, if this subclass has inherited the method already.



I would have to see the code to really help. I''m sorry but I don''t quite
undersatnd waht you mean here.


Daniel O''Connell [C# MVP] wrote:

If so, here''s another quote: "If necessary, methods or properties can
make calls to overridden members in the base class by prefacing the
method or property name with the keyword ''base.''"

In other words, if base class A contained public virtual int Method(),
and B contained public override int Method(), then for C, if I want to
make use of the method in class A, do I have to write it as:
base.Method()? If so, why would I need to do that if C already
inherits all public methods from A? Couldn''t I just use Method()
without the base prefix?


Yes, you could, in C, but only if you don''t mind if further descendents
override the method. Calling base.Method() is basically saying "Hey, I''m
calling A::Method() precisely" whereas just calling Method() is saying "I''m
calling the most recent override of the method Method()"



So if I were to use "Method()" in class C, it would be calling the
override method from class B instead of the original method from A?


>>


Yes, you could, in C, but only if you don''t mind if further descendents
override the method. Calling base.Method() is basically saying "Hey, I''m
calling A::Method() precisely" whereas just calling Method() is saying
"I''m calling the most recent override of the method Method()"



So if I were to use "Method()" in class C, it would be calling the
override method from class B instead of the original method from A?



No, atleast not if I understand your set of classes anyway, it''d be calling
the method in A. If you derive two classes from a class, each of those two
classes are entirely independent of eachother, overloads in one are
unrelated to the other in their entirety.

What I mean to say is that, given these classes

class A
virtual Method()
class B derives from A

class C derives from B
overrides Method

if a method defined in B calls base.Method() A::Method *will* be called
always. But if the method in B just calls Method(), the method will be
called virtually and the most derived method will be chosen. Which method is
called depends on the instance its called on.

If the instance is of class B then A::Method() will be called.
If the instance is of class C, then C::Method() will be called since in
class C Method is overridden.

ANother point I feel I should make is given
class A
virtual Method
class B derives from A
overrides Method
class C derives from A

class C will use A::Method() as class B has no relationship to C outside of
common ancestry. C is independent and overrides in B and in any classes
derived from B do no effect C in any way. To think of this in familial terms
you could say that it would be the same as your sister buying a boat: just
because you have the same parents doesn''t mean the boat is yours as well.


这篇关于虚拟和覆盖方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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