开发者

How to solve this inheritance difficulty?

开发者 https://www.devze.com 2023-03-26 05:26 出处:网络
I use class A from third-party library. Class has method M: public class A { public int M(int x) { ... } } I\'ve written my class B with method M:

I use class A from third-party library. Class has method M:

public class A
{
    public int M(int x)
    {
        ...
    }
}

I've written my class B with method M:

public class B : A
{
    开发者_如何学JAVApublic void M(params int[] xs)
    {

    }
}

And then in class C I want to call method M of parent class A

public class C : B
{
    public void M2()
    {
        int result = M(1);
    }
}

But compiler marks this line as error. It tries to use method M of class B, that returns void.

How can I solve this collision without rewriting class B?


I think that ((A)this).M(1) would help you. Note that if those methods are virtual/overridden somewhere - things could change.

Thus said, I think that hiding method in such a way is a bad idea that communicating either poor design choices or some hackery. At least I can't think out a way when I really need this.

0

精彩评论

暂无评论...
验证码 换一张
取 消