I would like to override the method in C #, but I have a different signature

Base class user must access the source method

class A
 public init()

The user of the derived class must use the derived method ONLY.

class B
 public init(int info)

I can not use "override" bc there is another signature. What parameters do I have so that the user of the derived class does not see two methods.

Notes. In general, I just need two classes that share some code. Inheritance is optional. But simplicity for user B is a priority.

+1
source share
8 answers

( ), , , . B A; polymorphism. , A init, , B.

?

: , , , , composition , B A, .

+9

, . , , - init() , , init(int info) , .

init() ?

(), , class A, isntance of class B extends A. A init() , B ( extends A) protected init(), . .

+4

, , - . B A, A ( Init()). , , Init() B , .

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

A B - . ?

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

, Base , , - .

+1

" ":

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
    }
}
0

. :

A.Init() , B NotImplementedException/InvalidOperationException.

Init() , , ( , Init (int info) XML ).

A B, B, A B A .

Edit: , , , Init() 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
}

, factory:

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() .

0

,

- :

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

    public void Init(int x)
    { }
}

Init() A

0

/ , , , , :

class Derived : Base
{

:

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

, , .

: , ? :

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

    private Base _base = new ActualDerived();

This is a whole point of private inheritance (as shown in C ++) - this is for situations where you want to inherit an implementation, but not an โ€œinterfaceโ€ (in an informal sense).

But in C # it is not available.

0
source

All Articles