參考自d程序設計語言---個人博客http://my.oschina.net/u/218155/blog?fromerr=SwOkb7Sw fllow mehtml
突然懶得打字了,之後再慢慢添上吧。----坑已在下面填上。node
結構體和類的不一樣我也很困惑,個人理解,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};ide
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); }