Spring练习题

题目

某网络游戏程序中有如下几个类。
Equip类(装备类)

public class Equip {//装备
    private String name;//装备名称
    private String type;//装备类型,头盔,铠甲等
    private Long speedPlus;//速度增效
    private Long attackPlus;//攻击增效
    private Long defencePlus;//防御增效

    public String getName() {
        return name;
    }

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

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Long getSpeedPlus() {
        return speedPlus;
    }

    public void setSpeedPlus(Long speedPlus) {
        this.speedPlus = speedPlus;
    }

    public Long getAttackPlus() {
        return attackPlus;
    }

    public void setAttackPlus(Long attackPlus) {
        this.attackPlus = attackPlus;
    }

    public Long getDefencePlus() {
        return defencePlus;
    }

    public void setDefencePlus(Long defencePlus) {
        this.defencePlus = defencePlus;
    }

    @Override
    public String toString() {
        return "Equip{" +
                "name='" + name + '\'' +
                ", type='" + type + '\'' +
                ", speedPlus=" + speedPlus +
                ", attackPlus=" + attackPlus +
                ", defencePlus=" + defencePlus +
                '}';
    }
}

Player类(玩家类 )

public class Player { //玩家
    private Equip armet;//头盔
    private Equip loricae;//铠甲
    private Equip boot;//靴子
    private Equip ring;//指环
    
    //升级装备
    public void updateEquip(Equip equip){
        if ("指环".equals(equip.getType())){
            System.out.println(ring.getName()+"升级为"+equip.getName());
            this.ring = equip;
        }
    }

    public Equip getArmet() {
        return armet;
    }

    public void setArmet(Equip armet) {
        this.armet = armet;
    }

    public Equip getLoricae() {
        return loricae;
    }

    public void setLoricae(Equip loricae) {
        this.loricae = loricae;
    }

    public Equip getBoot() {
        return boot;
    }

    public void setBoot(Equip boot) {
        this.boot = boot;
    }

    public Equip getRing() {
        return ring;
    }

    public void setRing(Equip ring) {
        this.ring = ring;
    }

    @Override
    public String toString() {
        return "Player{" +
                "armet=" + armet +
                ", loricae=" + loricae +
                ", boot=" + boot +
                ", ring=" + ring +
                '}';
    }
}

玩家的装配

装备战神头盔连环锁子甲波斯追风靴蓝魔指环
速度增效2688
攻击增效44212
防御增效61532

(1)编写程序代码,以如下格式输出蓝魔指环的属性。

蓝魔指环[速度增效:8;攻击增效:12;防御增效:2]

(2)使用aop实现如下功能。

现举行免费升级指环的活动,可以免费将任意指环升级为"紫色梦幻"指环,新的装备名称为"紫色梦幻+原指环名",而且将在原指环的基础上再加上6点攻击,6点防御。
提示
使用前置通知增强,判断要升级的装备是否为指环,如果是,则需按需求修改传入的参数的名称,以及攻击增效和防御增效属性。

使用xml
applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean class="com.work1.Player">
        <property name="armet" ref="armet"/>
        <property name="loricae" ref="loricae"/>
        <property name="boot" ref="boot"/>
        <property name="ring" ref="ring"/>
    </bean>
    <bean id="armet" class="com.work1.Equip">
        <property name="name" value="战神头盔"/>
        <property name="type" value="头盔"/>
        <property name="speedPlus" value="2"/>
        <property name="attackPlus" value="4"/>
        <property name="defencePlus" value="6"/>
    </bean>
    <bean id="loricae" class="com.work1.Equip">
        <property name="name" value="连环锁子甲"/>
        <property name="type" value="铠甲"/>
        <property name="speedPlus" value="6"/>
        <property name="attackPlus" value="4"/>
        <property name="defencePlus" value="15"/>
    </bean>
    <bean id="boot" class="com.work1.Equip">
        <property name="name" value="波斯追风靴"/>
        <property name="type" value="靴子"/>
        <property name="speedPlus" value="8"/>
        <property name="attackPlus" value="2"/>
        <property name="defencePlus" value="3"/>
    </bean>
    <bean id="ring" class="com.work1.Equip" scope="prototype">
        <property name="name" value="蓝魔指环"/>
        <property name="type" value="指环"/>
        <property name="speedPlus" value="8"/>
        <property name="attackPlus" value="12"/>
        <property name="defencePlus" value="2"/>
    </bean>
    <bean class="com.work1.BeforeBoost" id="aspect"/>
    <aop:config>
        <aop:pointcut id="myPointCut" expression="execution(* com.work1.Player.updateEquip(..))"/>
        <aop:aspect ref="aspect">
            <aop:before method="ringBeforeAdvice" pointcut-ref="myPointCut"/>
        </aop:aspect>
    </aop:config>
