不确定如何实现构造函数,然后通过实例调用它
本文关键字:然后 实例 调用 构造函数 何实现 实现 不确定 | 更新日期: 2023-09-27 18:34:13
好的,所以我有几个问题。让我先解释一下我必须做什么。我首先编写了这段代码,但没有构造函数,并且两个方法的声明中都有静态。现在我想添加一个提供给我的构造函数,我必须在 main 方法中使其成为实例,然后为它提供一些随机值。我还需要通过 Main 方法中的实例调用这两个方法。最后,编译后,它应该说"Hello World!"和"81"所以问题来了:
- 构造函数中的变量是否应该与方法中的变量命名相同?
- 如何在 main 方法中创建构造函数的实例?
- 如何给它一些随机值?
- 如何通过它们的实例调用这两种方法?
代码:
namespace CalculatorA
{
class Calculator
{
public string WriteText(string parametar1)
{
return parametar1;
}
public int WriteNumber(int parametar2)
{
return parametar2;
}
public Calculator(int operand1, int operand2)
{
this.operand1 = operand1;
this.operand2 = operand2;
}
}
class Program
{
static void Main(string[] args)
{
string s = Calculator.WriteText("Hello World!");
Console.WriteLine(s);
string n = Calculator.WriteNumber(53 + 28).ToString();
Console.WriteLine(n);
Console.ReadLine();
}
}
}
我认为你缺少一些面向对象的编程概念。构造函数几乎是一种初始化类实例的方法。
构造函数中的变量是否应该命名相同 作为方法中的那些?
必须命名构造函数参数。但是构造函数参数和方法参数之间没有联系。这些只是类的单独成员。
创建如何在 main 方法中创建构造函数的实例?
类的实例,但不创建构造函数。尽管当您使用 new
关键字启动对象时会调用构造。
如何给它一些随机值?
取决于类型。例如,对于整数,您可以使用 new Random().Next()
.
如何通过它们的实例调用这两种方法?
我想。。。只是打电话...就像那样:
calc.Method1();
calc.Method2();
问题 1:是和否 - 是,因为它们可以相似或相同地命名,并且由于其范围而仍然起作用。否,因为在示例代码中,您希望它们表示它们将持有的值,以便您可以一目了然地了解它们的用途。为简单起见,在我显示的示例中,我将保持它们在您的示例中的名称。
我建议谷歌搜索变量和命名约定的范围和生命周期。
此外,明智的做法是将字段或属性应用于Calculator
类,以便存储构造函数值以供类中的方法使用。 这应该是您对参数的意图。
问题2:创建Calculator
class
的实例
var calc = new Calculator(); //calc can be named to anything you want.
问题3:使用System.Random
实例然后生成各种随机数,如下所示。
var rnd = new Random();
var randomNumber1 = rnd.Next(0, 100);
var randomNumber2 = rnd.Next(0, 100);
//then using those in your Calculator.WriteNumber()
var calc = new Calculator(1,2); //without fields/properties this could be erroneous
var result = calc.WriteNumber(randomNumber1 + randomNumber2); //returns the sum of those to params.
问题4:您需要初始化Calculator
class
的实例 - 请参阅问题 2。然后使用该变量访问方法,例如:
var result = calc.WriteNumber(2); //returns 2
//or
var result = calc.WriteNumber(53 + 28); //returns 81
下面是一些可以从示例中学习的工作代码。希望这有帮助。
static void Main(string[] args)
{
//Create random numbers
var rnd = new Random();
var randomNumber1 = rnd.Next(1, 100); //get a random int between 1 and 99
var randomNumber2 = rnd.Next(1, 100); //and again
//Create instance of Calculator Class
var calc = new Calculator(randomNumber1, randomNumber2);
//Intro
string s = calc.WriteText("Hello World!");
Console.WriteLine(s);
//Do your addition method
var n = calc.WriteNumber(53 + 28);
Console.WriteLine(n);
//Do new addition method
var result = calc.Add(); //Returns _operand1 + _operand2
Console.WriteLine(result);
//exit application
Console.ReadLine();
}
class Calculator
{
// Set fields for your constructor to use.
// Another valid option is using properties, google this as its beyond the scope of a basic tutorial
private int _operand1, _operand2;
public string WriteText(string parametar1)
{
return parametar1;
}
public int WriteNumber(int parametar2)
{
return parametar2;
}
public int Add()
{
return _operand1 + _operand2;
}
public Calculator(int operand1, int operand2)
{
//You don't need the this keyword
_operand1 = operand1;
_operand2 = operand2;
}
}
- 对象的字段不必与构造函数的参数相同,尽管这种情况很常见。
-
var calculator = new Calculator(53,28);
- 使用
Random
类 -
calculator.WriteNumber(53+28);
构造函数中的变量是否应该与方法中的变量命名相同?
在构造函数中使用的变量未在类中声明。
你的类应该是这样的:
class Calculator
{
private int operand1 {get; set;} //need to declare the var before using
private int operand2 {get; set;}
private string Message {get; set;}
public string WriteText() //you don't need to send parameters to the function if you have already got them in the contructor
{
return Message;
}
public int GetSum()
{
return operand1 + operand2;
}
public Calculator(string Message, int operand1, int operand2)
{
this.Message = Message;
this.operand1 = operand1;
this.operand2 = operand2;
}
}
class Program
{
static void Main(string[] args)
{
Calculator CalcObj = new Calculator ("Hello word!", 53, 28); //That is how you can initialize a class object
string s = Calculator.WriteText(); //this will return you the string you passed in controller
Console.WriteLine(s);
string n = Calculator.GetSum().ToString(); //that will give you sum of two operand
Console.WriteLine(n);
Console.ReadLine();
}
}
对于您可以使用的随机数C# 中的 Random(( 方法在 main 方法中生成随机数并将它们传递给类的结构器,GetSum 会给你其中一些数字