A class that implements two interfaces that define the same method

//inteface i11 
 public interface i11
    {
        void m11();
    }
//interface i22
    public interface i22
    {
         void m11();
    }

//class ab implements interfaces i11 and i22
    public class ab : i11, i22
    {
        public void m11()
        {
            Console.WriteLine("class");
        }
    }

Now, when in the Main () method we create an object of class ab which interface will be called, please explain everything.

+3
source share
4 answers

Interfaces simply say: "Everything that implements this interface may be required to have these methods."
If you think about it in this context, you will see that it does not invoke an interface method; rather, it just implements the required method calls.

+4
source

There is only one implementation of the method, so it is obvious that the call is called regardless of the type of link. I.e:.

i11 o = new ab();
o.m11();

and

i22 o = new ab();
o.m11();

virtually the same.

ab.m11 . , , .

+9

. , . , , . , . , .

, IBank. :

IBank
{
    void AddMoney(int amount);
    void RemoveMoney(int amount);
    int GetBalance();
}

:

EuroBank : IBank
{
    void AddMoney(int amount){ balance+= amount; }
    void RemoveMoney(int amount){ balance-= amount; }
    int GetBalance(){ return balance; }
    private int balance;
}

IBank. .

IBank bank = new IBank;

, IBank. .

IBank bank = new EuroBank;
bank.AddMoney(7);

, AddMoney, , , AddMoney EuroBank.

, m11 ab. ab i11, i22. i11 i22. .

i11 first = new ab();
i22 second = new ab();

m11 , , ab.

+4

.
, , - m11 ab.

0

Source: https://habr.com/ru/post/1784348/


All Articles