减少抽象和混凝土类之间的耦合

发布于 2025-02-08 09:14:05 字数 560 浏览 1 评论 0原文

假设我有一个简单的代码结构,我需要在其中创建一个抽象类,然后定义该类型的某些具体对象

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文