c# - 我可以将数组和变量作为参数传递给 C# 中的方法吗?
问题描述
我正在制作一个 RPG,我有一个怪物遭遇功能,我在其中为敌人的攻击添加权重和模式,并且我可以在调用该功能时对其进行编辑:
数组和变量可以同时作为同一个函数的参数吗?如果没有,是否有任何替代方案可以实现相同的目标?
static void MonsterEncounter(float hp = 10, float speed = 3, float attack = 2, string monName = "Monster", float exp = 200F, float gold = 30, bool boss = false, bool doubleBattle = false, bool twoEnemy = false, float hpTwo = 10, float speedTwo = 10, float attackTwo = 1, string monNameTwo = "Monster 2", int teamMate = 0, string status = "Fine", string powerAttack = "Super Hit", float powerAttackHit = 10, float powerAttackHeal = 2, bool isHealing = false, bool isDefending = false, int waitTime = 0, string statusAfflict = "null", bool hasWeight = true, bool hasPattern = false, string statusTwo = "Fine",string powerAttackTwo = "Super Hit", float powerAttackHitTwo = 10, float powerAttackHealTwo = 2, bool isHealingTwo = false, bool isDefendingTwo = false, int waitTimeTwo = 0,string statusAfflictTwo = "null", bool hasWeightTwo = true, bool hasPatternTwo = false, int[] patternTwo = {1, 1, 1, 1, 1,1 ,1, 1, 1, 1}, int[] weight = {0, 0, 0, 0}, int[] pattern = {1, 1, 1, 1, 1,1 ,1, 1, 1, 1}, int[] weightTwo = {0, 0, 0, 0})
但这是相关的:
int[] patternTwo = {1, 1, 1, 1, 1,1 ,1, 1, 1, 1}, int[] weight = {0, 0, 0, 0}, int[] pattern = {1, 1, 1, 1, 1,1 ,1, 1, 1, 1}, int[] weightTwo = {0, 0, 0, 0}
但我在这里一直收到同样的错误:
main.cs(4265,254): error CS1525: Unexpected symbol '{'
main.cs(4265,255+): error CS1737: Optional parameter cannot precede required parameters
main.cs(4265,254): error CS1525: Unexpected symbol '1'
我尝试以不同的方式声明变量:
int[] patternTwo = new int[10] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, int[] weight = new int[4] {0, 0, 0, 0}, int[] pattern = new int[10] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, int[] weightTwo = new int[4] {0, 0, 0, 0}
但它仍然产生错误:
main.cs(4265,255+): error CS1736: The expression being assigned to optional parameter 'patternTwo' must be a constant or default value
main.cs(4265,255+): error CS1736: The expression being assigned to optional parameter 'weight' must be a constant or default value
main.cs(4265,255+): error CS1736: The expression being assigned to optional parameter 'pattern' must be a constant or default value
main.cs(4265,255+): error CS1736: The expression being assigned to optional parameter 'weightTwo' must be a constant or default value
我通过谷歌搜索,但我找到的页面要么使用不同的编程语言,要么不涵盖我的要求。
我可以将数组作为参数传递给 Java 中具有可变参数的方法吗?
数组和变量可以同时作为同一个函数的参数吗?如果没有,是否有任何替代方案可以实现相同的目标?
解决方案
这不是一个“真正的”解决方案,而是一种解决方法。您可以将默认数组值设置为null
,然后使用 if 语句设置所需的数组值
static void MonsterEncounter(float hp = 10, /*[...]*/ int[] patternTwo = null, int[] weight = null, int[] pattern = null, int[] weightTwo = null){
if (patternTwo == null) { pattern = new int [] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; }
//[...]
if (weightTwo == null) { weightTwo = new int[] { 0, 0, 0, 0 }; }
}
否则,您也可以使用重载。
推荐阅读
- c++ - 将复制返回的值分配给引用
- c++ - 关于在 C++ 中使用“+”进行整数变量和字符串连接的非常基本的问题
- python - 比较两个列表并识别出现差异的字符串位置
- android - 使用 setImageResource 后 Android 图像意外移动
- javascript - 我无法让 CSS-Grid-Template-Areas 工作。有任何想法吗?
- bash - 下载并覆盖 macOS 中的现有文件
- r - 将 ggplot 的 facet_grid 与 bmrs conditional_effects 结合
- javascript - 如何将html文本转换为mobi格式
- react-native - React-native 如何为状态变化的高度设置动画
- java - Java 线程停止一段时间