首页 > 技术文章 > Scala路径依赖【内部类】

yszd 2018-12-25 14:28 原文

 1 package big.data.analyse.scala.path_dependence
 2 
 3 /**
 4   * 路径依赖
 5   * Created by zhen on 2018/12/24.
 6   */
 7 // 定义外部类,内部类
 8 class Outer(val width: Int, val height : Int){
 9   //private val width = 12
10   //private val height = 6
11 
12   class Inner{
13     private val space = width * height
14 
15     def show(): Unit ={
16       println(" the space is equal : " + space)
17     }
18   }
19 }
20 object PathDependence {
21   def main(args: Array[String]) {
22     val outer = new Outer(6, 8)
23     val inner = new outer.Inner
24     inner.show()
25     // 特定类型的内部类
26     val inner_belong_outer : outer.Inner = new outer.Inner
27     inner_belong_outer.show()
28 
29     val another_other = new Outer(3, 4)
30     //val another_inner : outer.Inner = new another_other.Inner // Inner doesn't conform
31     val use_inner : Outer#Inner = new another_other.Inner // # 表示Inner是Outer的内部类
32     use_inner.show()
33 
34   }
35 }

结果:

 

推荐阅读