classXiyoujirenwu {floatHeight,weight; String head, Ear; voidspeak (String s) {head= "Crooked Head"; System.out.println (s); }} Public classExample4_3 { Public Static voidMain (String args[]) {Xiyoujirenwu zhubajie,sunwukong; //declaring ObjectsZhubajie =NewXiyoujirenwu ();//Assigning variables to ObjectsSunwukong =NewXiyoujirenwu (); Zhubajie.height=1.80f;//object assigns a value to its own variableZhubajie.head= "Big Head"; Zhubajie.ear= "A pair of big ears"; Sunwukong.height=1.62f;//object assigns a value to its own variablesunwukong.weight=1000f; Sunwukong.head= "embroidered floating serves fluttering"; System.out.println ("Zhubajie's height:" +zhubajie.height); System.out.println ("Zhubajie's head:" +zhubajie.head); System.out.println ("Sunwukong Weight:" +sunwukong.weight); System.out.println ("Sunwukong's head:" +sunwukong.head); Zhubajie.speak ("My old pig I want to marry a daughter-in-law");//Object Invocation MethodSystem.out.println ("Zhubajie now head:" +zhubajie.head); Sunwukong.speak ("Lao Sun I weigh 1000 pounds, I want to cheat eight to back Me");//Object Invocation MethodSystem.out.println ("Sunwukong now head:" +sunwukong.head); }}
Java Working with objects