It has been almost three years since I came into contact with Java in my sophomore year. From the most basic HTML and CSS to the final SSH, I walked through it step by step. I was happy, disappointed, and lonely. Although he is a half-way monk, he has completed his "study" through his own efforts. During this period, I participated in training institutions, but I really didn’t like that kind of training method, so I gave up and chose self-study (poor I paid 6,000 yuan). Although self-study was a lot of pain, it was more fun, and the effort and gain were only You know it yourself. Huang Tian lived up to his hard work, but I am stupid. In the first semester of his senior year, he finally completed Java by himself (he took a detour and wasted half a year), and got a good job with it. Thank you very much!
Too much gossip! Coming to the topic, LZ has just finished reading the design pattern recently and has a lot of feelings. And in the process of working, I deeply feel that the foundation of Java is not solid enough. For example, I am not familiar with IO, I don’t understand garbage collection, polymorphism, reflection, and even the three most basic features confuse me! So I am determined to make up for my Java basics! Starting from the first lesson---Encapsulation!!!!!!
One of the three major characteristics---Encapsulation
Encapsulation literally means packaging, and the professional point is information hiding , refers to the use of abstract data types to encapsulate data and data-based operations to form an indivisible independent entity. The data is protected inside the abstract data type, hiding internal details as much as possible, and only retaining some external Interfaces allow it to communicate with the outside world. Other objects in the system can only communicate and interact with this encapsulated object through authorized operations wrapped outside the data. That is to say, the user does not need to know the internal details of the object (of course there is no way to know), but can access the object through the interface provided by the object.
For encapsulation, an object encapsulates its own properties and methods, so it can complete its own operations without relying on other objects.
There are three great benefits to use packaging:
1. Good packaging can reduce coupling.
2. The structure inside the class can be modified freely.
3. Members can be more precisely controlled.
4. Hide information and implement details.
First, let’s take a look at two classes: Husband.java, Wife.java
public class Husband { /* * 对属性的封装 * 一个人的姓名、性别、年龄、妻子都是这个人的私有属性 */ private String name ; private String sex ; private int age ; private Wife wife; /* * setter()、getter()是该对象对外开发的接口 */ public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void setWife(Wife wife) { this.wife = wife; } }
public class Wife { private String name; private int age; private String sex; private Husband husband; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public void setAge(int age) { this.age = age; } public void setHusband(Husband husband) { this.husband = husband; } public Husband getHusband() { return husband; } }
From the above two examples, we can see that the wife reference in Husband does not have a getter(). At the same time, Wife's age also has no getter() method. As for the reason, I think everyone knows it. Men hide their wives in deep houses, and no woman wants others to know her age.
So encapsulation privatizes the properties of an object and provides some methods for properties that can be accessed by the outside world. If we don’t want to be accessed by outside methods, we don’t have to provide methods for outside access. But if a class does not provide methods for external access, then this class is meaningless. For example, we regard a house as an object. The beautiful decorations inside, such as sofas, TV series, air conditioners, tea tables, etc., are the private properties of the house. But if we don't have those walls to block it, will others be able to see it at a glance? What about nothing left? No privacy at all! With that shielding wall, we can have our own privacy and we can change the furnishings inside at will without affecting others. But if there are no doors or windows, what is the meaning of a tightly wrapped black box? Therefore, others can also see the scenery inside through the doors and windows. Therefore, doors and windows are the interfaces of the house object left to the outside world for access.
Through this we cannot really appreciate the benefits of encapsulation. Now we analyze the benefits of encapsulation from a program perspective. If we do not use encapsulation, then the object does not have setter() and getter(), then the Husband class should be written like this:
public class Husband { public String name ; public String sex ; public int age ; public Wife wife; }
We should use it like this:
Husband husband = new Husband(); husband.age = 30; husband.name = "张三"; husband.sex = "男"; //貌似有点儿多余
But if that day We need to modify Husband, for example, what about changing age to String type? It's okay that you only use this class in one place. If you have dozens or even hundreds of such places, will you change it to a crash? If encapsulation is used, we don't need to make any modifications. We only need to slightly change the setAge() method of the Husband class. }
public class Husband { /* * 对属性的封装 * 一个人的姓名、性别、年龄、妻子都是这个人的私有属性 */ private String name ; private String sex ; private String age ; /* 改成 String类型的*/ private Wife wife; public String getAge() { return age; } public void setAge(int age) { //转换即可 this.age = String.valueOf(age); } /** 省略其他属性的setter、getter **/
Other places still reference (husband.setAge(22)) unchanged.
到了这里我们确实可以看出,封装确实可以使我们容易地修改类的内部实现,而无需修改使用了该类的客户代码。
我们在看这个好处:可以对成员变量进行更精确的控制。
还是那个Husband,一般来说我们在引用这个对象的时候是不容易出错的,但是有时你迷糊了,写成了这样:
Husband husband = new Husband(); husband.age = 300;
也许你是因为粗心写成了,你发现了还好,如果没有发现那就麻烦大了,逼近谁见过300岁的老妖怪啊!
但是使用封装我们就可以避免这个问题,我们对age的访问入口做一些控制(setter)如:
public class Husband { /* * 对属性的封装 * 一个人的姓名、性别、年龄、妻子都是这个人的私有属性 */ private String name ; private String sex ; private int age ; /* 改成 String类型的*/ private Wife wife; public int getAge() { return age; } public void setAge(int age) { if(age > 120){ System.out.println("ERROR:error age input...."); //提示錯誤信息 }else{ this.age = age; } } /** 省略其他属性的setter、getter **/ }
上面都是对setter方法的控制,其实通过使用封装我们也能够对对象的出口做出很好的控制。例如性别我们在数据库中一般都是已1、0方式来存储的,但是在前台我们又不能展示1、0,这里我们只需要在getter()方法里面做一些转换即可。
public String getSexName() { if("0".equals(sex)){ sexName = "女"; } else if("1".equals(sex)){ sexName = "男"; } else{ sexName = "人妖???"; } return sexName; }
在使用的时候我们只需要使用sexName即可实现正确的性别显示。同理也可以用于针对不同的状态做出不同的操作。
public String getCzHTML(){ if("1".equals(zt)){ czHTML = "<a href='javascript:void(0)' onclick='qy("+id+")'>启用</a>"; } else{ czHTML = "<a href='javascript:void(0)' onclick='jy("+id+")'>禁用</a>"; } return czHTML; }
鄙人才疏学浅,只能领悟这么多了,如果文中有错误之处,望指正,鄙人不胜感激!
以上就是 java提高篇(一)-----理解java的三大特性之封装的内容,更多相关内容请关注PHP中文网(www.php.cn)!