还有很多需要学习的东西......
我有3个独立的课程,名为Beverage
,Stock
& Balance
。
这些课程以这样的方式相互关联,即如果您有足够的余额并且仍有供应,您只能订购饮料。
有2个类,它更容易写出来,但有3个类我不知道这些类如何交互... 任何想法?
答案 0 :(得分:3)
这样的事情可能是:
public interface IOrder
{
bool CanOrder();
bool Order();
}
public class Beverage : IOrder
{
Stock _stock = null;
Balance _balance = null;
//In order to be able to construct Beverage, you HAVE TO
//pass Stock and Balance
public Beverage(Stock stock, Balance balance) {
_stock = stock;
_balance = balance;
}
//interface implementation
public void Order () {
if(!CanOrder())
return;
//make order
}
//interface implementation
public bool CanOrder() {
//check here against _stock and _balance
//if can order
}
}