參考自d程序設計語言---個人博客http://my.oschina.net/u/218155/blog?fromerr=SwOkb7Sw fllow meide
d語言的類,同全部的支持oop的語言一致,只是有一點點差異函數
類的一些基本屬性oop
enum,static變量,他們初始化一次後不可改變ui
支持普通的方法this
構造器語法 爲this()和 ~this()url
也支持靜態構造器 static this() static ~this()spa
final能夠定義不可覆蓋的類 .net
類也支持property方法 設計
this() 的執行順序是: 分配內存,初始化,冠名,構造函數的調用code
~this()的步驟 對象冠名後是活的。全部引用消失,檢查是否能夠回收,某個時間點回收,釋放已解除分配的對象
完整的例子
import std.stdio; class CrawlerParser { enum my_flate = 2; static immutable defualtOpt = "byCurl"; string currentOpt = defualtOpt; uint catchTime,catchWait; static CrawlerParser instance; static int test_static ; static this(){ //instance = new CrawlerParser; test_static = 11; } static string getDefualtOpt(){ return defualtOpt; } uint getCatchTime(){ return catchTime; } uint getCatchWait(){ return catchWait; } final void runFinal(){ writeln("run final"); } string crawlerIt(){ return "crawler"; } this(uint catchTime){ //this.catchTime = catchTime; this(catchTime,uint.init); } this(uint catchTime,uint catchWait){ this.catchTime = catchTime; this.catchWait = catchWait; } CrawlerParser func(){ //this = new CrawlerParser(); return this; } this() { } ~this(){ writeln("free data"); } static ~this(){ writeln("static free data"); } } /* class XMLCrawlerParser : CrawlerParser{ this(){ } override void crawlerIt(){ writeln("crawler it"); } }*/ unittest{ //define d auto cp = new CrawlerParser(); assert(CrawlerParser.getDefualtOpt() == "byCurl"); cp.catchTime = 3; assert(cp.getCatchTime() == 3); //d object refence auto cp2 = cp; cp.catchTime = 4; assert(cp.getCatchTime() == 4); CrawlerParser cp3 = new CrawlerParser(); assert(cp3 !is null); cp3.catchTime = 5; assert(cp.getCatchTime() == 4); //this() 分配內存,初始化,冠名,構造函數的調用 CrawlerParser cp4 = new CrawlerParser(1); assert(cp4.getCatchTime() == 1); assert(cp4.getCatchWait() == 0); //~this()的步驟 對象冠名後是活的。全部引用消失,檢查是否能夠回收, //某個時間點回收,釋放已解除分配的對象 destroy(cp2); assert(cp2 !is null); assert(cp !is null); //static this assert(CrawlerParser.test_static == 11); //static this的調用順序是先引入線調用,析構函數相反 //XMLCrawlerParser xcp = new XMLCrawlerParser; //assert(CrawlerParser.crawlerIt()== "crawler"); //xcp.crawlerIt(); }