0
点赞
收藏
分享

微信扫一扫

d默认类构造器

何以至千里 2022-06-20 阅读 134

​​原文​​

enum defaultClassConstructor = q{
this(typeof(this.tupleof) params)
{
static foreach(i;0..this.tupleof.length)
{
this.tupleof[i] = params[i];
}
}
};

struct Color {}

class Point
{
Color rgba;
int x, y;
bool hidden;

mixin(defaultClassConstructor);
}

void main()
{
Point p = new Point(Color(),123,456,false);
assert(p.x == 123);
assert(p.y == 456);
assert(p.hidden == false);
}

如下也可以:

this(typeof(this.tupleof) params)
{
this.tupleof = params; /* 工作 */
}


举报

相关推荐

0 条评论