How to force the use of a private method, since interface methods are only publicly available?

The interface can be used to enforce methods, but they must be publicly available.

What if I want to use private methods?

Update: this is not about preventing the call, but to ensure that the private method has been DONE.

Therefore, I do not want to use the interface as such. I want to impose some kind of coding style on the team.

+5
source share
6 answers

. ( , ) , :

public abstract class A
{
    protected abstract void Foo();
}

public class B : A
{
    protected override void Foo() { }
}

, Foo B Foo() B.

Edit:

, - , , , : , :

public interface IFooWorker
{
    void DoWork();
    int CalculateSomething();
}

public class Foo
{
    private FooWorker _worker;
    private int _currentValue;
    private string _workStatement;


    public Foo()
    {
        _worker = new FooWorker(this);
    }

    private class FooWorker : IFooWorker
    {
        private Foo _outer;
        public FooWorker(Foo foo)
        {
            _outer = foo;
        }

        public void DoWork()
        {
            _outer._currentValue = CalculateSomething();
            _outer._workStatement = "I did work";
        }

        public int CalculateSomething()
        {
            return 42;
        }
    }

}
+9

? , , , . , , , , , , .

+4

, . , ... - , API . , .

" " ? ?

, , ", " - .

, , /Func/Action . , , . , , ​​ ... .. , -.

, , . , , , , , , , . , . , .NET, , , , .

"private", , , , OO.

!

+2

, . , .

+1

This does not make sense, since the private method declared in the interface cannot be called.

0
source

All Articles