我最近开始学习C#,所以我绝不是专家。 无论如何,我试图创建一个面向对象的计算器,但我遇到了一个问题,似乎我无法从某些类中的其他类访问方法。我没有得到错误代码但是当我放下断点并调试应用程序时,变量的值为0。
如果我从DisplayUserData中删除该方法并将代码粘贴到我的主程序文件中,它运行正常。似乎DisplayUserData类不能传递变量或从其他类中检索变量,为什么会这样?
哦,原谅可怕的方法,类和变量名称。
这是我的主程序文件:
namespace Calculator
{
class Program
{
static void Main(string[] args)
{
GetUserInput userInput = new GetUserInput();
Calculator calculator = new Calculator();
DisplayUserData displayUserCalculation = new DisplayUserData();
userInput.MethodOfCalculation();
userInput.UserNumberInput();
displayUserCalculation.DisplayCalculatedUserData();
}
}
}
接下来是我的GetUserInput类:
namespace Calculator
{
public class GetUserInput
{
public char Choice;
public decimal ValueOne;
public decimal ValueTwo;
public void MethodOfCalculation()
{
Console.WriteLine("Welcome to Calculator v.1!");
Console.WriteLine("Please choose a method of calculation below.");
Console.WriteLine("1: Addition");
Console.WriteLine("2: Subtraction");
Console.Write("Your choice: ");
Choice = Console.ReadKey().KeyChar;
}
public void UserNumberInput()
{
Console.Clear();
Console.WriteLine("Please enter two numbers for calculation!");
Console.Write("Number One: ");
ValueOne = Convert.ToDecimal(Console.ReadLine());
Console.Write("Number Two: ");
ValueTwo = Convert.ToDecimal(Console.ReadLine());
}
}
}
计算器类:
namespace Calculator
{
public class Calculator
{
public decimal CalculateUserInput(char userChoice, decimal valueOne, decimal valueTwo)
{
decimal calculatedValue = 0;
switch (userChoice)
{
case '1':
calculatedValue = valueOne + valueTwo;
break;
case '2':
calculatedValue = valueOne - valueTwo;
break;
}
return calculatedValue;
}
}
}
最后是DisplayUserData类:
namespace Calculator
{
class DisplayUserData
{
GetUserInput _userInput = new GetUserInput();
Calculator _calculator = new Calculator();
public void DisplayCalculatedUserData()
{
decimal calculatedValue = _calculator.CalculateUserInput(_userInput.Choice, _userInput.ValueOne, _userInput.ValueTwo);
switch (_userInput.Choice)
{
case '1':
Console.WriteLine("Addition: " + _userInput.ValueOne + " + " + _userInput.ValueTwo + " = " + calculatedValue);
break;
case '2':
Console.WriteLine("Subtraction: " + _userInput.ValueOne + " - " + _userInput.ValueTwo + " = " + calculatedValue);
break;
}
}
}
}
答案 0 :(得分:5)
您正在UserInput
课程中创建Calculator
和DisplayUserData
课程的新版本(实例)。因为您使用的是新实例,所以这些实例无法访问main方法中使用的实例中包含的数据。不是在类中创建新实例,而是通过构造函数传递现有实例以供类使用。
namespace Calculator
{
class DisplayUserData
{
private readonly GetUserInput _userInput;
private readonly Calculator _calculator;
DisplayUserData(GetUserInput userInput, Calculator calculator)
{
_userInput = userInput;
_calculator = calculator;
}
public void DisplayCalculatedUserData()
{
decimal calculatedValue = _calculator.CalculateUserInput(_userInput.Choice, _userInput.ValueOne, _userInput.ValueTwo);
switch (_userInput.Choice)
{
case '1':
Console.WriteLine("Addition: " + _userInput.ValueOne + " + " + _userInput.ValueTwo + " = " + calculatedValue);
break;
case '2':
Console.WriteLine("Subtraction: " + _userInput.ValueOne + " - " + _userInput.ValueTwo + " = " + calculatedValue);
break;
}
}
}
}
然后将其称为
displayUserCalculation.DisplayCalculatedUserData(userInput, calculator);
答案 1 :(得分:3)
您的DisplayUserData类引用了与您的程序引用的类不同的Calculator和GetUserInput类。您应该像这样将相同的实例传递给DisplayUserData。
class DisplayUserData
{
readonly GetUserInput _userInput;
readonly Calculator _calculator;
public DisplayUserData(Calculator calculator, GetUserInput userInput)
{
_calculator = calculator;
_userInput = userInput;
}
public void DisplayCalculatedUserData()
{
...
}
}
程序将是:
class Program
{
static void Main(string[] args)
{
GetUserInput userInput = new GetUserInput();
Calculator calculator = new Calculator();
DisplayUserData displayUserCalculation = new DisplayUserData(calculator, userInput);
userInput.MethodOfCalculation();
userInput.UserNumberInput();
displayUserCalculation.DisplayCalculatedUserData();
}
}