2014-03-28 81 views
0

我试图覆盖,看起来有点像这个斯卡拉 - 无法覆盖Java类

public class ExampleJava { 
    public ExampleJava(String string) { 

    } 

    protected String string; 
} 

当我使用

class ExampleScala(string : String) { 
    @Override 
    protected override def string : String 
} 
在斯卡拉

一个Java类的受保护的变量时,编译器会发出错误:

Error:(5, 7) overriding method string in class ExampleScala of type => 
java.lang.String; 
variable string in class ExampleJava of type net.java.lang.String has incompatible  
type; 
(Note that method string in class ExampleJava of type => java.lang.String is 
abstract, 
and is therefore overridden by concrete variable string in class ExampleJava of type 
java.lang.String) 
class ExampleScala(material : Material) extends ExampleJava(string : String) { 
^

更新:我不能莫迪fy ExampleJava,因为它在一个程序中被扩展,如果这个被释放它不会工作

回答

2
  1. 你不斯卡拉使用@Override;只有Java编译器才知道它。在斯卡拉override被用来代替。

  2. protected String string; in ExampleJava是一个字段,所以它根本不能被覆盖。如果你希望它是重写的,你需要写

    private String string; 
    
    protected String string() { 
        return string; 
    } 
    

代替。您还需要在ExampleScala中实际执行。

+0

任何方式做这样的事情,而不改变ExampleJava?我不能瓶坯修改ExampleJava因为它是在正在扩展程序,如果这个被释放它不会工作。 – yayestechlab

+0

不,根本无法覆盖字段。你究竟想实现什么? –

1

你不能@Override字段成员,你只能重写方法,它只是一个注释,确保在编译时天气你真的覆盖方法或者不

Indicates that a method declaration is intended to override a method declaration in a superclass. If a method is annotated with this annotation type but does not override a superclass method, compilers are required to generate an error message.

+0

它仍然发生没有@Override – yayestechlab