繁体   English   中英

C#多态和方法继承

[英]C# Polymorphism and Method Inheritance

考虑以下类:

public class X {};
public class Y : X {};
public class Z : X {};

public class A {
    public bool foo (X bar) { 
        return false;
    }
};

public class B : A { 
    public bool foo (Y bar) { 
       return true;
    }
};    

public class C : A { 
    public bool foo (Z bar) { 
       return true;
    }
};

有没有办法实现以下所需的输出?

A obj1 = new B();
A obj2 = new C();

obj1.foo(new Y()); // This should run class B's implementation and return true
obj1.foo(new Z()); // This should default to class A's implementation and return false

obj2.foo(new Y()); // This should default to class A's implementation and return false
obj2.foo(new Z()); // This should run class C's implementation and return true

我遇到的问题是,无论传递的参数如何,都始终会调用A类的实现。

你需要在Avirtual使用foo方法,这样它才能被正确覆盖并多态调用。

public class A { public virtual bool Foo (X bar) { ... } }
public class B { public override bool Foo (X bar) { ... } }

你现在这样做的方式是在BC有效地定义一个新的方法实现,只有当实例属于那种类型时才会调用它。 由于您将它们声明为类型AA obj1 = new B(); )并且该方法不是虚拟的,因此将始终调用A实现。

我相信您希望定义虚方法并使用包含派生类型的签名覆盖它。 这是不可能的,因为覆盖基础的方法应该具有相同的签名。 可能这是因为这需要编译器应用复杂的遍历规则。 因此,如前面的答案所示,以获得所需的行为:

public class A { public virtual bool Foo (X bar) { ... } }
public class B { 
    public override bool Foo (X bar) {
         if(bar is Y) { ... } 
         else base.Foo(bar);
    }
}

暂无
暂无

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

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