java中没有和有数据隐藏的程序

发布于 2025-01-18 06:17:38 字数 108 浏览 1 评论 0原文

我从理论上研究了java中的数据隐藏,但不知道里面发生了什么。每个教程都规定未经授权的人员无法访问他人的数据。

任何人都可以举例说明没有和有两个或三个用户以编程方式隐藏数据时会发生什么吗?

I have studied data hiding in java theoretically but don't know what is happening inside. Every tutorial, states that unauthorized persons cant access the data of others.

Can anyone please give an example of what will happen without and with data hiding with two or three users programmatically?

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

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

发布评论

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

评论(1

怎会甘心 2025-01-25 06:17:39

数据隐藏是向外部用户隐藏内部数据。这是通过将类的属性设置为私有并且不让类的对象直接访问它来实现的,而是我们创建 getter 和 setter 来访问私有属性。
示例:

//没有数据隐藏

public class Model{
public String name;
}

public class JavaApp{
public static void main(String args[]){
Model mObj = new Model();
mObj.name="abc";     // name = "abc"

}
}

//有数据隐藏

public class Model{
private String name;   //private name
}

public class JavaApp{
public static void main(String args[]){
Model mObj = new Model();
mObj.name="abc";     // Error
}
}

Data Hiding is hiding internal data from outside users. This is achieved by making the attributes of your class private and not letting the objects of the class access it directly, instead we create getters and setters to access the private attributes.
Example:

//Without Data Hiding

public class Model{
public String name;
}

public class JavaApp{
public static void main(String args[]){
Model mObj = new Model();
mObj.name="abc";     // name = "abc"

}
}

//With Data Hiding

public class Model{
private String name;   //private name
}

public class JavaApp{
public static void main(String args[]){
Model mObj = new Model();
mObj.name="abc";     // Error
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文