接口的引用实例问题
定义一个借口IBankAccount ,分别用两个类:savercount,goldencount来实现该类,
到main方法的时候,出现了下面两句:
IBankAccount a = new SaverAccount();
IBankAccount b = new goldencount();
为什么不用下面这两句呢?
SaverAccount a = new SaverAccount();
goldencount b = new goldencount();
同样也可以调用接口中的方法,为何要用接口引用类的实例呢?为何?谢谢
[解决办法]
你应该看看IOP(interface orientation programming)面向接口编程,
用接口可以实现很方便的功能,比如单元测试,各种设计模式。
举个例子,工厂方法,
IBankAccount account = Factory.CreateAccount(...);
[解决办法]