2011-01-08 17 views
0

なぜ私は以下のことをやらないのか分かりません。誰も私がこれを達成する方法を知っていますか?エラー12フィールド初期化子が非静的なフィールド、メソッド、またはプロパティを参照できません 'WindowsGame1.Player.BaseStrength

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace WindowsGame1 
{ 
    public class Player 
    { 
     int BaseStrength = 10; 
     int BaseIntelligence = 10; 
     int BaseDexterity = 10; 
     int BaseStamina = 10; 
     int BaseSpeed = 10; 

     int Damage; 
     int SpellDamage; 
     int Accuracy; 
     int LifePoints; 
     int CastingSpeed; 

     ***int Damage = (BaseStrength/2);*** 

    } 
} 

私が手にエラーがある:

エラー12あなたの場合は、フィールド初期化子が非静的フィールド、メソッド、またはプロパティ「WindowsGame1.Player.BaseStrength

答えて

2

を参照することはできませんダメージを他のフィールドに基づいて計算される値にするにはproperty

int Damage 
{ 
    get 
    { 
     return BaseStrength/2; 
    } 
} 

一方、通常のフィールドを使用してオブジェクトをインスタンス化するときに一度設定する場合は、初期化コードをコンストラクタに配置する必要があります。

public class Player 
{ 
    int baseStrength = 10; 
    int damage; 

    public Player() 
    { 
     damage = baseStrength/2; 
    } 
} 
関連する問題