与2个以上班级的一般互动
本文关键字:2个 | 更新日期: 2023-09-27 18:20:14
所以还有很多东西需要学习。。。
我有3个独立的类,分别命名为Beverage
、Stock
&Balance
。
这些类别是相互关联的,只有当你有足够的余额并且仍然有供应时,你才能点饮料。
有了2个类,写出来会更容易,但有了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
}
}