内部类
| package base |
| |
| |
| object IClass extends App { |
| |
| |
| val ic1 = new OuterClass().InnerObject |
| println("y = " + ic1.y) |
| |
| |
| val oc = new OuterClass() |
| |
| val ic2 = new oc.InnerClass() |
| println("x = " + ic2.x) |
| |
| |
| val ic3 = new OuterClass().getInnerClass |
| println("x = " + ic3.x) |
| |
| |
| val oic = new OuterObject.InnerClass() |
| println("m = " + oic.m) |
| |
| |
| println("n = " + OuterObject.InnerObject.n) |
| |
| } |
| |
| |
| class OuterClass { |
| |
| |
| def getInnerClass: InnerClass = { |
| new InnerClass() |
| } |
| |
| |
| class InnerClass { |
| var x = 1 |
| } |
| |
| |
| object InnerObject { |
| val y = 2 |
| } |
| |
| } |
| |
| |
| object OuterObject { |
| |
| |
| class InnerClass { |
| var m = 3 |
| } |
| |
| |
| object InnerObject { |
| var n = 4 |
| } |
| |
| } |
匿名内部类
| package base |
| |
| object RunC { |
| def main(args: Array[String]): Unit = { |
| |
| |
| val abs = new AbsC { |
| override def fun(): String = { |
| "Abs class fun!" |
| } |
| } |
| println(abs.fun()) |
| |
| } |
| } |
| |
| |
| abstract class AbsC { |
| |
| def fun():String |
| } |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步