Object exercises and java object-oriented exercises
(1) XiYouJiRenWu)
The attributes include height, name, and weapon)
Methods: printName and printWeapon)
(2) create two objects in the main method of the main class: zhuBaJie and sunWuKong. And
And call the printName and printWeapon methods respectively.
Displays the attribute values of two objects.
1 // create Class 2 String name; // name 3 double heiht; // height 4 String weapon; // weapon 5 6 void printName () 7 {8 System. out. print (name); 9} 10 void printWeapon () 11 {12 System. out. println ("name:" + name + ", weapon:" + weapon); 13}
1 worker renwu1 = new worker (); 2 renwu1.name = "Sun Wukong"; 3 renwu1.heiht = 160; 4 renwu1.weapon = "Ruyi golden hoop"; 5 6 7 worker renwu2 = new worker (); 8 renwu2.name = ""; 9 renwu2.heiht = 155; 10 renwu2.weapon = "nine-tooth dingtalk"; 11 12 years renwu3 = new round (); 13 renwu3.name = "Sha Seng "; 14 renwu3.heiht = 170; 15 renwu3.weapon = "Demon Master Rod"; 16 17 renwu4 = new slave (); 18 renwu4.name = "Tang Seng"; 19 renwu4.heiht = 180; 20 renwu4.weapon = "Zen stick"; 21 22 renwu1.printWeapon (); 23 renwu2.printWeapon (); 24 renwu3.printWeapon (); 25 renwu4.printWeapon ();
Result: