参考自d程序设计语言---我的博客http://my.oschina.net/u/218155/blog?fromerr=SwOkb7Sw fllow me
忽然懒得打字了,以后再慢慢添上吧。----坑已在下面填上。
结构体和类的不同我也很困惑,我的理解,class表示逻辑操作,struct表示数据对象
列举下类和struct的不同
结构体能代替对象,反过来不行
结构体不支持继承和接口的实现
在结构体内不支持super
结构体不允许改写,所有方法都是最终的
结构体不能和synchronized和struct一起使用
struct不支持默认的构造方法this()
struct不支持定义构造方法this(this)
结构体不支持protected修饰符
结构体的复制,是值传递,对象是引用
比如 xx s1;auto s2 = s1;s1和s2指向两个不同的对象
结构对象传递给函数是值传递
结构对象的生命周期非常有限,类似函数里面临时变量的周期
构造函数,
默认不运行代码实现this()构造器
但是声明的时候可以使用默认的
转发构造函数是被允许的
this(this)如果结构里面含有引用类型数据
可以使用this(this)在拷贝的时候做些特别的操作
可以定义一个析构函数~this()
可以定义任意数量的的静态构造器和析构函数
结构可以定义方法和静态方法
@property支持属性的方式调用方法
结构内部可以嵌套类和结构
类内部也可以嵌套结构和类
#disable可以禁用一些方法
结构体的偏移align消除结构见的间隙
联合union和c语言一样
枚举值和枚举类型
enum ae =123;
enum a1{aa,ab,ac};
import std.stdio;
import std.algorithm;
import std.conv;
struct Widget {
enum fd = 0.2;
static immutable defaultName = "my Widget";
string name = defaultName;
uint width,height;
static double getFd(){
return fd;
}
void changeName(string otherName){
name = otherName;
}
int x = 22;
double y = 3.14;
}
class C {
int x = 11;
double y = 3.14;
}
void changeStruct(Widget w){
w.x += 100;
}
void changeStructRef(ref Widget w){
w.x += 100;
}
struct Test{
private int[] arr;
int a;
double b;
int length = 100;
//this(){}//error
this(double b){
this(b,int.init);
}
this(double b,int a){
this.b = b;
this.a = a;
arr = new int[length];
}
this(this){
arr = arr.dup;
}
int get(int key){
return arr[key];
}
void set(int key,int value){
arr[key] = value;
}
@property ulong len(){
return arr.length;
}
}
struct Test2{
Test t1;
int x;
}
struct Test3{
Test2 t2;
string name;
}
void fun(int a){
assert(a > 1);
}
struct showThis{
int a = 0;
int x = 11;
static int callTime = 10;
static this(){
writeln("static this 1");
}
static this(){
writeln("static this 2");
}
static ~this(){
writeln("static ~this 1");
}
static ~this(){
writeln("static ~this 2");
}
static ~this(){
writeln("static ~this 3");
}
this(int a){
this.a = a;
writeln("start show this a is ",a);
}
~this(){
writeln("show this is over a is ",a);
}
void fun(){
.fun(a);
writeln(a);
}
ref showThis opAssign(ref showThis ths){
x = ths.x+10;
writeln("##",x);
return this;
}
bool opEquals(ref const showThis ths) {
return ths.x == x;
}
}
struct List{
private:
class Node{
int value;
abstract Node left();
abstract Node right();
}
class NodeLeaf:Node{
Node _left,_right;
override Node left(){
return _left;
}
override Node right(){
return _right;
}
}
class Leaf:Node{
override Node left(){
return null;
}
override Node right(){
return null;
}
}
Node root;
public:
void add(int value){
writeln("add node");
}
void search(int value){
writeln("search value");
}
}
void crawerIt(){
struct HtmlAtrribute{
string code = "UTF-8";
int try_time = 3;
string refer = "http://www.baidu.com";
string html;
this(string html){
this.html = html;
}
void tidy(){
writeln("tidy");
}
void convertTo(){
writeln("conver to ",code);
}
~this(){
writeln(" clean HtmlAtrribute");
}
}
auto html = "addadf";
auto htmAtrObj = HtmlAtrribute(html);
htmAtrObj.tidy();
htmAtrObj.convertTo();
}
struct Final(T){
private T cur;
this(T load){
this.cur = load;
}
private void opAssign(Final);
@property T get() {return cur;}
alias get this;
}
struct MyWidget{
int x;
private intFloat _times;
union intFloat{
int _int;
double _float;
}
this(int x){
this.x = x;
}
void varDump(){
writeln("var dup struct");
}
}
//get type by template
template select(bool cond,T1,T2){
static if(cond){
alias T1 Type;
}else{
alias T2 Type;
}
}
template isSomething(T){
enum bool value = is(T:const(char[])) || is(T:const(wchar[])) || is(T:const(dchar[]));
}
mixin template InjectX(T){
private T x;
T getX(){ return x; }
void setX(T y){
x = y;
}
}
mixin InjectX!double;
class ClassInject{
mixin InjectX!double;
}
struct StructInject{
mixin InjectX!double;
}
void funcJectX(){
mixin InjectX!double;
setX(10);
assert(getX() == 10);
}
unittest {
//simple use struct
Widget w;
assert(w.getFd == 0.2);
w.changeName("weibo_page_book");
//assign on c
C c1 = new C;
auto c2 = c1;
c2.x = 22;
assert(c1.x == 22);
Widget w2;
auto w3 = w2;
w3.x = 88;
assert(w2.x != 88);
assert(w2.x == 22);
//struct as function param
changeStruct(w3);
assert(w3.x == 88);
changeStructRef(w3);
assert(w3.x == 188);
//while ref data in struct you need this(this)
auto t = Test(11.2);
assert(t.b == 11.2);
t.set(1,12);
assert(t.get(1) == 12);
t.set(2,22);
t.set(3,33);
t.set(4,44);
assert(t.len == 100);
auto t2 = t;
t2.a = 222;
t2.set(1,111);
assert(t.a != 222);
//assert(t.get(1) == 111);
assert(t.get(1) != 111);
//this(this) in struct{struct}
Test2 test2;
test2.t1 = Test(11.2);
Test3 test3;
test3.t2.t1 = Test(11.23);
Test tttt1 = Test(12.1);
//test this(this)
Test3 test33;
test33 = test3;
test3.t2.t1.set(1,9999);
assert(test33.t2.t1.get(1) != 9999);
//struct use area
{
showThis st = showThis(31);
st.fun();
}
/*
for(int i =0;i<10;i++){
showThis st = showThis(i);
}*/
showThis st1 = showThis(30);
showThis st2 ;
st2 = st1;
assert(st2.x == 21);
st2.x = 12;
//assert(st2.a == 40);
assert(st2 != st1);
crawerIt();
auto mywid = Final!(MyWidget)(MyWidget(12));
mywid.varDump();
void chageMywid(ref MyWidget my){
my = MyWidget(42);
}
//chageMywid(mywid);
writeln(mywid.x);
MyWidget* myref = new MyWidget(44);
(*myref).varDump();
//align(1)
assert(typeid(typeof(to!double(mywid.x))));
mywid._times._int = 11;
mywid._times._float = 11.2;
enum int enum_a = 1;
alias int myint;
myint myint_t = 17;
alias select!(false,int,double).Type myType;
assert(is(myType == double));
assert(isSomething!(string).value == true);
//assert(isSomething!(int).value == true);
funcJectX();
auto cj = new ClassInject();
cj.setX(10);
assert(cj.getX() == 10);
StructInject sj;
sj.setX(100);
assert(sj.getX() == 100);
}