Static inner classes in scala

前端 未结 5 1409
旧时难觅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:32

    Not sure I fully understood your use case... If it can help you, objects inside classes are visible like an instance's fields, e.g.

    case class C(var x: Int)
    class A { case object b extends C(0) }
    val a = new A
    println(a.b.x)
    a.b.x = 2
    println(a.b.x)
    

    Moreover, you can perfectly override a parent's val with object:

    case class C(var x: Int)
    class A { val y: C = C(0) }
    class B extends A { override case object y extends C(2) }
    val a: A = new B
    println(a.y.x)
    

提交回复
热议问题