java基础练习

(1)编写西游记人物类(XiYouJiRenWu)
其中属性有:身高(height),名字(name),武器(weapon)
方法有:显示名字(printName),显示武器(printWeapon)
(2)在主类的main方法中创建二个对象:zhuBaJie,sunWuKong。并分别为他
们的两个属性(name,weapon)赋值,最后分别调用printName, printWeapon方法
显示二个对象的属性值。

package com.hanqi;

public class XiYouJiRenWu {
    
    String name;
    double height;
    String weapon;
    void name()
    {
        System.out.println(name);
    }
    void weapon()
    {
        System.out.println("用的武器是:"+weapon);
    }

}
    public static void main(String[] args) {
        XiYouJiRenWu renwu=new XiYouJiRenWu();
        renwu.name="孙悟空";
        renwu.height=1.24;
        renwu.weapon="如意金箍棒";
        renwu.name();
        renwu.weapon();
        XiYouJiRenWu renwu1=new XiYouJiRenWu();
        renwu1.name="猪八戒";
        renwu1.height=2.26;
        renwu1.weapon="九齿钉耙";
        renwu1.name();
        renwu1.weapon();

posted on 2016-05-16 11:11  煜渝  阅读(157)  评论(0编辑  收藏  举报

导航