C# - 我如何分配这个变量? [英] C# - how would I assign this variable?
问题描述
我的代码是:
public class PublicPrivate:MonoBehaviour {
public float level = 10f;
float health = level * 10f;
void 更新(){
if (health < = 0 ){
Debug.Log( 你已经死了。);
}
}
然而,当我调试时,它告诉我:
public float level = 10f;
float health = level * 10f;
字段初始化程序无法引用非静态字段,方法或属性' PublicPrivate.level'
我理解为什么,但是我无法弄清楚我应该将我的'health'变量更改为,因为float或int不行。有任何想法吗?目前正在学习C#所以如果这个问题有明显的答案,请原谅我。
我尝试了什么:
我试过将它改成int和double,都没有用。不太确定还有什么可以尝试。
其他两个解决方案都是正确的,你不能使用一个字段来初始化另一个。但是,对于这种情况,有一个非常简单的解决方案,为默认级别值定义一个常量,并根据它初始化两者:
public class PublicPrivate:MonoBehaviour {
const float defaultLevel = 10f;
public float level = defaultLevel;
float health = defaultLevel * 10f;
你甚至可以做得更好,摆脱尽可能多的内联常量支持明确命名的常量,并且具有:
public class PublicPrivate:MonoBehaviour {
const float defaultLevel = 10f;
public float level = defaultLevel;
const float levelToHealthFactor = 10f;
float health = defaultLevel * levelToHealthFactor;
问题不在于变量的类型。您不能在由另一个字段变量初始化的类中具有字段变量。这意味着您不能使用级别
来初始化health
。
这是因为:
您不能使用实例变量来初始化另一个实例变量。为什么?因为编译器可以重新排列这些 - 不能保证在defaultReminder之前初始化提醒,所以上面的行可能会抛出NullReferenceException。
您可以在此链接阅读更多相关信息
其中一个解决方案是在构造函数中移动第二个变量初始化,或者两个变量初始化如:
< pre lang =c#> public class PublicPrivate:MonoBehaviour {
< span class =code-keyword> public float 级别;
float health;
public PublicPrivate()
{
level = 10f;
health = level * 10f;
}
void 更新(){
if (health < = 0 ){
Debug.Log( 你已经死了。);
}
}
你也可以参考这个问题
My code is:
public class PublicPrivate : MonoBehaviour {
public float level = 10f;
float health = level * 10f;
void Update () {
if (health <= 0) {
Debug.Log ("You have died.");
}
}
However, when I debug, it tells me:
public float level = 10f;
float health = level * 10f;
A field initialiser cannot reference the non-static field, method, or property 'PublicPrivate.level'
I understand why, but I can't figure out what I should change my 'health' variable to since a float or int won't work. Any ideas? Currently learning C# so please forgive me if this question has an obvious answer.
What I have tried:
I've tried changing it to an int and a double, neither worked. Not too sure what else to try.
The other two Solutions are correct, you cannot use one field to initialize another. For this case, however, there is very a simple solution, define a constant for the default level value and initialize both based on that:
public class PublicPrivate : MonoBehaviour { const float defaultLevel = 10f; public float level = defaultLevel; float health = defaultLevel * 10f;
You could even do better, by getting rid of as many in-line constants as possible in favor of a clearly named constant, and have:
public class PublicPrivate : MonoBehaviour { const float defaultLevel = 10f; public float level = defaultLevel; const float levelToHealthFactor = 10f; float health = defaultLevel * levelToHealthFactor;
The problem is not the type of variable. You cannot have a field variable in a class being initialised by another field variable. It means that you are not allowed to uselevel
for the initialisation ofhealth
.
This is because :
You cannot use an instance variable to initialize another instance variable. Why? Because the compiler can rearrange these - there is no guarantee that reminder will be initialized before defaultReminder, so the above line might throw a NullReferenceException.
You can read more details about it at this link
one of the solution is to move the second variable initialization in the constructor, or both variables initialization like:
public class PublicPrivate : MonoBehaviour { public float level; float health; public PublicPrivate() { level = 10f; health = level * 10f; } void Update () { if (health <= 0) { Debug.Log ("You have died."); } }
you can also refer to this question
这篇关于C# - 我如何分配这个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!