C# C如何在不创建intance的情况下访问变量?

C# C如何在不创建intance的情况下访问变量?,c#,class,variables,instance,C#,Class,Variables,Instance,这就是我想做的: 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 in

这就是我想做的:

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;
}
我希望我能有不止一个经济舱

所以我想用工人的工资减去工人所属经济体的钱


如何做到这一点?

如果你想要一个以上的经济体,那么你需要一个记录每个工人所属经济体的财产。然后,您可以使用该引用从正确的经济体中减去工资:

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;
}

我想你在把作业发到这里之前必须先尝试一下