知识若不分享 实在没有意义 http://www.d-programming-language-china.org 20070418
点击下面网址查看原文:
http://www.d-programming-language-china.org
by: uFramer D语言论坛 http://www.d-programming-language-china.org
from: http://www.digitalmars.com/d/property.html
version: 基于D 1.012 (Apr 12, 2007)
每种类型和表达式都有可以查询的property:
| Expression | Value |
| int.sizeof | yields 4 |
| float.nan | yields the floating point nan (Not A Number) value |
| (float).nan | yields the floating point nan value |
| (3).sizeof | yields 4 (because 3 is an int) |
| 2.sizeof | syntax error, since "2." is a floating point number |
| int.init | default initializer for int's |
| int.mangleof | yields the string "i" |
| int.stringof | yields the string "int" |
| (1+2).stringof | yields the string "1 + 2" |
所有类型的property
| Property | Description |
| .init | initializer |
| .sizeof | size in bytes (equivalent to C's sizeof(type)) |
| .alignof | alignment size |
| .mangleof | string representing the 'mangled' representation of the type |
| .stringof | string representing the source representation of the type |
整数数据型别的property
| Property | Description |
| .init | initializer (0) |
| .max | maximum value |
| .min | minimum value |
浮点型别的property
| Property | Description |
| .init | initializer (NaN) |
| .infinity | infinity value |
| .nan | NaN value |
| .dig | number of decimal digits of precision |
| .epsilon | smallest increment to the value 1 |
| .mant_dig | number of bits in mantissa |
| .max_10_exp | 以 10 为底数的最大阶码maximum int value such that 10max_10_exp is representable |
| .max_exp | 以 2 为底数的最大阶码maximum int value such that 2max_exp-1 is representable |
| .min_10_exp | 以 10 为底数的最小阶码minimum int value such that 10min_10_exp is representable as a normalized value |
| .min_exp | 以 2 为底数的最小阶码minimum int value such that 2min_exp-1 is representable as a normalized value |
| .max | largest representable value that's not infinity |
| .min | smallest representable normalized value that's not 0 |
| .re | real part |
| .im | imaginary part |
.init property
.init 产生一个常量表达式,其值为默认初始值。如果应用到某个类型,其值为该类型的默认初始值。如果应用到某个变量或域,其值为这个变量或域的默认初始值。例如:
int a;
int b = 1;
typedef int t = 2;
t c;
t d = cast(t)3;
int.init // is 0
a.init // is 0
b.init // is 1
t.init // is 2
c.init // is 2
d.init // is 3
struct Foo
{
int a;
int b = 7;
}
Foo.a.init // is 0
Foo.b.init // is 7
.stringof property
.stringof 产生常量字符串,如果应用到类型就是表示类型的字符,如果应用到表达式就是源表达式的文字形式, 语义分析程序这时不起作用。例如:
struct Foo { }
enum Enum { RED }
typedef int myint;
void main()
{
writefln((1+2).stringof); // "1 + 2"
writefln(Foo.stringof); // "Foo"
writefln(test.Foo.stringof); // "test.Foo"
writefln(int.stringof); // "int"
writefln((int*[5][]).stringof); // "int*[5][]"
writefln(Enum.RED.stringof); // "Enum.RED"
writefln(test.myint.stringof); // "test.myint"
writefln((5).stringof); // "5"
}
类和结构的property
property是成员函数,但在语法上被看作是域。属性可读可写。通过调用一个无参数的方法可以读取属性。通过调用一个带有一个参数的方法可以对属性赋值,参数代表属性的新值。
简单的属性可以是:( 本文出处: http://www.d-programming-language-china.org )
struct Foo
{
int data() { return m_data; } // 读属性
int data(int value) { return m_data = value; } // 写属性
private:
int m_data;
}
用法:
int test()
{
Foo f;
f.data = 3; // 等价于 f.data(3);
return f.data + 3; // 等价于 return f.data() + 3;
}
如果没有读方法就意味着属性是只写的。
D语言论坛 http://www.d-programming-language-china.org
按:这里原译作“如果没有读方法就意味着属性是只写的”,按原英文改正。
如果没有写方法就意味着属性是只读的。
可以有多个写方法共存,用正常的重载规则来决定采用哪个函数。
在其他方面,这些方法同其他的方法都是相同的。它们可以是静态的、拥有不同的链接方式、被重载、被取地址等等。
注意:目前属性不能作为 op=、++ 或者 -- 运算符的左值。
( lastupdate:20070421 最新文章请访问http://www.d-programming-language-china.org )关于一大步成功社区:
yidabu提倡在交流中学习,在分享中提高
收集感兴趣的知识,写下心得,通过网络与别人一起分享
理解一点就实践一步,收获什么就分享什么,成功就是这样一点点一步步累积起来的
网络只是一个工具,只有自己身心提高才是实实在在的。d-programming-language-china.org为大家提供一个学习交流各种知识的平台