所以我在使用 setter 和 getter 时遇到了麻烦,因为我现在无法获得 addNumbers 的值,所以我已经将其他人注释掉了,所以我现在并不担心它们。我最关心的是,一旦我将 addnumbers 的值传递到 setter 中,然后从 getter 中检索 if 然后在 addNumbers 中使用它,就需要用什么语法来写它。如果有人可以启发我或给我一个像样的例子,那会比我现在更好。非常感谢您提前提供的帮助!
class MainModule
{
static void Main(string[] args)
{
Info myInfo = new Info();
myInfo.DisplayInfo("Assignment 3A - Math Ops w/ Properties");
MathOperations ops = new MathOperations();
int input1, input2;
Console.WriteLine("Enter first number to calculate");
ops.operand1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter second number to calculate");
ops.operand2 = int.Parse(Console.ReadLine());
Console.WriteLine("Added: {0}", ops.addNumbers);
//Console.WriteLine("Subtracted: " + ops.subtractNumbers(value1, value2));
//Console.WriteLine("Divided: " + ops.divideNumbers(value1, value2));
//Console.WriteLine("Multiplied: " + ops.multiplyNumbers(value1, value2));
Console.ReadLine();
}
class MathOperations
{
private int operand1;
private int operand2;
public int Operand1
{
get
{
return operand1;
}
set
{
operand1 = value;
}
}
public int Operand2
{
get
{
return operand2;
}
set
{
operand2 = value;
}
}
public MathOperations()
{
//default constructor
}
public MathOperations(int op1, int op2)
{
operand1 = op1;
operand2 = op2;
}
public int addNumbers()
{
return operand1 + operand2;
}