我正在尝试在main中输出一个变化的整数值,并在多个其他类中创建。出于学习目的,我看到是否可以做我正在尝试做的事情并查看导致错误的原因。这是代码:
class Program
{
static void Main(string[] args)
{
// how do I avoid an identifier error with unit.() if the other class is unit:stats?
unit.();
// how can I get the troop value from the unit:stats class?
Console.WriteLine(troop);
}
}
public class stats
{
public int troop;
}
public class unit : stats
{
public void archer()
{
troop = 100;
}
}
答案 0 :(得分:3)
您需要拥有对象的“实例”。所以,在你的主要:
unit myUnit = new unit(); //Create an instance of unit
myUnit.archer(); //Run the method that sets the troop number
Console.WriteLine(myUnit.troop);//Access the troop value from myUnit