代码之家  ›  专栏  ›  技术社区  ›  Robert Rouse

从JRuby中的父类访问受保护的变量

  •  0
  • Robert Rouse  · 技术社区  · 14 年前

    这可能吗?我找不到任何文件表明是这样。我看到过在早期版本的JRuby上关闭的罚单。

    任何帮助都很好。

    public class Something {
    
      protected float somethingelse = 1.0f;
    
    }
    

    我想找点事做。

    1 回复  |  直到 14 年前
        1
  •  2
  •   michaeltwofish    14 年前

    自从 this fix ,包访问, private protected 可以通过使用 field_accessor field_reader :

    require 'java'
    java_import 'Something'
    
    
    class Something
      field_accessor :somethingelse
    end
    
    class Stuff < Something
      def anotherstuff
        puts self.somethingelse
      end
    end
    
    Stuff.new.anotherstuff