C# 如何在不创建实例的情况下访问变量?
C# how to access variables without creating an intance?
这就是我想要做的:
public class Worker
{
public int wage;
public void pay()
{
Economy.money -= this.wage;
// I want the money(of the economy) to be subtracted by the wage of the worker.
}
}
public class Economy
{
public int money;
}
如果能有1个以上的经济就好了
所以我希望经济体(工人所属)的货币减去工人的工资。
我该怎么做?
如果您想要多个经济体,那么您需要一个 属性 来跟踪每个工人属于哪个经济体。然后您可以使用该参考从正确的经济中减去工资:
public class Worker {
public Economy InEconomy { get; private set; }
public int Wage { get; private set; }
// set the econdomy and wage in the constructor
public Worker(Economy economy, int wage) {
this.Wage = wage;
this.InEconomy = economy;
}
public void Pay() {
InEconomy.money -= this.Wage;
}
}
public class Economy {
public int money;
}
这就是我想要做的:
public class Worker
{
public int wage;
public void pay()
{
Economy.money -= this.wage;
// I want the money(of the economy) to be subtracted by the wage of the worker.
}
}
public class Economy
{
public int money;
}
如果能有1个以上的经济就好了
所以我希望经济体(工人所属)的货币减去工人的工资。
我该怎么做?
如果您想要多个经济体,那么您需要一个 属性 来跟踪每个工人属于哪个经济体。然后您可以使用该参考从正确的经济中减去工资:
public class Worker {
public Economy InEconomy { get; private set; }
public int Wage { get; private set; }
// set the econdomy and wage in the constructor
public Worker(Economy economy, int wage) {
this.Wage = wage;
this.InEconomy = economy;
}
public void Pay() {
InEconomy.money -= this.Wage;
}
}
public class Economy {
public int money;
}