为什么要在类中将变量声明为私有? [英] Why declare variables private in a class?

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

问题描述

我首先会道歉,因为我确定这已经在其他地方得到了解答-我只是找不到以我理解的方式解释它的答案!我正在修读MSc转换课程,但我仍在为此苦苦挣扎,其中包括一些基础知识-为什么将变量设为私有更好.

Folks I'll start by apologising as I'm sure this has been answered elsewhere - I just can't find an answer that explains it in a way I understand! I'm doing an MSc conversion course and there are some elementary basics that I'm still struggling with this, including this one - why making a variable private is better.

说我有一个名为Person的Java类,带有一个打印方法.我可以创建它并定义它:

Say I have a Java class called Person, with a print method. I could create it and define it as such:

public class Person
{
    public String name;
    public String telephoneNumber;

    public void print()
    {
       System.out.println(name + " " + telephoneNumber);
    }
}

然后,在主类中,我可以编写以下代码:

Then, in a main class, I could write the following code:

class testPerson
{
  public static void main(String[] args)
  {
    Person test;
    test = new Person();

    test.name = "Mary";
    test.telephoneNumber = "01234 567890";

    test.print();
  }
}

如果我这样做了,请test.print();会产生输出:

If I did this, the test.print(); would produce the output:

mary 01234 567890

但是,我知道这被认为是较差的编码.变量应该在公共类中是私有的,因为我们不想让人们看到此信息的存储方式或未经授权就可以编辑信息.

However, I know this is considered poor coding. Variables should be private inside a public class, as we don't want to allow people to see how this information is stored or to be able to edit information without authorisation.

所以现在,我将编辑Person类以将两个String声明为私有,并添加get和set方法,如下所示:

So now, I'll edit the Person class to declare the two Strings private and add get and set methods, like so:

private String name;
private String telephoneNumber;

public void setName (String name)
{
  this.name = name;
}

public void getName()
{
  return name;
}

// same code for telephone methods.

现在,在主类中,我将设置名称和电话的方法更改为以下内容:

Now, in the main class, I would change the methods of setting name and telephone to the following:

mary.setName("Mary");
mary.settelephoneNumber("01234 567890");

根据我要遵循的讲义,这样做效率更高(尽管可以通过添加Person()方法来实现实例化来提高效率)

According to the lecture notes I'm following, this is more efficient (although could be made even more efficient by adding in a Person() method to allow for instantiation etc.)

但是,我正努力了解为什么这会更好.
在前一种方法中,用户可以直接访问变量.但是,即使隐藏了它们无法直接访问的变量,用户也可以间接访问和修改它们,从而产生完全相同的结果.

However, I'm struggling to see why this is better.
In the former method of doing things, the user could directly access the variables. But even though by hiding the variables they can't directly access them, the user can indirectly access and modify them which produces the exact same outcome.

为什么这是首选,而我无疑会错过/忽略什么愚蠢的事情?

Why is it that this is preferred and what no doubt silly thing am I missing/overlooking?

推荐答案

并不是说它效率更高,而是因为它更易于维护和良好实践.

It's not that it's more efficient, it's that it's more maintainable and a good practice.

例如,使用setter方法,可以在实际进行设置之前,让 setTelephoneNumber 实际检查 String 是否是有效的电话号码.如果将变量设为公共变量,则不可能这样做.从一开始就使用二传手就意味着您可以回去,例如稍后添加验证,而如果您已将变量公开,则必须添加一个setter并修改所有用户以使用setter,而不是直接修改该变量.

For example, with setter methods, you could have your setTelephoneNumber actually check that the String is a valid telephone number before you actually do the setting. You couldn't possibly do that if you made the variable public. Using a setter from the very beginning means that you can go back and e.g. add validation later on, whereas if you had made the variable public, you would have to add a setter and modify all your users everywhere to use the setter instead of modifying the variable directly.

这篇关于为什么要在类中将变量声明为私有?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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