D語言結構體


D語言陣列允許定義型別的變數,可容納同類但是結構體幾個資料項,它允許結合不同型別的資料項提供另一種使用者定義的資料型別。

結構用來表示一個記錄,假設要跟蹤圖書館中的書籍。可能希望跟蹤了解每本書的以下屬性:

  • 書名

  • 作者

  • 科目

  • 書籍編號ID

定義一個結構體

要定義一個結構,必須使用結構體struct語句。該結構語句定義了一個新的資料型別,專案不止一個成員。該結構語句的格式是這樣的:

struct [structure tag]
{
   member definition;
   member definition;
   ...
   member definition;
} [one or more structure variables];  

結構體標籤是可選的,每個成員的定義是正常的變數定義,比如int i; or float f; 或任何其他有效的變數定義。在該結構的定義的結尾,最後的分號之前,可以指定一個或多個結構變數,但它是可選的。這裡是將宣告書的結構體:

struct Books
{
   char [] title;
   char [] author;
   char [] subject;
   int   book_id;
};

存取結構成員

要存取一個結構的任何成員,我們使用成員存取運算子(.)成員存取運算編碼為結構體變數名,而我們希望存取結構成員之間的時間段。可以使用struct關鍵字來定義結構型別的變數。下面是例子來說明結構的用法:

import std.stdio;

struct Books
{
   char [] title;
   char [] author;
   char [] subject;
   int   book_id;
};

void main( )
{
   Books Book1;        /* Declare Book1 of type Book */
   Books Book2;        /* Declare Book2 of type Book */

   /* book 1 specification */
   Book1.title = "D Programming".dup;
   Book1.author = "Raj".dup;
   Book1.subject = "D Programming Tutorial".dup;
   Book1.book_id = 6495407;

   /* book 2 specification */
   Book2.title = "Lua Programming".dup;
   Book2.author = "Raj".dup;
   Book2.subject = "Lua Programming Tutorial".dup;
   Book2.book_id = 6495700;

   /* print Book1 info */
   writeln( "Book 1 title : ", Book1.title);
   writeln( "Book 1 author : ", Book1.author);
   writeln( "Book 1 subject : ", Book1.subject);
   writeln( "Book 1 book_id : ", Book1.book_id);

   /* print Book2 info */
   writeln( "Book 2 title : ", Book2.title);
   writeln( "Book 2 author : ", Book2.author);
   writeln( "Book 2 subject : ", Book2.subject);
   writeln( "Book 2 book_id : ", Book2.book_id);
}

當上面的程式碼被編譯並執行,它會產生以下結果:

Book 1 title : D Programming
Book 1 author : Raj
Book 1 subject : D Programming Tutorial
Book 1 book_id : 6495407
Book 2 title : Lua Programming
Book 2 author : Raj
Book 2 subject : Lua Programming Tutorial
Book 2 book_id : 6495700

結構作為函式的引數

可以傳遞一個結構非常類似的方式函式作為引數傳遞任何其他變數或指標。存取在上面的例子已經使用類似的方式的結構變數:

import std.stdio;

struct Books
{
   char [] title;
   char [] author;
   char [] subject;
   int   book_id;
};

void main( )
{
   Books Book1;        /* Declare Book1 of type Book */
   Books Book2;        /* Declare Book2 of type Book */

   /* book 1 specification */
   Book1.title = "D Programming".dup;
   Book1.author = "Raj".dup;
   Book1.subject = "D Programming Tutorial".dup;
   Book1.book_id = 6495407;

   /* book 2 specification */
   Book2.title = "Lua Programming".dup;
   Book2.author = "Raj".dup;
   Book2.subject = "Lua Programming Tutorial".dup;
   Book2.book_id = 6495700;

   /* print Book1 info */
   printBook( Book1 );

   /* Print Book2 info */
   printBook( Book2 );

}

void printBook( Books book )
{
   writeln( "Book title : ", book.title);
   writeln( "Book author : ", book.author);
   writeln( "Book subject : ", book.subject);
   writeln( "Book book_id : ", book.book_id);
}

當上面的程式碼被編譯並執行,它會產生以下結果:

Book title : D Programming
Book author : Raj
Book subject : D Programming Tutorial
Book book_id : 6495407
Book title : Lua Programming
Book author : Raj
Book subject : Lua Programming Tutorial
Book book_id : 6495700

結構體初始化

結構可以有兩種形式,可使用建構函式和其他使用{}格式進行初始化。一個例子如下所示。

import std.stdio;

struct Books
{
   char [] title;
   char [] subject = "Empty".dup;
   int   book_id = -1;
   char [] author = "Raj".dup;

};

void main( )
{
   Books Book1 = Books("D Programming".dup, "D Programming Tutorial".dup, 6495407 );
   printBook( Book1 );

   Books Book2 = Books("Lua Programming".dup, "Lua Programming Tutorial".dup, 6495407,"Raj".dup );
   printBook( Book2 );

   Books Book3 =  {title:"Obj C programming".dup, book_id : 1001};
   printBook( Book3 );
}

void printBook( Books book )
{
   writeln( "Book title : ", book.title);
   writeln( "Book author : ", book.author);
   writeln( "Book subject : ", book.subject);
   writeln( "Book book_id : ", book.book_id);
}

當上面的程式碼被編譯並執行,它會產生以下結果:

Book title : D Programming
Book author : Raj
Book subject : D Programming Tutorial
Book book_id : 6495407
Book title : Lua Programming
Book author : Raj
Book subject : Lua Programming Tutorial
Book book_id : 6495407
Book title : Obj C programming
Book author : Raj
Book subject : Empty
Book book_id : 1001

靜態成員

靜態變數初始化一次。例如有這本書唯一的ID,我們可以使book_id靜態和遞增書籍ID。一個例子如下所示。

import std.stdio;

struct Books
{
   char [] title;
   char [] subject = "Empty".dup;
   int   book_id;
   char [] author = "Raj".dup;
   static int id = 1000;
};

void main( )
{
   Books Book1 = Books("D Programming".dup, "D Programming Tutorial".dup,++Books.id );
   printBook( Book1 );

   Books Book2 = Books("Lua Programming".dup, "Lua Programming Tutorial".dup,++Books.id);
   printBook( Book2 );

   Books Book3 =  {title:"Obj C programming".dup, book_id:++Books.id};
   printBook( Book3 );
}

void printBook( Books book )
{
   writeln( "Book title : ", book.title);
   writeln( "Book author : ", book.author);
   writeln( "Book subject : ", book.subject);
   writeln( "Book book_id : ", book.book_id);
}

當上面的程式碼被編譯並執行,它會產生以下結果:

Book title : D Programming
Book author : Raj
Book subject : D Programming Tutorial
Book book_id : 1001
Book title : Lua Programming
Book author : Raj
Book subject : Lua Programming Tutorial
Book book_id : 1002
Book title : Obj C programming
Book author : Raj
Book subject : Empty
Book book_id : 1003