首先需要熟悉一下D语言。D语言目前还没有什么书(有一本日文的,不知道小日本怎的对D这么热衷),所能参考的就是D语言的官方文档啦。不太适合编程的初学者,对于有一定变成经验的人来说,倒是挺合适的。英文原文从DMD官方网站可以找到:
http://www.digitalmars.com/d/ ;中文翻译:
http://blog.csdn.net/uframer/ 。
下面的源码就是针对D语言文档中的没一节中的要点编写的,这是前面三节Declarations,Types,Properties中的主要概念,着重与C/C++不同的地方,并且加入了详细的中文注释。把下面的代码拷贝到一个文本文件中,主要,要存成unicode编码格式才能编译通过。
下面的源码就是针对D语言文档中的没一节中的要点编写的,这是前面三节Declarations,Types,Properties中的主要概念,着重与C/C++不同的地方,并且加入了详细的中文注释。把下面的代码拷贝到一个文本文件中,主要,要存成unicode编码格式才能编译通过。
import std.stdio;
//
引入D语言的标准io库, 它是"dmdlibphobos.lib"的一部分; 库文件的查找由sc.ini中的LIB环境变量指定.
void main()
{
try
{
tryDeclarations();
tryTypes();
tryProperties();
}
catch(Exception e)
{
writefln("There is an error:");
}
}
// 参考"dmdhtmlddeclaration.html"
void tryDeclarations()
{
// D语言中的变量在会初始化为默认值
int myInt;
uint myUint;
float myFloat;
writefln("default of int = ",myInt,
", default of uint = ", myUint,
", default of float = ", myFloat);
// 声明是从右向左读的
writef("This is an array : ");
int[4] myArray;
foreach(int i,int e;myArray)//如果下标越界,则会抛出一个Exception
writef(i, " = ", e,", ");
writefln();
//auto关键字
auto myAuto = 99;
writefln("this is an auto var, sizeof = ", myAuto.sizeof,
", value = ", myAuto);
typedef int MYINT; //typedef是强类型的
alias int MYINITA; //alias对于类型系统则与原来的类型相同
}
// 参考"dmdhtmld ype.html"
void tryTypes()
{
//enum可以指定数据类型
enum ETest : ubyte
{
ET_One, ET_Two
}
//使用cast关键字进行显示类型转换
int a = -1;
uint b = cast(uint)a;
writefln("b = ", b);
//function关键字用来声明函数指针
void function(/*参数列表*/) myFunctionPtr;
myFunctionPtr = &tryDeclarations;//指向一个函数
writefln("myFunctionPtr = ", myFunctionPtr);
//delegate可以指向类的成员函数
}
// 参考dmdhtmldproperty.html
void tryProperties()
{
//每种type都有相同的一些property
writefln("float.sizeof = ", float.sizeof,
", byte.init = ",byte.init,
", int.max = ", int.max,
", uint.max = ",uint.max);
//class和struct可以有property成员
struct Foo
{
int data() { return m_data; } // read property
int data(int value) { return m_data = value; } // write property
private:
int m_data;
}
Foo f;
f.data = 3; // same as f.data(3);
f.data = f.data + 3; // same as f.data() + 3;
writefln("f.data = ",f.data);
}
void main()
{
try
{
tryDeclarations();
tryTypes();
tryProperties();
}
catch(Exception e)
{
writefln("There is an error:");
}
}
// 参考"dmdhtmlddeclaration.html"
void tryDeclarations()
{
// D语言中的变量在会初始化为默认值
int myInt;
uint myUint;
float myFloat;
writefln("default of int = ",myInt,
", default of uint = ", myUint,
", default of float = ", myFloat);
// 声明是从右向左读的
writef("This is an array : ");
int[4] myArray;
foreach(int i,int e;myArray)//如果下标越界,则会抛出一个Exception
writef(i, " = ", e,", ");
writefln();
//auto关键字
auto myAuto = 99;
writefln("this is an auto var, sizeof = ", myAuto.sizeof,
", value = ", myAuto);
typedef int MYINT; //typedef是强类型的
alias int MYINITA; //alias对于类型系统则与原来的类型相同
}
// 参考"dmdhtmld ype.html"
void tryTypes()
{
//enum可以指定数据类型
enum ETest : ubyte
{
ET_One, ET_Two
}
//使用cast关键字进行显示类型转换
int a = -1;
uint b = cast(uint)a;
writefln("b = ", b);
//function关键字用来声明函数指针
void function(/*参数列表*/) myFunctionPtr;
myFunctionPtr = &tryDeclarations;//指向一个函数
writefln("myFunctionPtr = ", myFunctionPtr);
//delegate可以指向类的成员函数
}
// 参考dmdhtmldproperty.html
void tryProperties()
{
//每种type都有相同的一些property
writefln("float.sizeof = ", float.sizeof,
", byte.init = ",byte.init,
", int.max = ", int.max,
", uint.max = ",uint.max);
//class和struct可以有property成员
struct Foo
{
int data() { return m_data; } // read property
int data(int value) { return m_data = value; } // write property
private:
int m_data;
}
Foo f;
f.data = 3; // same as f.data(3);
f.data = f.data + 3; // same as f.data() + 3;
writefln("f.data = ",f.data);
}