对象是否封装了数据,使得同一类的其他实例也无法访问该数据?

发布于 2024-07-18 12:08:42 字数 125 浏览 5 评论 0 原文

在Java中,

对象是否封装数据,使得同一类的其他实例都无法访问数据? 仅当使用关键字“private”时? Java 中的“访问器方法”是什么 - 像 getName() 这样的方法?

谢谢

In Java,

Do objects encapsulate data so that not even other instances of the same class can access the data? Only when the keyword "private" is used? What are "accessor methods" in Java - methods like getName()?

Thanks

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

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

发布评论

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

评论(7

你在我安 2024-07-25 12:08:43

即使字段/方法是“私有”的,仍然可以通过反射访问/调用它,除非您安装了不允许这样做的自定义安全管理器。

Even if a field/method is 'private', it can still be accessed/invoked via reflection unless you install a custom security manager that disallows that.

把时间冻结 2024-07-25 12:08:43

封装的思想是允许不同单元的实现自由变化。 虽然我们谈论的是对象,但对于封装来说,我们实际上指的是代码单元。 在基于类的语言中,代码单元通常是[外部]类。

如果没有在同一个类中进行访问,二进制操作(例如 equals)也会变得愚蠢。 所以私有意味着[外部]类私有,而不是同一实例中同一个类私有。

访问器方法通常表明除简单值对象(仅限 getter)之外的任何内容都存在不良设计。 对象应该有行为,而不仅仅是愚蠢的数据集合。 使用 getter 将位于外部的代码移至对对象有意义的方法。 仔细想想,99% 的情况下 getter 只是返回一个字段值。 如果您要添加 getter 和 setter,则将字段设为私有的价值相对较小。

The idea of encapsulation is to allow implementations of different units to vary freely. Although we talk of objects, for encapsulation we really mean a unit of code. In class-based languages, the unit of code is usually the [outer] class.

It also happens that binary operations (such as equals) become daft without access within the same class. So private means private to the [outer] class, not private to the same class within the same instance.

Accessor methods generally indicate bad design on anything but simple value objects (getters only). Objects should have behaviour, rather than just be a dumb collection of data. Move code that would be on the outside using getters to a method that is meaningful on the object. Hand on heart, 99% of the time getters just return a field value. There is relatively little value in making a field private if you are going to add a getter and setter.

三生殊途 2024-07-25 12:08:43

对象是否会封装数据,以便同一类的其他实例都无法访问数据?

当然,如果您不使用静态成员。

摘自此链接

有时,您希望拥有所有对象共有的变量。 这是通过 static 修饰符来完成的。 声明中带有 static 修饰符的字段称为静态字段或类变量

Do objects encapsulate data so that not even other instances of the same class can access the data?

Sure, if you are not using static members.

Extract from this link:

Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables

月光色 2024-07-25 12:08:43

什么是 Java 中的“访问器方法” -
像 getName() 这样的方法?

是的 - getFoo()setFoo() 是名为 foo 的“属性”的访问器方法 - 这是 JavaBeans 规范。 这些比公共字段更受欢迎的原因是它们允许您只有一个 getter(使属性只读),进行额外的簿记(如计算派生字段)和验证设置值(抛出例如 PropertyVetoException 当值不可接受时)。

整个东西最初是为了与 GUI 工具一起使用,它允许您以图形方式配置和组合 JavaBeans 以“构建应用程序”。 事实证明,这在很大程度上是一个白日梦,但 JavaBean 和属性的概念已被证明对于常规编码很有用,并且得到了广泛的传播。

许多人误解了这个概念,认为“封装”只是意味着为私有属性编写 setter 和 getter,而不是将它们公开 - 然后正确地认为这是愚蠢的。 封装意味着除非以严格控制的方式,否则根本不暴露类的内部工作原理。 在良好的 OO 设计中,类中不应该有太多的 get 方法和很少的 set 方法。

What are "accessor methods" in Java -
methods like getName()?

Yes - getFoo() and setFoo() are accessor methods for a "property" named foo - this is part of the JavaBeans specification. The reason why these are preferred over public fields is that they allow you to have only a getter (making the property read only), do additional bookkeeping (like calculating derived fields) and validation of set values (throwing e.g. a PropertyVetoException when the value is not acceptable).

The whole thing was originally intended to be used with GUI tools that would allow you to graphically configure and combine JavaBeans in order to "build applications". This turned out to be largely a pipe dream, but the concept of JavaBeans and properties has turned out to be useful for regular coding and become wide-spread.

Many people misunderstand the concept and believe that "encapsulation" just means writing setters and getters for private properties instead of making them public - and then rightfully consider that idiotic. Encapsulation means not exposing the inner workings of a class at all except in tightly controlled ways. In good OO design, you should not have too many get methods and very few set methods in a class.

小ぇ时光︴ 2024-07-25 12:08:42

我不倾向于将其视为一个对象可以访问另一个对象,而是将其视为什么代码可以访问对象内的哪些数据。

在 Java(和 C#,顺便说一句)中,类中的代码可以访问同一类的任何对象的私有成员。 然后您就获得了包/程序集访问权限和公共访问权限。

棘手的是受保护的访问,这是对子类中代码的某种访问 - 但这取决于目标对象:只有当对象是该对象的实例时,才允许访问该对象的受保护成员。与代码位置或某个子类相同的类型 - 即使它由父类公开。 例如,假设您有:

class Parent
{
    protected int x;
}

class Child1 extends Parent
class Child2 extends Parent
class Grandchild extends Child1

然后在 Child1 代码中,您只能访问 Parent.x 已知(在编译时)为以下实例的对象子1孙子。 例如,您不能使用 new Parent().xnew Child2().x

I don't tend to think of it in terms of one object having access to another, but rather what code has access to what data within an object.

In Java (and C#, btw) code within a class has access to the private members of any object of the same class. Then you've got package/assembly access and public access.

The tricky one is protected access, which is sort of access to code in subclasses - but it depends on the target object: you're only allowed to access protected members of an object if it's an instance of the same type as the location of the code, or some subclass - even if it's being exposed by a parent class. So for instance, suppose you had:

class Parent
{
    protected int x;
}

class Child1 extends Parent
class Child2 extends Parent
class Grandchild extends Child1

Then within the Child1 code, you can access Parent.x only for objects which are known (at compile-time) to be instances of Child1 or Grandchild. You couldn't, for instance, use new Parent().x or new Child2().x.

陌生 2024-07-25 12:08:42

不,私有字段甚至可以从其他实例(在同一类的方法内)访问。

然而,它们不能从子类访问,即使在同一实例中也是如此。

您提供 getter 方法来允许“外部”代码访问类中的字段。 由于提供哪些 getter、使它们可见的程度以及它们的实现方式取决于您,因此您可以对谁可以访问数据以及如何访问数据进行大量控制。

请注意,如果有 getName,则实际上并不需要有 name 字段:它完全取决于数据来源的 getter 的实现。

即使 getter(或 setter)仅包装私有字段,拥有这些 setter 和 getter 也是一种很好的风格(而不是允许直接字段访问)。

No, private fields can be accessed even from other instances (within a method of the same class).

They cannot be accessed from subclasses, however, not even within the same instance.

You provide getter methods to allow "outside" code to access fields in your class. Since it is up to you what getters you provide, how visible you make them, and how they are implemented, you can exercise a lot of control as to who can access the data and how.

Note that there does not really need to be a name field if there is a getName: it is entirely up to the implementation of the getter where that data comes from.

Even if the getter (or setter) just wraps a private field, it is good style to have these setters and getters (as opposed to allowing direct field access).

水水月牙 2024-07-25 12:08:42

getName() 应该返回名称(天气字段或其他“事物”)。

getName() should return the name (wheather field or some other "thing").

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