English 中文(简体)
我想凌驾于C#的方法,但我有不同的签字。
原标题:I would like to override a method in C#, but I have a different signature
  • 时间:2009-09-02 13:44:49
  •  标签:

基础班级用户应使用原始方法

class A
 public init()

衍生的班级用户应当使用衍生方法。

class B
 public init(int info)

I cannot use "override" bc there s a different signature. What options do I have so that the derived class user does not see two methods.

Notes. All in all I just need two classes that share some code. Inheritance is not a must. But simplicity for the user of B is a priority.

问题回答

这是一个大的法典轮(违反了一些基本的组织宗旨),而且根据我所知,不能用任何语言做。 In OOP, an instance of B is an instance of A; this is poly吗? 因此,如果<代码>A有一个公共方法,名称为init。 不接受任何参数,那么<代码> B。

你们试图做些什么?

Edit:现在,你又说,遗产不是必须的,只是使用。 Give B a private instance of A, for example.

由于类型系统的性质,你要求做的事情是不可能的。 任何B例都可以视为A例,因此,你可以指任何一种方法(包括<条码>Init(<><>>>>)。 您所能做的最好办法是在B中超载<条码>Init(),并在操作时间排出一个例外。

public class B
{
     void Init()
     {
         throw new NotSupportedException();
     }
}

与这里的一些答复/评论相反,你要求的是would。 如果存在:

class Derived : Base
{

可以通过考虑工作:

class Derived
{
    private Base _base = new Base();

换言之,它实际上不是基础阶层,而是执行工作的隐蔽部分。

这一工作的倒数是:基地有什么是你必须提供的抽象方法? 你必须写:

class Derived
{
    class ActualDerived : Base
    {
        // override abstract method(s)
    }

    private Base _base = new ActualDerived();

这是私人继承的全方位(见C++),是你想要继承执行,而不是“相互关系”(非正式)。

但是,在C#中,没有。

假定A和B具有共同之处。 您能否把这一因素推向不同的基类?

public class Base
{
    ... common stuff ...
}
public class A : Base
{
    public void Init()
    {
    }
}
public class B : Base
{
    public void Init(int info)
    {
    }
}

如果你需要多变,那么就是指基地,或者说,更糟的是,托马斯的接口是走的路。

不是继承,而是使用一个接口作为“中男子”:

public interface IAllThatYouNeed
{
    public void DoSomeStuff();
}

public class A : IAllThatYouNeed
{
    public void Init() {
        // do stuff
    }
}

public class B : IAllThatYouNeed
{
    public void Init(int info) {
        // do stuff
    }
}

它认为,它是不可能的。

i 试图这样做:

public class B : A
{
    private override void Init() { }

    public void Init(int x)
    { }
}

但它仍然从A类中可见一斑。

这里没有完美的解决办法。 采取一些可能的办法:

一种做法是使A.Init(A.Init)(虚拟)在B中凌驾于该词,使它成为一种不受约束的Exception/InvalidOperationException。

Init () 留有可见之处,但用户很快发现不会使用(这明确表明Init(int info)将在XML文件和例外信息中使用)。

如果你不关心继承部分,而只是想利用B类A类的功能,则没有B类从A产生,也没有B即时A并利用其功能。

Edit: You can use an interface implementing the common operations in order to retain inheritance while avoiding to implement Init() in B:

public interface IOperations
{
    void DoStuff();
    void Foo();
}

public class A : IOperations
{
    public void Init()
    {
        // Do class A init stuff
    }

    #region IOperations Members

    public void DoStuff()
    {
        // ...
    }

    public void Foo()
    {
        // ...
    }

    #endregion
}

public class B : IOperations
{
    A _operations = new A();

    public void Init(int initData)
    {
        _operations.Init();
        // Do class B init stuff
    }

    #region IOperations Members

    public void DoStuff()
    {
        _operations.DoStuff();
    }

    public void Foo()
    {
        _operations.Foo();
    }

    #endregion
}

可以通过使用工厂来做到这一点:

public static class OperationsFactory
{
    public static IOperations CreateOperations()
    {
        A result = new A();
        result.Init();

        return result;
    }

    public static IOperations CreateOperations(int initData)
    {
        B result = new B();
        result.Init(initData);

        return result;
    }
}

这样,即时代码就被很好地概括了起来,两种因诺特(Init)方法之间的区别从用户代码中隐蔽。





相关问题
热门标签