为什么要私下上课或属性?
我想问一下为什么我应该私下班级或私有属性,我可以保护它免受什么保护?我认为,只有应用程序中的用户才能提供给它们或更新它们,例如(邮件,密码或年龄)只能是私有的,但他不要求填写的任何其他属性可能是公共的?是真的吗?
public class student{
private String mail;
public String getMail() {return mail;}
public mail(String email) {mail=email;}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,在任何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:
to the code outside the class
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.