【发布时间】:2019-11-17 14:06:41
【问题描述】:
对不起,如果标题不准确,我真的不知道我要查找的内容的名称
假设我有这个类:
public class Potion(){
public int WaterAmount;
public int ReagentAmount;
}
现在,如果我想要一种方法来检查药水中的水量或试剂量,我会:
public int GetWaterAmount ( Potion pot ){
return pot.WaterAmount;
}
public int GetReagentAmount ( Potion pot ){
return pot.ReagentAmount;
}
现在我的问题是如何将这两种方法合二为一,以便我可以输入要检查的液体参数?这是我正在寻找的一些无效语法:
public int GetAmount ( Potion pot, int SelectedLiquid){
return pot.SelectedLiquid;
}
void main(){
GetAmount(pot, WaterAmount);
GetAmount(pot, ReagentAmount);
}
本质上我怎样才能让一个参数(selectedliquid)引用一个类中的不同变量(水量或试剂量)?
或者这是不可能的,我确实需要为每个要检查的变量使用 1 种方法?
【问题讨论】:
标签: c# unity3d methods parameters