测试驱动开发(第二节)

对于那些可以被当做数值来使用的对象,我们称为VO(Value,Object)数值对象,数值对象的一个要求是一旦数值对象的实例变量值在构造函数中被指定,那么以后就再也不允许变化。
数值对象的一个隐含意思就是,所有的操作都必须返回一个对象,另一个隐含意思就是使用数值对象必须要实现equals函数
//Dollor实体类:
package com.hellokitty.pro;

public class Dollor {
public int ammount;
//构造函数
public Dollor(int amount) {
this.ammount = amount;
}
public Dollor times(int multiplier) {
return new Dollor(this.ammount*multiplier);
}
public boolean equals(Object object) {
Dollor dollor = (Dollor)object;
return ammount == dollor.ammount;
}
}
//测试类:
package com.hellokitty.tdd;

import org.junit.Test;

import junit.framework.Assert;

import com.hellokitty.pro.Dollor;

public class TestDollor {
@Test
public void testMultiPlication() throws Exception {
Dollor five = new Dollor(5);
Dollor six = new Dollor(6);
Dollor product = five.times(2);
Assert.assertEquals(10, product.ammount);
product = five.times(3);
Assert.assertEquals(15, product.ammount);
Assert.assertTrue(new Dollor(5).equals(new Dollor(5)));
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值