Java模型类中的封装

发布于 2025-01-17 13:51:38 字数 840 浏览 3 评论 0原文

假设我们开发了一个简单的博客网站后端,并且该应用具有三个或更多 pojo 类,例如post, user ,类别 /代码>。

所有类都具有相同的字段,例如idcreateDate更新

作为Java-Programmer,我们使用private访问修饰符将封装应用于类中的所有字段。我的问题非常简单:我们可以使用 notault访问修饰符使用 sharstonance 执行封装

代码:

public abstract class BaseModel {
    String id;
    LocalDateTime createdDate;
    LocalDateTime updatedDate;
    // getters and setters
}

public class Post extends BaseModel{
    private String slug;
    private String name;
    private String title;
    // other fields, getters and setters
}

public class Category extends BaseModel{
    private String name;
    private String slug;
    // other fields, getters and setters
}

Let assume that we develop a simple blog website backend and the app have three or more POJO classes like Post, User, Category.

All class have the same fields such as id, createdDate, updateDate.

As java-programmers, we apply Encapsulation to all the fields in a class using private access modifier. My question is very simple: Can we perform encapsulation using default access modifier with Inheritance?

The code:

public abstract class BaseModel {
    String id;
    LocalDateTime createdDate;
    LocalDateTime updatedDate;
    // getters and setters
}

public class Post extends BaseModel{
    private String slug;
    private String name;
    private String title;
    // other fields, getters and setters
}

public class Category extends BaseModel{
    private String name;
    private String slug;
    // other fields, getters and setters
}

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

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

发布评论

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

评论(2

白芷 2025-01-24 13:51:39

受保护的修饰符

,使用受保护访问修饰符 封装 类成员 parent class class

package private 字段和方法在 sublasses parent parent 类外部都不会看到。相反,受保护的变量和行为都可以访问任何子类无论其位置如何。

组成与继承的

继承并不总是有益的,事实上有很多情况下。在做出 class-design 的决策之前,您需要考虑所有利弊,例如特定类是否从另一类派生出来。

益处是什么,扩展 baseModel

觉得您无法从这里利用多态性。因为您可以与 parent类型 basemodel一起使用的唯一行为是用于创建和更新日期的Getters and Setters。同时,您将无法访问子类的特定行为。

它看起来像是一个缺点,因为basemodel 不是为扩展而设计的。即,它都不包含任何有用的实现(我没有考虑到getters/setter ),也不要通过其子类实现抽象方法(这将是有利的多态性的情况)。

实际上,您正在扩展basemodel只是为了重复使用几个变量。这不是使用继承的令人信服的理由。

您的示例是替代的完美案例,是关系(postbasemodel具有关系(post包括basemodel)。

当一个类包含其实例字段的设计技术是另一个类的实例时,而不是扩展此类,称为构图

通常,组成比继承更可取。除了继承外,它还允许重复使用行为,同时又密切相结合。

如果将BaseModel进行具体类,并将 coptosing 应用于其他类,则您的代码将看起来像这样。

public class BaseModel {
    private String id;
    private LocalDateTime createdDate;
    private LocalDateTime updatedDate;
    // getters and setters
}

public class Post {
    private BaseModel base;
    private String slug;
    private String name;
    private String title;
    // other fields, getters and setters
}

public class Category {
    private BaseModel base;
    private String name;
    private String slug;
    // other fields, getters and setters
}

Protected modifier

The common practice it to use protected access modifier to encapsulate class members within the Parent class.

Package private fields and methods will not be visible to subclasses located outside the package of the Parent class. Conversely, protected variables and behavior will be accessible to any subclass regardless of its location.

Composition vs Inheritance

Inheritance is not always beneficial, in-fact there are many cases where it isn't. You need to consider all pros and cons before making class-design decisions like whether a particular class will derive from another class.

What are the benefits of extending the BaseModel?

It doesn't feel like you can take advantage from the polymorphism here. Because the only behavior you can use with the parent type BaseModel are getters and setters for dates of creation and update. And at the same time you'll not be able to access the specific behavior of subclasses.

It looks rather as a drawback because BaseModel isn't designed for extension. I.e. it neither contains any useful implementations (I'm not taking getters/setters into account), no abstract methods are meant to be implemented by its subclasses (that would be a scenario of advantageous polymorphism).

In fact, you are extending BaseModel just in order to reuse a couple of variables. That not a compelling reason to utilize inheritance.

Your example is a perfect case to substitute an IS A relationship (Post is a BaseModel) with HAS A relationship (Post includes BaseModel).

The design technic, when a class contains its instance field an instance of another class instead of extending this class, is called Composition.

As a general rule, composition is a more preferable approach than inheritance. As well as inheritance, it allows to reuse the behavior and at the same time it classes closely coupled.

If you make the BaseModel a concrete class and apply composition to other classes, your code will look like that.

public class BaseModel {
    private String id;
    private LocalDateTime createdDate;
    private LocalDateTime updatedDate;
    // getters and setters
}

public class Post {
    private BaseModel base;
    private String slug;
    private String name;
    private String title;
    // other fields, getters and setters
}

public class Category {
    private BaseModel base;
    private String name;
    private String slug;
    // other fields, getters and setters
}
记忆之渊 2025-01-24 13:51:39

默认修饰符表示它只能在包中访问。因此,如果您在不同的包中使用子类,则这些字段将无法访问。

如果这就是您想要的,那就太好了!它是封装的。

如果这不是您想要的,那么糟糕...但是您仍然拥有 protected 修饰符。

这让子类继承这些字段,即使它位于不同的包中。

也请参阅这个答案:什么是Java 中 public、protected、package-private 和 private 之间的区别?

the default modifier signifies it's only accessible in the package. So if you are using a child class in a different package, the fields will not be accessible.

If that's want you want, great! It's encapsulated.

If that's not what you want, bummer... BUT you still have the protected modifier.

This let's a child class inherit the fields even if it's in a different package.

See this answer too: What is the difference between public, protected, package-private and private in Java?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文