In fact, has been in the process of doing Java development or just beginning to learn the time did not think so much, are simply to do, but since doing a project also slowly found that a lot of time, people's energy and brain is limited, will always forget what has been done or learned things, slowly began to write the habit of blogging, One can be self-reinforcing, two can be shared to others can discuss with you, because everyone's opinion will let you get different things, just like a teacher who met before, he said that every time he told the object-oriented have a different feeling, So it is also a kind of encouragement to themselves and the first-line programmer, hope that after many years we think back, we have been so hard to fight.
First of all, my understanding of encapsulation is simple.
The simple thing to say is that you create a class that displays untrusted classes for the properties and methods of a trusted class hidden, that is, the property privatization method is open
Again, the benefit is that you can protect the properties, reduce coupling, and better control the members.
Below is a demonstration of the next package with one of the simplest examples
public class FengZhuang {
private String name;
private int age;
Private String sex;
private double weight;
Double Heigh;
How to assign an age value
public void Setage (int.) {
if (age > 0 && age<120) {
This.age = age;
}else{
System.out.println ("Too old or too small");
}
}
Methods to get the age value
public int getage () {
return age;
}
public void mm () {
System.out.println ("mm");
}
}
Although the above example is relatively simple, a simple demonstration of the package should be possible.
Hope that the way the great God to correct and exchange, grateful!!!
This article is from the "Java" blog, so be sure to keep this source http://4534508.blog.51cto.com/4524508/1960927
Package of Java object-oriented three features