20120709-the key factor affecting explicit or implicit interface implementation

来源:互联网 发布:java action models 编辑:程序博客网 时间:2024/06/09 18:52



1. first situation:

    one class implement one interface

   and all the interface implementation is implicit

   

 public interface IBankAccount  {    void PayIn(decimal amount);    bool Withdraw(decimal amount);    decimal Balance{get;}  }


 

public class SaverAccount : IBankAccount  {    //类SaverAccount独有的    public void PayIn2()    {    }    //接口IBankAccount可以访问的    public void PayIn(decimal amount)    {    }
    //接口IBankAccount可以访问的    public bool Withdraw(decimal amount)    {      return false;    }    //接口IBankAccount可以访问的    public decimal Balance    {      get      {        return 1;      }    }  }


when use IBankAccount

  IBankAccount ba = new SaverAccount();      ba.PayIn(1);      SaverAccount sa = new SaverAccount();      sa.PayIn(2);      sa.PayIn2();//接口不能调用


 

2. the second situation

 

 

//the tiny difference is PayIn is explicit implementation   //接口IBankAccount可以访问的    public void IBankAccount.PayIn(decimal amount)    {    }

 

 //此时PayIn不可调用      sa.PayIn(2);

3. one class implement multiple interface

each interface can only invoke the member and function that is assigned by itself.

if the class explicit one interface's function, the instance of this class couldn't invoke the explicit interface fuction

 

 

 

 

 

 

 

    
原创粉丝点击