-1

所以还有很多东西要学...

我有 3 个独立的课程,名为Beverage, Stock& Balance

这些课程以这种方式相互关联,只有当您有足够的余额并且仍有供应时,您才能订购饮料。

有 2 个类更容易写出来,但是有 3 个类我不知道这些类必须如何交互......任何人有什么想法吗?

4

1 回答 1

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
   }
}
于 2013-06-28T14:53:02.187 回答