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

方法

V没有Class。但是可以为类型定义方法。 方法是一个具有特殊接收器参数的函数, 只有指定类型的接收器才能执行此函数。 接收方在fn和方法名之间有自己的参数列表。

struct User {
    name string
    email string
mut:
    age int
}

fn (u User) can_register() bool {
    return u.age > 15
}

fn (u mut User) has_birthday() {
    u.age += 1
}

fn main() {
    mut bob := User {
        name: 'Bob'
        email: 'bob@bob.com'
        age: 15
    }
    alice := User {
        name: 'Alice'
        email: 'alice@alice-mail.com'
        age: 17
    }
    println(bob.can_register())
    println("Bob needs to be 16 to register, but he only is ${bob.age}.")
    println(alice.can_register())
    bob.has_birthday()
    println(bob.age)
}

输出

false
Bob needs to be 16 to register, but he only is 15.
true
16

上面的代码实现了两种类型为User的接收器u的方法。 注意,has\birthday()方法有一个mut接收器,这是必需的,因为我们要更改它的数据。 V的惯例不是使用诸如self、this之类的接收者名字,而是一个短的,最好是一个只有一个字母的名字。

练习

1.为Person类型创建一个确定某人是否未成年的方法。 2.创建一个确定Animal是否有毛发的方法。

Previous函数Next数组

Last updated 5 years ago

Was this helpful?