简体   繁体   English

无法分配给'Money',因为它是'方法组'

[英]Cannot Assign to 'Money' Because It Is a 'Method Group'

I'm doing a project for class, and I have to call the Money function from my Player class. 我正在为课程做一个项目,我必须从我的Player类调用Money函数。 However, I do not know how to change Money into something else that is not a Method Group . 但是,我不知道如何将Money更改为其他不是Method Group I don't know much programming, so my range of solutions is rather small. 我不太了解编程,因此我的解决方案范围相当小。 Also, the instructor said I cannot make any changes to the Main class, only to the Player class. 此外,教师说我不能对Main类进行任何更改,只能对Player类进行任何更改。

Here's the code for the Main class: 这是Main类的代码:

 p1.Money += 400;

And here's the 'Money' function from my Player class: 这是我的Player类中的'Money'函数:

public int Money ()
    {
        return money;
    }

Money() is a method. Money()是一种方法。 You can't set it - it only returns something (an int ) (or nothing if void ). 你不能设置它 - 它只返回一些东西(一个int )(如果是void没有任何东西)。

You need to change it to a property that can also be set: 您需要将其更改为也可以设置的属性

public int Money {get; set;}

or, more elaborative: 或者,更详细:

private int _money;
public int Money { get { return _money; } set {_money = value;} }

It should be 它应该是

money += 400;

or change the Method to a property, 或将方法更改为属性,

public int Money {get;set;}

Depending on the context of where you are trying to do the increment (in class or in a class that uses the Money property (field) 取决于您尝试进行增量的上下文(在类或使用Money属性的类中(字段)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM