access java base class's static member in scala

我是研究僧i 提交于 2019-12-01 04:47:38

This isn't possible in Scala. Since Scala has no notation of static you can't access protected static members of a parent class. This is a known limitation.

The work-around is to do something like this:

// Java
public class BaseStatic extends Base {
  protected int getCount() { return Base.count; }
  protected void setCount(int c) { Base.count = c; }
}

Now you can inherit from this new class instead and access the static member through the getter/setter methods:

// Scala
class Derived extends BaseStatic {
  println(getCount());
}

It's ugly—but if you really want to use protected static members then that's what you'll have to do.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!