The purpose of abstraction is to produce classes, and the purpose of classes is to create objects with properties and behavior. Objects can not only manipulate their own variables to change state, but also call methods in the class to produce certain behaviors.
By using the operator "." (the dot operator is also called the reference operator or the access operator), an object can access its own variables and call methods.
After the object is created, it has its own variables, which are the entities of the object. Objects access their own variables by using the dot operator ".", access format:
object.variable;
After the object is created, you can use the dot operator "." to call methods in the class that created it, thereby producing certain behaviors. The calling format is:
object.method;
When an object calls a method, the member variables that appear in the method refer to the variables assigned to the object.
For example:
classXiyoujiRenwu{floatheight,weight;Stringhead;voidspeak(Strings){head=Tiantou;System.out.println(s);}} publicclassMain{publicstaticvoidmain(String[]args){XiyoujiRenwuzhubajie,sunwukong;//Declaration object zhubajie=newXiyoujiRenwu ();//Assign variables to the object sunwukong=newXiyoujiRenwu();zhubajie.height=1.80f;//The object assigns values to its own variables zhubajie.head=大头;sunwukong.height=1.60f;//The object assigns its own variables to Assign sunwukong.weight=1000f;sunwukong.head=long hair;System.out.println(zhubajie’s height:+zhubajie.height);System.out.println(zhubajie’s head:+zhubajie.head);System.out .println(sunwukong’s weight:+sunwukong.weight);System.out.println(sunwukong’s head:+sunwukong.head);zhubajie.speak(I want to marry a wife);//The object calls the method System.out. print1n (zhubajie’s current head: +zhubajie.head); sunwukong.speak (I want to trick Bajie into carrying me); // The object calls the method System.out.println (sunwukong’s current head: +sunwukong.head); }}
The running results are as follows:
zhubajie’s height: 1.8zhubajie’s head: big head sunwukong’s weight: 1000.0 sunwukong’s head: long hair I am an old pig who wants to marry a wife zhubajie’s current head: tilted My old sun wants to trick Bajie into carrying me sunwukong’s current head: tilted head