为什么Java中的实例变量总是私有的? [英] Why are instance variables in Java always private?

查看:129
本文介绍了为什么Java中的实例变量总是私有的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,我正在学习封装,并看到了一个示例,其中实例变量在类中声明为私有。

I'm newbie to Java and I'm learning about encapsulation and saw an example where instance variables are declared as private in a class.

http://www.tutorialspoint。 com / java / java_encapsulation.htm

我有2个查询:


  1. 为什么实例变量是私有的?为什么不公开?

  2. 如果公开实例变量并直接访问该怎么办?我们是否看到了任何限制?

您能否举例说明在实例变量被声明为公共时会出现什么问题在Java中的一个类?

Can you explain with an example as to what will go wrong in case the instance variables are declared as public in a class in Java?

推荐答案

实例变量是私有的,以强制这些类的用户使用方法来访问它们。
在大多数情况下,有普通的getter和setter,但也可以使用其他方法。

Instance variables are made private to force the users of those class to use methods to access them. In most cases there are plain getters and setters but other methods might be used as well.

例如,使用方法可以限制访问只读,即如果没有设置器,则可以读取但不写入字段。如果该字段是公开的,那将无法实现。

Using methods would allow you, for instance, to restrict access to read only, i.e. a field might be read but not written, if there's no setter. That would not be possible if the field was public.

此外,您可以为字段访问添加一些检查或转换,这对于普通访问权限是不可能的。公共领域。如果某个字段是公开的,您之后想要通过执行其他检查等的某种方法强制进行所有访问。您必须更改该字段的所有用法。如果您将其设为私有,则只需稍后更改访问方法即可。

Additionally, you might add some checks or conversions for the field access, which would not be possible with plain access to a public field. If a field was public and you'd later like to force all access through some method that performs additional checks etc. You'd have to change all usages of that field. If you make it private, you'd just have to change the access methods later on.

如果手机是私人的:

If phone was private:

考虑这种情况:

class Address {
  private String phone;

  public void setPhone(String phone) {
    this.phone = phone;
  }
}

//access:
Address a = new Address();
a.setPhone("001-555-12345");

如果我们开始使用这样的类,稍后将需要对phoneNumber执行检查(例如,一些最小长度,仅数字等。)您只需要更改设置器:

If we started with the class like this and later it would be required to perform checks on the phoneNumber (e.g. some minimum length, digits only etc.) you'd just have to change the setter:

class Address {
  private String phone;

  public void setPhone(String phone) {
    if( !isValid( phone) ) { //the checks are performed in the isValid(...) method
     throw new IllegalArgumentException("please set a valid phone number");
    }

    this.phone = phone;
  }
}

//access:
Address a = new Address();
a.setPhone("001-555-12345"); //access is the same

如果手机是公开的:

If phone was public:

有人可以像这样设置手机,你什么也做不了关于它:

Someone could set phone like this and you could not do anything about it:

Address a = new Address();
a.phone="001-555-12345";

如果您现在要强制执行验证检查,则必须将其设为私有且无论谁编写上述行,都必须将第二行更改为:

If you now want to force the validation checks to be performed you'd have to make it private and whoever wrote the above lines would have to change the second line to this:

a.setPhone("001-555-12345");

因此你不能在不破坏其他代码的情况下添加支票(它不会再编译) 。

Thus you couldn't just add the checks without breaking other code (it wouldn't compile anymore).

此外,如果通过方法访问类的所有字段/属性,则保持访问一致,用户不必担心属性是否存储(即是一个实例字段)或计算(只有方法,没有实例字段)。

Additionally, if you access all fields/properties of a class through methods you keep access consistent and the user would not have to worry about whether the property is stored (i.e. is a instance field) or calculated (there are just methods and no instance fields).

这篇关于为什么Java中的实例变量总是私有的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