LogoLogo
  • V by Example
  • en
    • examples
      • section_3
        • Functions
        • Methods
        • Arrays
        • Struct
      • section_1
        • Primitives
        • Keywords
        • Variables
        • Formatted Print
        • Strings
        • Comments
      • section_4
        • Files
        • JSON
        • Testing
        • Array Functions
      • section_2
        • Operators
        • Looping Constructs
        • If statement
        • Match
  • V por exemplos
    • Contribuindo
    • examples
      • section_1
        • Comentários
        • Primitivos
        • Palavras-chave
      • section_2
        • Operadores
  • V mit einem Beispiel
    • examples
      • section_3
        • Methods
        • Struct
        • Functions
        • Arrays
      • section_4
        • Testing
        • Array Functions
        • JSON
        • Files
      • section_2
        • Looping Constructs
        • If statement
        • Match
        • Operators
      • section_1
        • Primitive Datentypen
        • Schlüsselwörter
        • Formatted Print
        • Strings
        • Kommentare
        • Variables
    • CONTRIBUTING_de
  • V dengan Contoh
    • examples
      • section1
        • Variabel
  • 通过例子学V语言
    • 例子
      • section_3
        • 函数
        • 方法
        • 数组
        • 结构体
      • section_1
        • 基本类型
        • 关键字
        • 变量
        • print系列函数
        • 字符
        • 注释
      • section_4
        • 文件
        • JSON操作
        • 调试
        • 数组函数
      • section_2
        • 运算符
        • 循环结构
        • If表达式
        • 匹配
  • コード例で学ぶV言語
    • examples
      • section_3
        • 関数
        • メソッド
        • 配列
        • 構造体
      • section_1
        • プリミティブ型
        • キーワード
        • 変数
        • 書式付き出力
        • 文字列
        • コメント
      • section_4
        • ファイル操作
        • JSON操作
        • テスト
        • 配列の関数
      • section_2
        • 演算子
        • ループ
        • If文
        • マッチ
  • Changelog
  • Contributing
  • Documentation Style Guide
Powered by GitBook
On this page
  • 前缀&
  • 访问修饰符
  • 命名规则
  • 练习

Was this helpful?

  1. 通过例子学V语言
  2. 例子
  3. section_3

结构体

Previous数组Nextsection_1

Last updated 5 years ago

Was this helpful?

struct是一种复合数据类型(或记录)声明,它在内存块中的一个名称下定义物理分组的变量列表,允许通过单个指针或通过返回相同地址的struct声明的名称访问不同的变量。 对于来自[OOP]) 语言的人来说,它可以被认为是“类”,但有更多的限制。

struct User {
    name string
    email string
    country string
}

fn main() {
    user := User {
        name: "V developers"
        email: "developers@vlang.io"
        country: "Canada"
    }

    println(user.country)
}

注意:结构是在堆栈上分配的。

创建结构的新实例时,可以使用逗号分隔每个字段。当您想在一行上创建一个新实例时,它很有用。

user := User { name: "V developers", email: "developers@vlang.io", country: "Canada" }

前缀&

您还可以在堆上分配一个结构,并使用&前缀获取对它的引用,如下所示:

user := &User{"V developers", "developers@vlang.io", "Canada"}
println(user.name)

user的类型是&user。它是对User的引用。

访问修饰符

默认情况下,结构字段是private和immutable。它们的访问修饰符可以用pub和mut更改。

struct User {
    email string   // private and immutable (default)
}

您可以将它们定义为private mutable。

struct User {
    email string
mut:
    first_name string  // private mutable
    last_name string   // (you can list multiple fields with the same access modifier)
}

您还可以将它们定义为public immutable(只读)。

struct User {
    email string
mut:
    first_name string
    last_name string
pub:
    sin_number int     // public immutable (readonly)
}

或作为public,但仅在父模块中是mutable。

struct User {
   email string
mut:
   first_name string
   last_name string
pub:
   sin_number int
pub mut:
   phone int    // public, but mutable only in parent module
}

或父模块内外的public和mutable。

struct User {
    email string
mut:
    first_name string
    last_name string
pub:
    sin_number int
pub mut:
    phone int
__global:
    address_1 string    // public and mutable both inside and outside parent module
    address_2 string    // (not recommended to use, that's why the 'global' keyword
    city string         // starts with __)
    country string
    zip     string
}

命名规则

  • struct的名称应始终为大写。

练习

1.创建存储和显示“用户”信息的结构。 2.创建一个包含“x”和“y”字段的“Point”结构,并用private和public保护它们。

变量命名使用 。

https://en.wikipedia.org/wiki/Object-oriented_programming
Snake_Case