Java基础 第二节 第二十课

举报
我是小白呀iamarookie 发表于 2021/09/10 22:36:26 2021/09/10
【摘要】 引用类型用法总结 概述class 作为成员变量武器类盔甲类角色类测试类 interface 作为成员变量定义接口定义角色类定义测试类 interface 作为方法参数和返回值类型定义...

概述

实际开发中, 引用类型的使用非常重要, 也是非常普通的. 我们可以在理解基本类型的使用方式基础上, 进一步去掌握引用类型的使用方式. 基本类型可以作为成员变量, 作为方法的阐述, 作为方法的返回值. 那么引用类型也是可以的.

class 作为成员变量

在定义一个类 Role (游戏角色) 时, 代码如下:

public class Role {
    int id;  // 角色id
    int blood;  // 生命值
    String name;  // 角色名字
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5

使用 int 类型表示角色 id 和生命值, 使用 String 类型表示姓名. 此时, String 本身就是引用类型, 由于使用的方式类似常量, 所以往往忽略了它是引用类型的存在. 如果我们继续丰富这个类的定义, 给 Role 增加武器, 穿戴装备等熟悉, 我们将如何编写呢?

武器类

定义武器类, 将增加攻击能力:

public class Weapon {
    String name;  // 武器名称
    int damage;  // 伤害值

    // 构造方法
    public Weapon(){

    }

    public Weapon(String name, int damage){
        this.name = name;
        this.damage = damage;
    }

    // get/set方法
    public String getName() {
        return name;
    }

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

    public int getDamage() {
        return damage;
    }

    public void setDamage(int hurt) {
        this.damage = hurt;
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

盔甲类

定义盔甲类, 将增加防御能力, 也就是提升生命值:

public class Armor {
    String name;  // 装备名称
    int protect;  // 防御值

    // 构造方法
    public Armor(){

    }

    public Armor(String name, int protect){
        this.name = name;
        this.protect = protect;
    }

    // get/set方法
    public String getName() {
        return name;
    }

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

    public int getProtect() {
        return protect;
    }

    public void setProtect(int protect) {
        this.protect = protect;
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

角色类

定义角色类:

public class Role {
    int id;  // 角色id
    int blood;  // 生命值
    String name;  // 角色名字
    Weapon weapon;  // 武器属性
    Armor armor;  // 盔甲属性

    // 构造方法
    public Role(){

    }

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

    // get/set 方法

    public Weapon getWeapon() {
        return weapon;
    }

    public void setWeapon(Weapon weapon) {
        this.weapon = weapon;
    }

    public Armor getArmor() {
        return armor;
    }

    public void setArmor(Armor armor) {
        this.armor = armor;
    }

    // 攻击方法
    public void attack() {
        System.out.println("角色: " + this.name);
        System.out.println("使用"+ weapon.getName() +", 造成"+weapon.getDamage()+"点伤害");
    }
    // 穿戴盔甲
    public void equip() {
        // 增加防御,就是增加blood值
        this.blood += armor.getProtect();
        System.out.println("角色: " + this.name);
        System.out.println("装备"+armor.getName()+", 生命值增加"+armor.getProtect());
    }
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

测试类

定义测试类:

public class Test11 {
    public static void main(String[] args) {
        // 创建Role对象
        Role role = new Role(001,1,"马保国" );
        // 创建Weapon对象
        Weapon weapon = new Weapon("闪电五连鞭",0);
        // 创建Armor对象
        Armor armor = new Armor("浑元意形气功",-999);

        // 设置武器
        role.setWeapon(weapon);
        // 设置盔甲
        role.setArmor(armor);

        // 武器攻击
        role.attack();
        // 穿戴盔甲
        role.equip();
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

执行结果:
在这里插入图片描述
注: 类作为成员变量是, 对它进行赋值的操作, 实际上, 是赋给它该类的一个对象.

interface 作为成员变量

接口是对方法的封装, 对应游戏当中, 可以是看做扩展游戏角色的技能. 所以, 如果想扩展更强大技能, 我们在 Role 中可以增加接口作为成员变量, 来设置不同的技能.

定义接口

public interface CastSpell {
// 法术攻击
public abstract void spellAttack();
}

定义角色类

public class Role2 {
    CastSpell castSpell;

    public void setCastSpell(CastSpell castSpell){
        this.castSpell = castSpell;
    }
    // 法术攻击
    public void SpellAttack(){
        System.out.print("发动法术攻击: ");
        castSpell.spellAttack();
        System.out.println("攻击完毕");
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

定义测试类

public class Test12 {
    public static void main(String[] args) {
        // 创建游戏角色
        Role2 role = new Role2();
        // 设置角色法术技能
        role.setCastSpell(new CastSpell() {
            @Override
            public void spellAttack() {
                System.out.println("浑元意形街舞");
            }
        });

        // 发动法术攻击
        role.SpellAttack();

        // 更换技能
        role.setCastSpell(new CastSpell() {
            @Override
            public void spellAttack() {
                System.out.println("我闪闪闪闪闪闪");
            }
        });

        // 发动法术攻击
        role.SpellAttack();
    }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

执行结果:
在这里插入图片描述
注: 我们使用一个接口, 作为成员变量, 以便随时更换技能, 这样的设计更为灵活, 增强了程序的扩展性. 接口作为成员变量时, 对它进行赋值的操作, 实际上, 是赋给它该接口的一个子对象.

interface 作为方法参数和返回值类型

当接口作为方法的参数时, 需要传递什么呢? 当接口作为方法的返回值类型时, 需要返回什么呢? 对, 其实就是它的子类对象. ArrayList 类我们并不陌生, 查看 API 我们发现, 实际上, 它是java.util.List接口的实现类. 所以, 当我们看见 List 接口作为参数或者返回值类型时, 当然可以将 ArrayList 的对象进行传递或返回.

请观察如下方法: 获取某集合中所有的偶数.

定义方法

public static List<Integer> getEvenNum(List<Integer> list) {
        // 创建保存偶数的集合
        ArrayList<Integer> evenList = new ArrayList<>();
        // 遍历集合list,判断元素为偶数,就添加到evenList中
        for (int i = 0; i < list.size(); i++) {
            Integer integer = list.get(i);
            if (integer % 2 == 0) {
                evenList.add(integer);
            }
        }
        /*
         *返回偶数集合
         *因为getEvenNum方法的返回值类型是List,而ArrayList是List的子类,
         *所以evenList可以返回
         */
        return evenList;
    }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

调用方法

import java.util.ArrayList;
import java.util.List;

public class Test14 {
    public static void main(String[] args) {
        // 创建ArrayList集合,并添加数字
        ArrayList<Integer> srcList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            srcList.add(i);
        }
       /*
        *获取偶数集合
        *因为getEvenNum方法的参数是List, 而ArrayList是List的子类
        *所以srcList可以传递
        */
        
        List list = getEvenNum(srcList);
        System.out.println(list);
    }
}
输出结果:
[0, 2, 4, 6, 8]

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

注:

  • 接口作为参数时, 传递它的子类对象
  • 接口作为返回值类型时, 返回呀的子类对象

文章来源: iamarookie.blog.csdn.net,作者:我是小白呀,版权归原作者所有,如需转载,请联系作者。

原文链接:iamarookie.blog.csdn.net/article/details/110184688

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。