有没有办法在 C# 中使用私有构造函数将两个数字相加
Is there a way to add two numbers using a private constructor in C#
我正在尝试使用私有构造函数添加两个数字,但直到现在都失败了。有没有办法添加它们?
class Program {
static void Main(string[] args) {
int add = Addition.add(); //Console.WriteLine()
Console.ReadKey();
}
}
public class Addition {
public static int num1, num2;
private Addition() {
Console.WriteLine("Enter two numbers");
num1 = int.Parse(Console.ReadLine());
num2 = int.Parse(Console.ReadLine());
}
public static int add() {
return num1 + num2;
}
}
这是一个例子:
public class Example {
public int Result {get; private set;}
private Example(int x, int y){
Result = x + y;
}
public static Example Create(int x, int y){
return new Example(x,y);
}
}
我正在尝试使用私有构造函数添加两个数字,但直到现在都失败了。有没有办法添加它们?
class Program {
static void Main(string[] args) {
int add = Addition.add(); //Console.WriteLine()
Console.ReadKey();
}
}
public class Addition {
public static int num1, num2;
private Addition() {
Console.WriteLine("Enter two numbers");
num1 = int.Parse(Console.ReadLine());
num2 = int.Parse(Console.ReadLine());
}
public static int add() {
return num1 + num2;
}
}
这是一个例子:
public class Example {
public int Result {get; private set;}
private Example(int x, int y){
Result = x + y;
}
public static Example Create(int x, int y){
return new Example(x,y);
}
}