C# 类构造函数问题C
我有一个类使用这个构造函数,显然它似乎不起作用。在那之前,我只是忘了语法,你能提醒我怎么做吗C# 类构造函数问题C,c#,class,syntax,C#,Class,Syntax,我有一个类使用这个构造函数,显然它似乎不起作用。在那之前,我只是忘了语法,你能提醒我怎么做吗 public class Excerscise { Excerscise(int t, double w, DateTime d) : tries(t), weight(w), date(d) {} int tries; double weight; DateTime date; } 诸如此类: public class
public class Excerscise
{
Excerscise(int t, double w, DateTime d) : tries(t), weight(w), date(d) {}
int tries;
double weight;
DateTime date;
}
诸如此类:
public class Excerscise {
int tries;
double weight;
DateTime date;
// it seems, that the constructor should be public
public Excerscise(int t, double w, DateTime d) {
tries = t;
weight = w;
date = d;
}
}
一个例子
class Program
{
class C2
{
int A;
int B;
public C2(int a, int b)
{
A = a;
B = b;
}
}
static void Main()
{
C2 c = new C2(1, 2);
}
}
C不支持字段初始化语法,比如,你来自C++背景。这样做:
public class Exercise
{
int tries;
double weight;
DateTime date;
Exercise(int t, double w, DateTime d)
{
tries = t;
weight = w;
date = d;
}
}
如果你只是忘记了,为什么不查阅一下文档呢?聪明的说法是请为meSome编写这段代码,人们不会为它看起来的任何东西感到羞耻。显然,对于那些费心阅读文档的人来说,这篇文档完全不是C代码。复制/粘贴/痛苦地盯着一个不理解的编译器错误。该死,忘了把构造函数公之于众!非常感谢。