是否可以在类外部访问受保护的方法-红宝石

时间:2018-07-02 06:34:53

标签: ruby

class User    
  def say_secret_with_self
    self.secret
  end

  protected

  def secret
    "secret"
  end
end

u = User.new
p u.say_secret_with_self   # => "secret"

我听说受保护的方法只能由继承的成员访问。但是以上是可能的红宝石。是正确的代码吗?

2 个答案:

答案 0 :(得分:4)

是的,这是正确的代码。该类的实例能够调用self方法,即使它们是protectedprotected/private方法是从外部隐藏的,而不是从内部隐藏的。

答案 1 :(得分:1)

方法0 rows仍然受到保护。您不能从您的班级(或继承的班级)外部调用它。但您可以从 live_grid.addOnScrollListener(new PaginationScrollListener(gridLayoutManager) { @Override protected void loadMoreItems() { // TODO: 07/11/16 check if totalPage > 1 before triggering next load Log.d(TAG, "live_grid loadMoreItems......."); int visibleItemCount = gridLayoutManager.getChildCount(); int totalItemCount = gridLayoutManager.getItemCount(); int firstVisibleItemPosition = gridLayoutManager.findFirstVisibleItemPosition(); Log.d(TAG, "visibleItemCount: "+ visibleItemCount); Log.d(TAG, "totalItemCount: "+totalItemCount); Log.d(TAG, "firstVisibleItemPosition: "+firstVisibleItemPosition); Log.d(TAG, "currentPage: "+currentPage); Log.d(TAG, "PAGE_SIZE: "+PAGE_SIZE); if (!isLoading && !isLastPage) { // if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount && firstVisibleItemPosition >= 0) { if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount && firstVisibleItemPosition >= 0 && currentPage < (PAGE_SIZE)) { //&& totalItemCount >= PAGE_SIZE firstVisibleItemPosition = visibleItemCount; loadMoreItems1(); } } // presenter.setIsLoading(true); // presenter.setCurrentPage(presenter.getCurrentPage() + 1); // presenter.loadNextPlaylist(); } 调用它,因为它仍与declare @Columnnew varchar(50) set @Columnnew ='NewColumn12' select 处于同一类