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");
}


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