加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Ruby中的私有方法

发布时间:2020-12-16 19:34:07 所属栏目:百科 来源:网络整理
导读:Rails控制器的一个例子,它定义了一个私有方法: class ApplicationController ActionController::Base private def authorization_method # do something endend 然后,它被用在ApplicationController的子类中: class CustomerController ApplicatioControll
Rails控制器的一个例子,它定义了一个私有方法:
class ApplicationController < ActionController::Base
  private
  def authorization_method
    # do something
  end
end

然后,它被用在ApplicationController的子类中:

class CustomerController < ApplicatioController
  before_action :authorization_method

  # controller actions
end

如何从其子类调用私有方法? private in Ruby是什么意思?

解决方法

无法使用显式接收器调用私有方法.但是它们可以由类的任何子类和实例调用.

Here是Ruby中公共,受保护和私有方法的一个很好的解释.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读