学Java第十四天-------类与对象的综合案例(一)

一、文字版格斗游戏

两个人格斗,根据每次攻击减去随机的血量,看最后谁能胜出

先定义一个Javabean类:有角色的姓名,有角色的血量

import java.util.Random;

public class Role {
    private String name;

    private int blood;

    public Role (){};
    public Role(String name, int blood) {
        this.name = name;
        this.blood = blood;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getBlood() {
        return blood;
    }

    public void setBlood(int blood) {
        this.blood = blood;
    }

    public void attack(Role role){
        Random r=new Random();
        int cutblood=r.nextInt(20)+1;

        int remainblood=role.getBlood()-cutblood;
        remainblood=(remainblood>0)?remainblood:0;

        role.setBlood(remainblood);
        //this代表的是调用者
        System.out.println(this.name+"举起拳头,打了"+role.name+",造成了"+cutblood+"伤害"+role.name+"还剩了"+remainblood+"点血");
    }
}

attack函数中的this代表的是调用者,用attack函数可以把两个对象联系起来

要使用对象的属性name,用getName()方法,遵循封装特性

public void attack(Role role){
        Random r=new Random();
        int cutblood=r.nextInt(20)+1;

        int remainblood=role.getBlood()-cutblood;
        remainblood=(remainblood>0)?remainblood:0;

        role.setBlood(remainblood);
        //this代表的是调用者
        System.out.println(this.getName()+"举起拳头,打了"+role.getName()+",造成了"+cutblood+"伤害"+role.getName()+"还剩了"+remainblood+"点血");
    }

再写: 

public class gameTest {
    public static void main(String[] args) {
        Role r1=new Role("鲁智深",100);
        Role r2=new Role("李逵",120);

        while(true){
            r1.attack(r2);
            if(r2.getBlood()<=0){
                System.out.println("鲁智深将李逵打败了");
                break;
            }

            r2.attack(r1);
            if(r1.getBlood()<=0){
                System.out.println("李逵将鲁智深打败了");
                break;
            }
        }

    }
}

知识点总结

1.可以不用非得在构造函数上面直接  "this.成员变量=成员变量",可以在构造函数中调用函数,在该函数中  "this.成员变量=成员变量"也可以.

2.成员方法中也可以调用成员方法,而且只要一个成员方法中没有涉及到与成员变量同名的局部变量,则提到的成员变量名就是成员变量.比如getName()中的return name,name指的就是成员变量.

3.不管要调用类中的哪个成员函数,首先都要用创建的对象来调用,如r1.showRoleInfo(),此时类中的成员变量的值都变成了r2中原先赋予的值

4.不管是在类中,还是在类外,如果要使用成员变量XXX,都要用getXXX()方法.

5.类中可以定义数组,并且在之后的成员方法中可以用到之前定义的数组 

二、pro版

增加了成员变量  面貌  和  性别  ,还有  攻击招式  和  受伤状况

这样的话,JavaBean如下:

package com.itheima.exe2;

import java.util.Random;

public class Role {
    private String name;
    private int blood;
    private char gender;
    private String face;

    String[] girlfaces = {"气宇轩昂", "相貌平平", "五官端正", "面目狰狞"};
    String[] boyfaces = {"美奂绝伦", "沉鱼落雁", "亭亭玉立", "相貌平平"};

    //攻击的描述
    String[] attacks_desc = {"%s使出了一招<背心钉>,转到对方的身后,一掌向%s背心的英台转过去.",
            "%s大喝一声,身形下伏,一招霹雷坠地,锤向%s双腿.",
            "%s上步抢身,招中套招,一招<披挂连环>,连环攻向%s."};

    //受伤的描述
    String[] injures_desc = {"结果%s退了半步,毫发无损.",
            "结果给%s造成一处瘀伤.",
            "结果一击命中,%s痛得弯下腰."};

    public Role() {
    }

    ;

    public Role(String name, int blood, char gender) {
        this.name = name;
        this.blood = blood;
        this.gender = gender;
        setFace(gender);
    }

    public char getGender() {
        return gender;
    }

    public void setGender(char gender) {
        this.gender = gender;
    }

    public String getFace() {
        return face;
    }

    public void setFace(char gender) {
        Random r = new Random();
        if (gender == '男') {
            int index = r.nextInt(boyfaces.length);
            this.face = boyfaces[index];
        } else if (gender == '女') {
            int index = r.nextInt(girlfaces.length);
            this.face = girlfaces[index];
        } else {
            this.face = "面目狰狞";
        }
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getBlood() {
        return blood;
    }

    public void setBlood(int blood) {
        this.blood = blood;
    }

    public void attack(Role role) {
        Random r = new Random();
        int index=r.nextInt(attacks_desc.length);
        String KungFu=attacks_desc[index];
        //要写成string.format()的形式!!!
        System.out.println(String.format(KungFu,this.getName(),role.getName()));
        int cutblood = r.nextInt(20) + 1;

        int remainblood = role.getBlood() - cutblood;
        remainblood = (remainblood > 0) ? remainblood : 0;

        role.setBlood(remainblood);
        //this代表的是调用者
        if(remainblood>70){
            System.out.println(String.format(injures_desc[0],role.getName()));
        }
        else if(remainblood>40){
            System.out.println(String.format(injures_desc[1],role.getName()));
        }
        else{
            System.out.println(String.format(injures_desc[2],role.getName()));
        }


    }

    public void showRoleInfo() {
        System.out.println("血量为" + getBlood());
        System.out.println("姓名为" + getName());
        System.out.println("性别为" + getGender());
        System.out.println("面貌为" + getFace());
    }

}

测试类不怎么变化:

package com.itheima.exe2;

public class gameTest {
    public static void main(String[] args) {
        Role r1=new Role("鲁智深",100,'男');
        Role r2=new Role("李逵",120,'男');

        /*r1.showRoleInfo();
        r2.showRoleInfo();*/
        while(true){
            r1.attack(r2);
            if(r2.getBlood()<=0){
                System.out.println("鲁智深将李逵打败了");
                break;
            }

            r2.attack(r1);
            if(r1.getBlood()<=0){
                System.out.println("李逵将鲁智深打败了");
                break;
            }
        }

    }
}

总结:

写字符串%s占位符,要用string.format(),比如

System.out.println(String.format(KungFu,this.getName(),role.getName()));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值