为什么要私下上课或属性?

发布于 2025-01-21 12:39:59 字数 274 浏览 2 评论 0 原文

我想问一下为什么我应该私下班级或私有属性,我可以保护它免受什么保护?我认为,只有应用程序中的用户才能提供给它们或更新它们,例如(邮件,密码或年龄)只能是私有的,但他不要求填写的任何其他属性可能是公共的?是真的吗?

public class student{
    private String mail;
    public String getMail() {return mail;}
    public mail(String email) {mail=email;}
}

I would like to ask why I should make a class private or make an attribute private I protect it from what? I think that only attributes that will be available to users in the application to supply them or update them like (mail, password, or age) only should be private but any other attribute that he wouldn't asked to fill out could be public? Is it true?

public class student{
    private String mail;
    public String getMail() {return mail;}
    public mail(String email) {mail=email;}
}

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

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

发布评论

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

评论(1

浴红衣 2025-01-28 12:39:59

通常,在任何OOP支持的语言中,封装用于防止从外部功能中意外更改代码(数据)。通过定义班级成员的私有,我们可以保护数据免受意外损坏。

以下是封装的好处:

  • 保护数据免受
  • 班级每个成员的可访问性的意外腐败规范
    到类外部的代码
  • 灵活性和代码的可扩展性和降低复杂性的
  • 降低对象之间的耦合,从而改善了代码
    可维护性

尽管我们可以并且应该在我们正在编写的所有应用程序中使用它,但这与小型应用程序无关。这是关于大型框架的。

我个人相信,通过深入研究OOP概念,您可以从中获得很多好处。我可以指定一些参考文献,但是谷歌搜索OOP有很多令人兴奋的话题。

stackaverflow:封装概念

面向对象的编程(c#)

您还可以搜索YouTube,有很多资源可以开始使用OOP概念。

Generally, in any OOP-Supported language, the encapsulation is used to prevent the alteration of code (data) accidentally from the outside functions. By defining the class members' private, we can protect the data from accidental corruption.

The following are the benefits of encapsulation:

  • Protection of data from accidental corruption
  • Specification of the accessibility of each of the members of a class
    to the code outside the class
  • Flexibility and extensibility of the code and reduction in complexity
  • Lower coupling between objects and hence improvement in code
    maintainability

Although we can and should use it in all scales of applications that we are writing, it's not about small applications. It's about large-scale frameworks.

I personally believe you can get so much benefit by deep diving into the OOP concepts. I can specify some references, yet there are plenty of exciting topics to follow by googling OOP.

Stackoverflow: Encapsulation concept

Object-Oriented programming (C#)

w3Schools What is OOP?

You also can search YouTube, there are plenty of resources to get started with the OOP Concept.

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