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
本文详细探讨了在面向对象编程中,如何通过实现接口来扩展类的功能,包括接口的隐式实现、显式实现以及一个类同时实现多个接口的情况。通过实例展示了不同情况下的接口使用与类的行为差异。
5372

被折叠的 条评论
为什么被折叠?



