Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala无法解析继承的Java接口常量成员_Java_Oop_Scala_Interface - Fatal编程技术网

Scala无法解析继承的Java接口常量成员

Scala无法解析继承的Java接口常量成员,java,oop,scala,interface,Java,Oop,Scala,Interface,Java中的类层次结构: 接口:集群、分类 类Kluster层次结构如下所示 Cluster <- , +-- Kluster Classify <- ' 文件:oop/Kluster.java package oop; public interface Cluster { public String HELLO = "hello"; } package oop; interface Classify { public String

Java中的类层次结构:

接口:集群、分类

类Kluster层次结构如下所示

Cluster  <- ,
            +-- Kluster
Classify <- '
文件:oop/Kluster.java

package oop;

public interface Cluster {
    public String HELLO = "hello";
}
package oop;

interface Classify {
    public String GOODBYE = "good bye";
}

public class Kluster implements Cluster, Classify {

}
package oop;

public class KlusterMain {
    public static void main(String[] args) {
        System.out.println(Kluster.HELLO);
        System.out.println(Kluster.GOODBYE);
    }
}
package oop

object cluster {
  def main(args: Array[String]) {
    val k = new Kluster
    println(Cluster.HELLO)
    println(Classify.GOODBYE)
    println(Kluster.HELLO) // <- this is the problematic line
  }
}
文件:oop/KlusterMain.java

package oop;

public interface Cluster {
    public String HELLO = "hello";
}
package oop;

interface Classify {
    public String GOODBYE = "good bye";
}

public class Kluster implements Cluster, Classify {

}
package oop;

public class KlusterMain {
    public static void main(String[] args) {
        System.out.println(Kluster.HELLO);
        System.out.println(Kluster.GOODBYE);
    }
}
package oop

object cluster {
  def main(args: Array[String]) {
    val k = new Kluster
    println(Cluster.HELLO)
    println(Classify.GOODBYE)
    println(Kluster.HELLO) // <- this is the problematic line
  }
}
到目前为止,一切正常。我可以打印“你好”和“再见”常量

现在,当我试图从Scala编译器访问它们时,它给出了一个错误

文件:oop/cluster.scala

package oop;

public interface Cluster {
    public String HELLO = "hello";
}
package oop;

interface Classify {
    public String GOODBYE = "good bye";
}

public class Kluster implements Cluster, Classify {

}
package oop;

public class KlusterMain {
    public static void main(String[] args) {
        System.out.println(Kluster.HELLO);
        System.out.println(Kluster.GOODBYE);
    }
}
package oop

object cluster {
  def main(args: Array[String]) {
    val k = new Kluster
    println(Cluster.HELLO)
    println(Classify.GOODBYE)
    println(Kluster.HELLO) // <- this is the problematic line
  }
}

为什么Scala不能解析Kluster对象同时实现集群接口和分类接口的层次结构?

您知道,将常量放入接口以在实现类时使用它们是不好的方法,不是吗?在Java中,如果需要,可以使用带有私有构造函数的
final
类和
import static
来缩短常量名称。在Scala中,您使用
对象
s和导入。但是Scala并没有静态字段的概念——它有适当参与继承的对象。用适当的面向对象系统统一Java中的静态字段是不可能的,所以在Scala中不能使用子类中的静态成员(字段和方法)


另请参见。

当然,我知道。这是ApacheMahout 0.7代码库中的简化代码。我试着使用Scala中的相同版本,Scala应该与Java兼容。我想Scala和Java之间还有更多的不兼容之处。是的,Scala应该(事实上)与Java兼容。兼容性的级别是个问题。在Scala中,Java中的几个设计决策不能以一种合理的方式得到支持,而不会破坏某些东西,因此Java的这些部分被认为不是非常重要,Scala也不支持它们。这种特性之一是静态继承。Scala没有静态的概念,继承静态成员在Java中是非常罕见的用例,因此Scala不支持它。