"test"; if (o instanceof String s) { System.out.println(s.length()); } Object o = "test"; if (o instanceof String) { String s = (String) o; System.out.println(s.length()); }
Bar Baz sealed 要 sealed 要 final or non-sealed 要 final or non-sealed MyBaz Bazがnon-sealedであれば 継承できる。sealされない sealed class Hoge permits Foo, Bar {} final class Foo extends Hoge {} sealed class Bar extends Hoge permits Baz {} non-sealed class Baz extends Bar {} class MyBaz extends Baz {}
Point(int x, int y) {} int getLen(Point p) { return switch (p) { case Point(0, int y) -> y; case Point(int x, 0) -> x; case Point(int x, int y) -> (int)sqrt(x * x + y * y); } }
Some[A](x: A) extends Option[A] case object None extends Option[Nothing] def a(opt: Option[String]):String = { opt match { case Some(s) => "message is %s" format s case None => "empty message" } } ※ コードはイメージです 定義 利用