* 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);
}
文章來源于領測軟件測試網 http://www.kjueaiud.com/