阅读(2022)
赞(18)
D编程 结构(Structs)
2021-09-01 10:27:18 更新
结构Structs用于表示记录,假设您想跟踪图书馆中的书籍,您可能需要跟踪有关每本书的以下属性-
- 标题 title
- 作者 author
- 主题 subject
- 图书ID book_id
定义结构
要定义结构,必须使用 struct 语句, struct语句定义了一种新的数据类型, struct语句的格式如下:
struct [structure tag] {
member definition;
member definition;
...
member definition;
} [one or more structure variables];
结构标签tag是可选的,每个成员定义都是一个普通的变量定义,如int i;或浮动f,在分号前面的结构定义末尾,您可以指定一个或多个可选的结构变量。这是声明Books结构的方式-
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="D Programming".dup;
Book2.author="Raj".dup;
Book2.subject="D 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 : D Programming
Book 2 author : Raj
Book 2 subject : D 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="D Programming".dup;
Book2.author="Raj".dup;
Book2.subject="D 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 : D Programming
Book author : Raj
Book subject : D 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("D Programming".dup,
"D 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 : D Programming
Book author : Raj
Book subject : D Programming Tutorial
Book book_id : 6495407
Book title : Obj C programming
Book author : Raj
Book subject : Empty
Book book_id : 1001
静态成员
静态变量仅初始化一次,如要获得书籍的唯一ID,我们可以将book_id设为static静态类型 ,并增加书籍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("D Programming".dup, "D 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 : D Programming
Book author : Raj
Book subject : D Programming Tutorial
Book book_id : 1002
Book title : Obj C programming
Book author : Raj
Book subject : Empty
Book book_id : 1003