logo
  • 教程列表
  • SO官方文檔
  • 常量
    • 定義一個常量
    • 修改常量
    • 常量不能在方法中定義
    • 定義和更改類中的常量
  1. StackOverflow 文件
  2. Ruby Language 教程
  3. 常量
  4. 定義和更改類中的常量

定義和更改類中的常量

Created: November-22, 2018

class Message
  DEFAULT_MESSAGE = "Hello, world"

  def speak(message = nil)
    if message
      puts message
    else
      puts DEFAULT_MESSAGE
    end
  end
end

可以使用以下程式碼更改常量 DEFAULT_MESSAGE:

Message::DEFAULT_MESSAGE = "Hullo, world"
  • 常量不能在方法中定義

Copyright © 2018. All right reserved

tastones.com 备案号:鲁ICP备18045372号-1

  • 關於本站
  • 免責聲明