可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Searching turns up a simple definition: data hiding.
But, consider the following two examples:
1) First Example:
Class Employee
{
public int age;
}
2) Second Example:
Class Employee
{
private int age;
public int getAge(){return age;}
}
Question:
In both the above specified examples, there is no data hiding, as age
is either being modified by others or being viewed by others. Where is the data hiding? How does encapsulation help in the above examples?
回答1:
There is data hiding. The second example hides how the value is stored. Also, the second example makes the value read-only for code outside the class. By having private fields and exposing them through methods or properties you immediately gain some advantages, some of which are:
- Access control granularity; you can choose to make the value publicly read-only, read-write or write-only.
- Possibility to validate input prior to storing the value
- Possibility to add logging or other auditing mechanisms
Note that encapsulation is not only about having access control. The primary use for encapsulation is to hide implementation details from calling code. When calling code wants to retrieve a value, it should not depend on from where the value comes. Internally, the class can store the value in a field or retrieve it from some external resource (such as a file or a database). Perhaps the value is not stored at all, but calculated on-the-fly. This should not matter to the calling code.
When you expose the value through a method or property, you shield calling code from such details, which also gives you the possibility to change the implementation, without affecting calling code.
回答2:
Encapsulation separates the concept of what something does from how it is implemented.
Encapsulation is a very important concept in Object Oriented Programming. It is hiding the data from other modules in the application. In your example 2, as you can see you can only get the age and that too using a getter method, but you can only set the value to the private member within that class and not outside it. This is the advantage of Encapsulation.
回答3:
Think about this variant:
class Employee
{
private int _age;
public string Age
{
get { return _age.ToString(); }
}
}
In your example the outside and inside use an integer and that is confusing. (Similar to showing 2 * 2 = 4 to explain multiplication and forgetting that 2 + 2 = 4 too)
回答4:
Encapsulation
is data hiding
simply.
let's see how encapsulation happens.
Assume you create a Employee class and include two instance variables. name and salary. You don't make them private. Now what happens another class in the program can access these two variables simply by creating an object if the employee class. So what we want is to hide these both or salary information to outside classes.
If we want to hide salary We can simply declare salary as a private
variable. Now we know even another class create an object and try to access salary as before it doesn't work. Because it is a private field which is only accessible to Employee class. Now we have hidden our data.
But assume that we need a person to enter salary details of each of the employees.
I will make it more clear.
The data entering person has 3 types of data to enter woked_hours, OT and leaves
(Emplyee class has three variables for this).
We want this person to enter these details but we do not want
him to see
how the salary calculation
is done. It is done according to the mathematical equation which is inside Employee class.
So he cannot calculate and enter salary it self. But we let him see the final salary. there We create a method to get the value of the salary inside employee class. it is
public int getSalary()
{
return salary;
}
He knows this, that there are two methods he can use to access those hidden fields. set method and get method.
So he call getmethod.
yes he can see
the salary
information. Because we have created the getMethod to return salary. Now he try to use set method to enter a new salary amount to change current value of salary. so he call e.setSalary(1000);
But it gives an error. Why? because in the employee class there is no method written as
public void setSalary(int newSalary)
{
salary=newSalary;
}
We intentionally didn't write that code because we don't want him to set salary value straight away. So we have hidden some information but some we still let him access. We gave him access to Read only
. But not Write
for salary.
so wasn't it useful now that encapsulation we have done.
The same way as he is allowed to set
these fields woked_hours, OT and leaves
. for those fields we have given him access to Write only. There we intentionally do not write getter methods
. If you write both getter
and setter you are allowing him to READ/ Write
both access.
It doesn't matter only set or get, The field is private, the field is hidden already data is hidden. But some access is given through getters
and setters
.
回答5:
In your first example the variable age is set as a public variable. Here anyone can modify the meeber age when an object of type Employee is declared.
However, in your second example the variable age is actually private, and can only be modified from inside of the class Employee. This is the case of data hiding.
In object oriented programming encapsulation is when one object is composed of another. This is in contrast to inheritance where an object can extent an already existing class.
回答6:
There is more to Encapsulation than data hiding. Encapsulation allows you to keep all the data and the functionality pertinent to an entity with this entity. Levels of visibility of this data is a related, but not identical, concept. E.g. an Employee
object caries all employee data and functionality (methods) with it. Note that this is not enforced, but enabled by the OO languages.
This may sound natural to you if you were born into the OO era, but this was the big leap from Functional Programming to OOP, both conceptual (design) and in languages that support this paradigm.
Quoting from Wikipedia article:
In a programming language
encapsulation is used to refer to one
of two related but distinct notions,
and sometimes to the combination
thereof:
- A language mechanism for restricting access to some of the
object's components.
- A language construct that facilitates the bundling of data with
the methods (or other functions)
operating on that data.
回答7:
Say I use your code like this:
Employee employee = new Employee();
int age = employee.getAge();
// Do something with age
Here, unless I look at your source code, I have absolutely no idea how Employee
has calculated its age. For all I know, getAge()
could look like this:
public int getAge() {
return new java.util.Random().nextInt( 100 );
}
or perhaps something even more complicated. I just don't know.
So in your second example, the fact that getAge()
just returns a private member variable is completely hidden from me.