减少抽象和混凝土类之间的耦合
假设我有一个简单的代码结构,我需要在其中创建一个抽象类,然后定义该类型的某些具体对象
public abstract class Foo {
protected int var1;
protected String var2;
protected Foo(int var1, String var2) {
this.var1 = var1;
this.var2 = var2;
}
//abstract methods here
}
,并且
public class ConcreteFoo1 extends Foo{
public ConcreteFoo(int var1, String var2) {
super(var1, var2);
}
//methods override
}
每次添加新实例变量,或者简单地更改摘要类中的那些,我需要更改还有混凝土类。有没有办法避免通过super()
依赖项注入我们做的耦合?
Let's say I have this simple code structure in which I need to create an abstract class and then to define some concrete objects of that type
public abstract class Foo {
protected int var1;
protected String var2;
protected Foo(int var1, String var2) {
this.var1 = var1;
this.var2 = var2;
}
//abstract methods here
}
and
public class ConcreteFoo1 extends Foo{
public ConcreteFoo(int var1, String var2) {
super(var1, var2);
}
//methods override
}
Everytime I add new instance variables, or simply change those already present in the abstract class, I need to change also the concrete classes. Is there a way to avoid this kind of coupling the we do via super()
dependency injection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论