简体   繁体   English

getter/setter在封装中的作用是什么?

[英]What is the role of getter / setter in encapsulation?

As per my knowledge, encapsulation hides your data member from an outsider and only allows it access using methods of the same class.据我所知,封装对外部人员隐藏了您的数据成员,并且只允许使用相同 class 的方法进行访问。 So that they're not able to show your personal data to outsiders.这样他们就无法向外界显示您的个人数据。 Following way it's true,顺其自然,

private double amount;

public void add(double value) {
    return this.amount + value;
}

Here your amount variable doesn't share with outsiders using private and they don't know about the variable name you used.在这里,您的数量变量不会与使用 private 的外部人员共享,他们也不知道您使用的变量名称。

But in similar case we also provide getter / setter for access them so how can encapsulation become secure from outsiders?但是在类似的情况下,我们还提供了 getter / setter 来访问它们,那么封装如何才能对外界变得安全呢?

public void setAmount(double value) {
    this.amount = value;
}

public double getAmount() {
    return this.amount;
}

This means most probably getter or setter method has the same pattern and outsider which has a basic knowledge about this.这意味着很可能 getter 或 setter 方法具有相同的模式,而局外人对此有基本的了解。 So I suppose after doing some attempt he is able to get using getter or change using setter.所以我想在做了一些尝试之后,他能够使用 getter 或使用 setter 进行更改。 In such case, defining amount directly public or providing getter both becomes the same.在这种情况下,直接公开定义数量或提供 getter 都变得相同。

Can someone explain it?有人可以解释一下吗?

Imagine you have a program with a million lines of code, including the ones you've already provided.想象一下,您有一个包含一百万行代码的程序,包括您已经提供的代码。 Except your amount property is public.除非您的金额财产是公开的。

Now, imagine that in that code, you set up in line 600 that amount is equal to 300.00 Then in line 4547 you set it again to 500.50现在,假设在该代码中,您在第 600 行中设置金额等于 300.00 然后在第 4547 行中您再次将其设置为 500.50

Then in line 585676 you do it one more time.然后在第 585676 行再做一次。

Then you run your program and you run into errors.然后你运行你的程序,你会遇到错误。 You don't know where this error is coming from because it's all valid code.你不知道这个错误是从哪里来的,因为它都是有效的代码。 Then you realize that it must be something wrong with amount, but since you were just directly accesing your amount property, you end up having a horrible time trying to debug that.然后你意识到它一定是数量有问题,但由于你只是直接访问你的数量属性,你最终会在尝试调试它时遇到可怕的时间。

And then, imagine in partícular, that you created a bunch of separate classes and all of them were accesing that amount property and changing it.然后,特别想象一下,您创建了一堆单独的类,并且所有这些类都在访问该数量属性并对其进行更改。 So you're never getting the amount you want and you don't know where it's changing.所以你永远不会得到你想要的数量,你不知道它在哪里改变。

Lastly, imagine that you decide that you want to add a sales tax of 14% to your amount, so that every amount will be x * 0.14.最后,假设您决定要在您的金额中添加 14% 的销售税,这样每个金额将为 x * 0.14。 You may add the logic in the class, but since you have the property set as public, that logic won't be enforced in all of the instances where you directly assigned the value to amount.您可以在 class 中添加逻辑,但由于您将属性设置为公共,因此不会在您直接将值分配给金额的所有实例中强制执行该逻辑。

Basically: setters and getters are safety measures.基本上:setter 和 getter 是安全措施。 You do not want other classes being able to change the properties in OTHER classes.您不希望其他类能够更改其他类中的属性。 The example we used was simple but imagine you had a class with really complex properties, and you ended up changing the logic of these properties accidentally because you didn't use a setter.我们使用的示例很简单,但假设您有一个具有非常复杂属性的 class,并且您最终意外更改了这些属性的逻辑,因为您没有使用 setter。

Same goes for getters.吸气剂也是如此。 Imagine the opposite case, you change the internal logic of the class, but you were getting the values of its properties through some other way that wasn't a getter, now all of your code is broken because the other classes that were accessing the property were not made aware of the changes in logic.想象一下相反的情况,您更改了 class 的内部逻辑,但是您通过其他不是 getter 的方式获取其属性的值,现在您的所有代码都被破坏了,因为其他类正在访问该属性没有意识到逻辑的变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM