Static inner classes in scala

前端 未结 5 1435
旧时难觅i
旧时难觅i 2020-12-28 14:03

What is the analog in Scala of doing this in Java:

public class Outer {
  private Inner inner;

  public static class Inner {
  }

  public Inner getInner()          


        
5条回答
  •  庸人自扰
    2020-12-28 14:47

    As others have pointed out, "static" classes should be placed inside the companion object.

    In Scala, classes, traits, and objects which are members of a class are path-dependent. For example:

    class Button {
      class Click
    }
    
    
    val ok = new Button
    val cancel = new Button
    
    val c1 = new ok.Click
    val c2 = new cancel.Click
    

    Now c1 and c2 are instances of -different- classes. One class is ok.Click, and the other is cancel.Click. If you wanted to refer to the type of all Click classes, you could say Button#Click.

提交回复
热议问题