2011-01-13 128 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) 
}