</beans>

切面类

@Component
public class BeforeBoost {

    public void myPointCut(){

    }

    public void ringBeforeAdvice(JoinPoint joinPoint){
        //获取目标对象方法参数
        Object[] args = joinPoint.getArgs();
        // 使用数组
        List<Object> equips =  Arrays.asList(args);
        /*System.out.println(equips);*/
        
        Object object = equips.get(0);
        Equip equip= (Equip) object;
        if(equip.getType().equals("指环")){
            System.out.println("获取传入目标方法的参数对象" + equip);
            String name = "紫色梦幻" + equip.getName();
            equip.setName(name);
            equip.setAttackPlus(equip.getAttackPlus()+6);
            equip.setDefencePlus(equip.getDefencePlus()+6);
//            System.out.println(equip.getAttackPlus());
            System.out.println("修改后的参数对象"+equip);
        }
        System.out.println("----------------------------------------");
    }
}

测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class EquipTest {
    @Autowired
    private Player player;

    @Autowired
    @Qualifier("ring")
    private Equip equip;
    @Test
    public void test1() {
        Equip ring = player.getRing();
        System.out.println(ring);
        player.updateEquip(equip);
        System.out.println(player);
    }
}

结果是
在这里插入图片描述

使用注解
applicationContext2.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

    <!--扫描bean组件-->
    <context:component-scan base-package="com.obtk.work1"/>
    <!--启动基于注解的声明方式 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    <bean class="com.work1.Player">
        <property name="armet" ref="armet"/>
        <property name="loricae" ref="loricae"/>
        <property name="boot" ref="boot"/>
        <property name="ring" ref="ring"/>
    </bean>
    <bean id="armet" class="com.work1.Equip">
        <property name="name" value="战神头盔"/>
        <property name="type" value="头盔"/>
        <property name="speedPlus" value="2"/>
        <property name="attackPlus" value="4"/>
        <property name="defencePlus" value="6"/>
    </bean>
    <bean id="loricae" class="com.work1.Equip">
        <property name="name" value="连环锁子甲"/>
        <property name="type" value="铠甲"/>
        <property name="speedPlus" value="6"/>
        <property name="attackPlus" value="4"/>
        <property name="defencePlus" value="15"/>
    </bean>
    <bean id="boot" class="com.work1.Equip">
        <property name="name" value="波斯追风靴"/>
        <property name="type" value="靴子"/>
        <property name="speedPlus" value="8"/>
        <property name="attackPlus" value="2"/>
        <property name="defencePlus" value="3"/>
    </bean>
    <bean id="ring" class="com.work1.Equip" scope="prototype">
        <property name="name" value="蓝魔指环"/>
        <property name="type" value="指环"/>
        <property name="speedPlus" value="8"/>
        <property name="attackPlus" value="12"/>
        <property name="defencePlus" value="2"/>
    </bean>

</beans>

切面类

@Aspect
@Component
public class BeforeBoost {
    @Pointcut("execution(* com.obtk.work1.Player.updateEquip(..))")
    public void myPointCut(){

    }
    @Before("myPointCut()")
    public void ringBeforeAdvice(JoinPoint joinPoint){
        //获取目标对象方法参数
        Object[] args = joinPoint.getArgs();
        // 使用数组
        List<Object> equips =  Arrays.asList(args);
        /*System.out.println(equips);*/
        
        Object object = equips.get(0);
        Equip equip= (Equip) object;
        if(equip.getType().equals("指环")){
            System.out.println("获取传入目标方法的参数对象" + equip);
            String name = "紫色梦幻" + equip.getName();
            equip.setName(name);
            equip.setAttackPlus(equip.getAttackPlus()+6);
            equip.setDefencePlus(equip.getDefencePlus()+6);
//            System.out.println(equip.getAttackPlus());
            System.out.println("修改后的参数对象"+equip);
        }
        System.out.println("----------------------------------------");
    }
}

测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext2.xml")
public class EquipTest2 {
    @Autowired
    private Player player;

    @Autowired
    @Qualifier("ring")
    private Equip equip;
    @Test
    public void test1(){
        Equip ring = player.getRing();
        System.out.println(ring);
        player.updateEquip(equip);
        System.out.println(player);
    }
}

结果为
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值