2017-10-20 45 views
-6

我想使用私有构造函数添加两个数字,但直到现在还没有失败。有没有办法添加它们?有没有办法在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; 
    } 
} 
+0

Plesae编辑你的问题,并将其添加到格式化的问题本身。还要解释这段代码有什么问题 –

回答

1

下面是一个例子:

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); 
    } 
} 
相关问题