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
联系我们

邮箱 626512443@qq.com
电话 18611320371(微信)
QQ群 235681453

Copyright © 2015-2024

备案号:京ICP备15003423号-3