This article describes the Java object-oriented member hiding and property encapsulation operations. Share it for your reference, as follows:
I don't understand the advantages of this function yet, but I found a principle of object-oriented programming in the book - try to make the data private as much as possible. If you need to modify or obtain the data information, the member information will not be modified directly, but will be implemented through method calls.
Write the following code:
packagefengzhuang;class HuaShanPai{ private String zhangmen; private int dizishu; private String gongfu; public String getZhangmen() { return zhangmen; } public void setZhangmen(String zhangmen) { this.zhangmen = zhangmen; } public int getDizishu() { return dizishu; } public void setDizishu(int dizishu) { this.dizishu = dizishu; } public String getGongfu() { return gongfu; } public void setGongfu(String gongfu) { this.gongfu = gongfu; }}public class FengZhuang { public static void main(String[] args) { // TODO Auto-generated method stub HuaShanPai huashandizi = new HuaShanPai(); huashandizi.setDizishu(123); huashandizi.setGongfu("Zixia Divine Art"); huashandizi.setZhangmen("Yue Buqun"); System.out.println("Number of disciples of Huashan Sect: " + huashandizi.getDizishu()); System.out.println("Huashan Sect Kungfu: " + huashandizi.getGongfu()); System.out.println("Huashan Sect Head: " + huashandizi.getZhangmen()); }}Running results:
Number of disciples of Huashan Sect: 123
Huashan School Kung Fu: Zixia Divine Tactics Huashan School Head: Yue Buqun
First of all, what is worth mentioning about the above code is the implementation of the method. In fact, in eclipse, the function of implementing get and set is integrated with an automatic generation function. The implementation of the corresponding methods in the code uses this function. For specific functions, in the source menu bar, select the Generate getter and setter options to pop up a dialog box. Just check the properties you need to process in the dialog box.
It can be seen from the code in the main method that the modification of private attributes is realized through the set function, and similarly, the acquisition of the modified attributes is realized through the get function. Of course, it should be possible to obtain attributes any time.
At present, it is not clear what advantages or advantages this function has in software components. If you have an advantage, you can actually imitate some similar functions in C language. This technology can be expanded in disguise to the software construction that is currently working.
As for privatizing data, the code uses the private keyword to modify the attribute definition. In this way, the attributes cannot be obtained or modified when accessed directly through the attributes of an object. In this way, the data owned by the object is manifested as a private state.
For more Java-related content, readers who are interested in this site can view the topics: "Introduction and Advanced Tutorial on Java Object-Oriented Programming", "Tutorial on Java Data Structure and Algorithm", "Summary of Java Operation DOM Node Skills", "Summary of Java File and Directory Operation Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.