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


// ---------------------------------------------------------------------------------
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值