* Tests whether this money is zero
*/
public abstract boolean isZero();
/**
* Multiplies a money by the given factor.
*/
public abstract IMoney multiply(int factor);
/**
* Negates this money.
*/
public abstract IMoney negate();
/**
* Subtracts a money from this money.
*/
public abstract IMoney subtract(IMoney m);
/**
* Append this to a MoneyBag m.
*/
public abstract void appendTo(MoneyBag m);
}
这里我们定义一个java接口,表示了“金钱”这个神奇东西的一些美妙的抽象方法!早年有首迟志强的歌叫《钞票》:是谁制造的钞票,你在世上逞霸道,有人为你愁眉苦脸啊有人为你哈哈笑;姑娘为你走错了路,小伙子为你受改造!东奔又西跑,点头又哈腰,钞票!人人为你离不了钱哪!你这杀人不见血的刀…形象无比,不扯了,跑题啦!I am back!
之后我们实现这个接口,在src文件夹下定义一个叫做Money.java的类:
public class Money implements IMoney {
private int fAmount;
private String fCurrency;
/**
* Constructs a money from the given amount and currency.
*/
public Money(int amount, String currency) {
fAmount= amount;
fCurrency= currency;
}
/**
* Adds a money to this money. Forwards the request to the addMoney helper.
*/
public IMoney add(IMoney m) {
return m.addMoney(this);
}
public IMoney addMoney(Money m) {
if (m.currency().equals(currency()) )
return new Money(amount()+m.amount(), currency());
return MoneyBag.create(this, m);
}
文章来源于领测软件测试网 https://www.ltesting.net/