假设,我有2个配置。依赖注入:基于接口和类的松耦合机制之间的区别?
第一个:
interface I {
...
}
class A implements I {
...
}
class B implements I {
...
}
class Component {
I i;
Component (I i) {
this.i = i;
}
}
二之一:
class C {
...
}
class A extends C {
...
}
class B extends C {
...
}
class Component {
C c;
Component (C c) {
this.c = c;
}
}
在什么其中使用两个不同的松散耦合的机制(基于接口和基于类)这两个结构之间的差异?
为什么我需要使用接口而不是类?
[编程接口](http://stackoverflow.com/questions/383947/what-does-it-mean-to-program-to- an-interface)非常详细地介绍了这一点。 –