2011-01-13 60 views
2

Hoi,我正在学习scala并试图将一些Java代码翻译成Scala。这里有一些下面的代码在Java中,我想翻译Scala中的“不包含类”的含义

public class Note{ 
    protected void addNote(Meeting n) { 
     //add n to a list 
    } 
} 

public abstract class Meeting{ 

    public Meeting(String name, Note note){ 
     note.addNote(this) 
    } 
} 

当我把它们翻译成斯卡拉

class Note{ 
    protected[Meeting] addNote(n:Meeting){ 
     //add n to list 
    } 
} 

abstract class Meeting(name:String,note:Note){ 
    note.addNote(this) 
} 

后来我在课堂上注意的错误:会议不是一个封闭类。

这是什么意思?我试过包名而不是Meeting,就像这样:protected [packagename] addNote(n:Meeting),但它不起作用。

回答

1

你不能以这种方式做朋友类。尝试添加一个封闭的包装,像这样:

package translation 
class Note{ 
    protected[translation] def addNote(n:Meeting){ 
    //add n to list 
    } 
} 
abstract class Meeting(name:String, note:Note){ 
    note.addNote(this) 
}