站长资源脚本专栏

Ruby定义私有方法(private)的两种办法

整理:jimmy2025/2/26浏览2
简介#定义私有方法途径1:class Cdef public_methodprivate_methodenddef private_method endprivate :private_method #定义方法为私有end#定义私有方法途径2:class Cdef public_methodpri
#定义私有方法途径1:
class C
  def public_method
    private_method
  end
 
  def private_method 
  end
 
  private :private_method #定义方法为私有
end
 
#定义私有方法途径2:
class C
  def public_method
    private_method
  end
 
  private
  def private_method #定义私有方法
  end
end
 
C.new.public_method