我有几个类,想根据开关更改特定类的特定实例中的布尔值,但似乎无法获得指向该值的“指针”,我只是得到一个副本。
Class aClass;
Class bClass;
Class cClass;
public class Class
{
public bool b = true;
...
}
void Method(int i)
{
bool localBool;
switch(i)
case 1:
localBool = aClass.b;
break;
case 2:
localBool = bClass.b;
break;
case 3:
localBool = cClass.b;
break;
localBool = false; // This changes the localBool, but not Class.b
}
编辑:如果我在班级中有 3 个布尔值而不是 3 个班级怎么办?
Class aClass;
public class Class
{
public bool a = true;
public bool b = true;
public bool c = true;
...
}
void Method(int i)
{
bool localBool;
switch(i)
case 1:
localBool = aClass.a;
break;
case 2:
localBool = aClass.b;
break;
case 3:
localBool = aClass.c;
break;
localBool = false; // This changes the localBool, but not aClass.Bool
}