D語言遊戲編程(3):D語言基礎之Attributes

 下面是D語言文檔Attributes一節講述的主要內容,還是看代碼吧!
import std.stdio;

void main()
{
    tryAttributes();
}


//-- begin of Attributes ----------------------------------------------------------
//attribute語法上都支持後面跟一個大括號的形式
const
{
    
int A_CONST_INT = 100;
    
int B_CONST_INT = 100;
}

const int C_CONST_INT = 100;

abstract class MyBase
{
    
this()
    
{
        writefln(
"MyBase.ctor");
    }

    
~this()
    
{
        writefln(
"MyBase.dtor");
    }

    
    
void showMe()
    
{
        writefln(
"MyBase.showMe");
    }

}


class MyClass : MyBase
{
        
this()
        
{
            writefln(
"MyClass.ctor()");
            super(); 
//可以決定父類構造函數調用的位置,如果沒有這行則先調用
        }

        
        
~this()
        
{
            writefln(
"MyClass.dtor()");
        }

        
        
override void showMe()
        
{
            writefln(
"MyClass.showMe");
        }

        
}


void tryAttributes()
{
    
//attribute是聲明的修飾符
    
    

    
//
    
//虛基類不能創建實例
    
//MyBase mb = new MyBase;//Error: cannot create instance of abstract class MyBase
    
    MyClass mc 
= new MyClass;
    
    writefln(
"---- enter scope");
    
{
        
//scope -- 離開作用域之後自動析構
        scope MyClass mm = new MyClass;
                
        
//override函數調用
        MyClass tt = mm;
        tt.showMe();
    }

    writefln(
"---- leave scope");
}


//---------------------------------------------------------------------------------
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章