2.按要求编写Java应用程序:
(1)编写西游记人物类(XiYouJiRenWu)
其中属性有:身高(height),名字(name),武器(weapon)
方法有:显示名字(printName),显示武器(printWeapon)
(2)在主类的main方法中创建二个对象:zhuBaJie,sunWuKong。并分别为他
们的两个属性(name,weapon)赋值,最后分别调用printName, printWeapon方法
显示二个对象的属性值。
package com.mianxiangduixiang; public class XiYouJiRenWu { double shenGao; String weapon; String name; XiYouJiRenWu(double sg,String w,String n) { shenGao=sg; name=n; weapon=w; } String name() { return name; } String weapon() { return weapon; } }
package com.mianxiangduixiang; public class XiYouJiRenWu1 { public static void main(String[] args) { XiYouJiRenWu WuKong =new XiYouJiRenWu(180.0, "金箍棒", "悟空"); { System.out.println(WuKong.name+WuKong.shenGao+WuKong.weapon); } XiYouJiRenWu BaJie =new XiYouJiRenWu(180.0, "钉耙", "八戒"); { System.out.println(BaJie.name+BaJie.shenGao+BaJie.weapon()); } } }