(1) Writing the journey to the People Class (XIYOUJIRENWU)
The attributes are: height (height), name, weapon (weapon)
Methods are: Display Name (printname), display weapon (printweapon)
(2) Create two objects in the main method of the Master class: Zhubajie,sunwukong. and separately for his
The two attributes (Name,weapon) are assigned, and finally the Printname is called, Printweapon method
Displays the property values for two objects.
package Com.hanqi; Public class Xiyoujirenwu { double height; String name; String weapon; void Printname () { System.out.println ("This is" +name); } void Printweapon () { System.out.println (name+ "weapon is" +weapon "); } }
Public Static voidMain (string[] args) {//TODO Auto-generated method stubsXiyoujirenwu XiYouJiRenWu1=NewXiyoujirenwu (); Xiyoujirenwu1.name= "Monkey King"; Xiyoujirenwu1.weapon= "Ruyi Golden cudgel"; Xiyoujirenwu1.printname (); Xiyoujirenwu1.printweapon (); Xiyoujirenwu XIYOUJIRENWU2=NewXiyoujirenwu (); Xiyoujirenwu2.name= "Pig"; Xiyoujirenwu2.weapon= "Nine Teeth Rake"; Xiyoujirenwu2.printname (); Xiyoujirenwu2.printweapon (); }
Java Object-oriented exercises