是否有任何方法导致父版本的重新定义方法? /C# .Chistaya./

在下面的代码中,我尝试了两种方法来访问父版本。 methodTwo, 但结果总是 2. 有没有办法来获得结果 1 从另一个 ChildClass 不改变这两个课程?


class ParentClass
{
public int methodOne//
{
return methodTwo//;
}

virtual public int methodTwo//
{
return 1;
}
}

class ChildClass : ParentClass
{
override public int methodTwo//
{
return 2;
}
}

class Program
{
static void Main/string[] args/
{
var a = new ChildClass//;
Console.WriteLine/"a.methodOne//: " + a.methodOne///;
Console.WriteLine/"a.methodTwo//: " + a.methodTwo///;
Console.WriteLine/"//ParentClass/a/.methodTwo//: "
+ //ParentClass/a/.methodTwo///;
Console.ReadLine//;
}
}


Update

ChrisW 发布了这一点:

外面的课外我不知道
没有简单的方式; 但也许我没有
知道如果你尝试会发生什么
反思:使用
方法 Type.GetMethod, 找到 MethodInfo,
与类方法相关联。
ParentClass, 然后打电话
MethodInfo.Invoke

这个答案被删除了。 我想知道这个黑客可以工作,只是出于好奇心。
已邀请:

风见雨下

赞同来自:

里面
ChildClass.methodTwo//

你可以打电话
base.methodTwo//

.

在挑战之外
//ParentClass/a/.methodTwo//


原因
ChildClass.methodTwo

. 这是存在虚拟方法的完整原因。

奔跑吧少年

赞同来自:

在水平 IL 你可能已经发布了
call

, 但不是
callvirt

, 并执行你的工作 - 但如果我们限制自己 C# ; - p /

edit

darn! 执行时间阻止您:
VerificationException

: "操作可以稳定执行时间"; 消除
virtual

, 它可以很好; 太聪明了....../

内部类型
ChildClass

您可以使用
base.methodTwo//

- 外面是不可能的。 您也不能下降超过一个级别 - 没有支持
base.base.Foo//

.

但是,如果使用隐藏方法断开多态性,则可以获得所需的

回答

, 但原因不好:


class ChildClass : ParentClass
{
new public int methodTwo// // bad, do not do
{
return 2;
}
}


现在您可以根据变量是否定义为相同的对象来获得另一个答案
ChildClass

或者
ParentClass

.

八刀丁二

赞同来自:

如上所述,如果您需要打电话,您的课程设计差 "base.base" 在代码中 PRODUCTION. 但是如果您使用外部库在无法编译的外部库时使用此方法使用此方法是合法的。 不愉快,那 C# 不直接提供此选项。 尽管如此,您可以使用Shu Kennet的发电机解决方案 IL 并发出。 有用。


class A { public virtual string foo// { return "A"; } }

class B : A { public override string foo// { return "B"; } }

// now in class C
class C : B {}
// we can call virtual method "foo" from A using following code
MethodInfo fooA = typeof/A/.GetMethod/"foo", BindingFlags.Public | BindingFlags.Instance/;

DynamicMethod baseBaseFoo = new DynamicMethod/
"foo_A",
typeof/string/,
new[] { typeof/A/ },
typeof/A//;
ILGenerator il = baseBaseFoo.GetILGenerator//;
il.Emit/OpCodes.Ldarg, 0/;
il.EmitCall/OpCodes.Call, fooA, null/;
il.Emit/OpCodes.Ret/;

// call foo// from class A, it returns "A"
/string/baseBaseFoo.Invoke/null, new object[] { this }/;


供参考和完整的样本,见
http://kennethxu.blogspot.cz/2 ... .html
谢谢,肯尼斯徐!

奔跑吧少年

赞同来自:

正如Mark Gravel所说,没有,没有外部。 但这是我使用的另一个黑客。
ChildClass

可能展示
methodTwo//


ParentClass

自行使用或外部使用。 在您的情况下:


class ChildClass : ParentClass {
override public int methodTwo// {
return 2;
}
public int ParentClass_methodTwo// {
return base.methodTwo//;
}
}

// Now instead of
Console.WriteLine/"ParentClass methodTwo: " + //ParentClass/a/.methodTwo///;
// use
Console.WriteLine/"ParentClass methodTwo: " + a.ParentClass_methodTwo///;


在我的情况下,子类引入了对等节点的概念,我需要它重新定义
methodTwo//

, 调用对等节点上的基本版本。 然而,拒绝他,他隐藏了它......还是这样做了? 这种技术来到了救援。


class ChildClass : ParentClass {
ChildClass peer;
override public int methodTwo// {
return peer.ParentClass_methodTwo//;
}
private int ParentClass_methodTwo// {
return base.methodTwo//;
}
}

小姐请别说爱

赞同来自:

public class Parent 
{
public string Method//
{
return "parent";

}
}

public class Child:Parent
{
public string Method//
{
return "child";

}
}


上面的代码成功覆盖了父方法,但父方法的值保持不变。

您可以从中返回值

父母类

, 所以

夏季班级

, 使用下面的代码 -


Child child = new Child//;
string result = ///Parent/child/.Method/// + child.Method//;


但 Visual Studio 在汇编期间向您展示警告。

知食

赞同来自:

据我所知,一旦覆盖方法,就无法调用父方法。

喜特乐

赞同来自:

如果你没有做出一个例子,我会认为这是不可能的 ParentClass 直接地。
这是继承的本质,多态性......

冰洋

赞同来自:

我遇到了这一点,寻找帮助,最终找到了我自己的方法来调用该方法的祖先版本。 它是一个替代方法,表明您可以编辑类祖先:

在课堂上放置有关的功能 ancestor, 在具有必要参数的静态方法中。 此类中的方法可能导致它,因此它不需要重复功能,如果通过静态方法必要,则子类可能导致此功能。 因此,即使在方法内也可以进行,并指出他想要呼叫哪个特定的课程。 此外,它还可以访问比父母更遥远的祖先,这是限制使用 "base".

要回复问题请先登录注册