与2个以上班级的一般互动
General interaction with more than 2 classes
本文关键字:2个 更新时间:2023-10-16
所以还有很多东西需要学习。。。
我有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
}
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 如何在C++中从两个单独的for循环中添加两个数组
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 处理多个异常集合的C++方法
- 什么时候在C++中返回常量引用是个好主意
- 给定n个元素的m个集合.在C++中找到出现在最大集合数中的元素
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 尝试通过多个向量访问变量时,向量下标超出范围
- Ardunio UNO解决了多个重叠的定时器循环
- 如何返回一个类的两个对象相加的结果
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 数到第n个楼梯的路(顺序无关紧要)
- 基于多个条件处理地图中的所有元素
- 为多个会话保留XPtr
- 如何在C++中初始化嵌套类中的2个memeber
- 运算符C++ "delete []"仅删除 2 个前值
- IPC使用多个管道和分支进程来运行Python程序