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

  • 关于我们
  • 免责声明