是否可以在 Spring 中扩展 pojo 类?

发布于 2025-01-07 17:39:48 字数 263 浏览 3 评论 0原文

我有一个 pojo,其中包含一组私有变量及其 setter 和 getter。这个 pojo 在我的应用程序中广泛使用。现在,我必须支持一个场景,其中有多个 pojo,并且每个 pojo 都是我拥有的初始 pojo 的超集。我是否可以扩展我原来的pojo,这样我就不需要改变我现有的业务逻辑。 我是春天的新手,我不知道这是否可能。 Pojo B 应该包含 pojo A 中的所有内容以及更多内容。 在代码中,我将通过 pojo A 创建 pojo B 对象。 基本上,有些类似于继承的东西,但是带有pojos。

I have a pojo which contains a set of private variables and their setters and getters. This pojo is used extensively across my application. Now, I have to support a scenario where there are multiple pojos and each pojo is a superset of the initial pojo I have. Is it possible that I can extend my original pojo so that I need not change my existing business logic.
I am new to spring and I dont know if this is possible.
Pojo B should contain everything in pojo A and few more things.
Inside the code I will create pojo B objects through pojo A.
Basically, some thing similar to inheritance, but with pojos.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

漫雪独思 2025-01-14 17:39:48

通常,您可以聚合或继承。

继承:

class A {
    private String name;
    public String getName() {
        return name;
    }
}

class B extends A {
}

聚合

public interface A {
    public String getName();
}

public class AImpl implements A {
    private String name;
    public String getName() {
        return name;
    }
}

public class BImpl implements A {
    private A wrapped;
    public BImpl(A wrapped) {
        this.wrapped = wrapped;
    }
    public String getName() {
        return wrapped.getName();
    }
}

Typically, you would either aggregate or inherit.

Inherit:

class A {
    private String name;
    public String getName() {
        return name;
    }
}

class B extends A {
}

Aggregate

public interface A {
    public String getName();
}

public class AImpl implements A {
    private String name;
    public String getName() {
        return name;
    }
}

public class BImpl implements A {
    private A wrapped;
    public BImpl(A wrapped) {
        this.wrapped = wrapped;
    }
    public String getName() {
        return wrapped.getName();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文