这是我试图在 C Sharp 中解决的问题。
我收到一个错误:
预期错误;或 =(不能在声明中指定构造函数参数)
谁能帮我解决这个问题或指导我解决这个问题?
namespace program
{
public class Integer
{
private int intvar;
public Integer()
{
intvar = 0;
}
public Integer(int x)
{
intvar = x;
}
public void display()
{
Console.Write(intvar);
Console.Write("\n");
}
public void add(Integer x, Integer y)
{
intvar = x.intvar + y.intvar;
}
}
class Program
{
static void Main(string[] args)
{
Integer a(5),b(45);
Integer c;
c.add(a,b);
c.display();
Console.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud)
你不能在 C# 中创建这样的对象。我假设你来自 C++,在那里这种语法是可能的。
在 C# 中,您必须使用 new 创建对象:
Integer foo = new Integer(45);
Run Code Online (Sandbox Code Playgroud)