如何创建已经具有默认值的结构? 像这样:
struct First {
int data = 4;
int pos = 5;
}
void main () {
var a = First ();
assert(a.data == 4);
}
答案 0 :(得分:1)
AlThomas的回应:
“ Vara中的结构可以具有初始化程序(类似于类的构造函数)和方法。因此,我可以从第二个pastebin中提取的内容可以写为:”
struct First {
int data;
int pos;
public First (int[] mass) {
data= 5;
pos = mass.length;
}
public int sas () {
return data + pos;
}
}
void main () {
int[] a = {1,3,0,1,2,3,2,1};
var b = First (a);
print (@"$(b.sas ())\n");
}