嵌套类:

class Human {
class Student{
val age = 10
}
} object ClassDemo {
def main(args: Array[String]): Unit = {
val h = new Human
val s = new h.Student
println(s.age)
}
}

有时会遇到这种情况:

class Human {
class Student {
def addS(s: Student) = {
val ab = new ArrayBuffer[Student]()
ab += s
}
}
} object ClassDemo {
def main(args: Array[String]): Unit = {
val h = new Human
val h2 = new Human
val s = new h.Student
val s2 = new h2.Student
s.addS(s2)
}
}

以上addS会报错,因为方法只能接收h.Student不能接收h2.Student。

解决方法有2个:

1. 类型投影

import scala.collection.mutable.ArrayBuffer

class Human {
class Student {
def addS(s: Human#Student) = {
val ab = new ArrayBuffer[Human#Student]()
ab += s
}
}
} object ClassDemo {
def main(args: Array[String]): Unit = {
val h = new Human
val h2 = new Human
val s = new h.Student
val s2 = new h2.Student
s.addS(s2)
}
}

2. 伴生对象

object Human {
class Student { }
} class Human {
def addS(s: Human.Student) = {
val ab = new ArrayBuffer[Human.Student]()
ab += s
}
} object ClassDemo {
def main(args: Array[String]): Unit = { val h = new Human
val s = new Human.Student
val s2 = new Human.Student h.addS(s)
h.addS(s2) }
}

嵌套类要访问外部类有2种方式:

1. 外部类.this

class Human {
val name = "Sky"
class Student {
println(Human.this.name)
def addS(s: Student) = {
val ab = new ArrayBuffer[Student]()
ab += s
}
}
}

2. “自身类型”

class Human {
outter =>
class Student {
println(outter.name)
def addS(s: Student) = {
val ab = new ArrayBuffer[Student]()
ab += s
}
} val name = "Sky"
}

快学Scala 第八课 (嵌套类)的更多相关文章

  1. 快学Scala 第十一课 (类继承)

    类继承: class People { } class Emp extends People{ } 和Java一样,final的类不能被继承.final的字段和方法不能被override. 在Scal ...

  2. 快学Scala 第七课 (类构造函数)

    类 主构造器: class Person (var name: String){ } 主构造参数可以不带val或者var,如果没有被其他方法使用,则不保存为字段. 如果被其他方法使用,则被升格为字段, ...

  3. 快学Scala 第六课 (类getter和setter)

    类getter和setter 如果字段定义是private[this], 字段是私有的,但不生成getter和setter方法. class Counter { private[this] var v ...

  4. 快学Scala 第十课 (包和包对象)

    Scala包定义: 嵌套式: package a1 { class a1Class{ val age = 10 } package a2 { class PackageTest { def main( ...

  5. 快学Scala 第十三课 (类型层级,对象相等性)

    Scala 类型层级: 对象相等性: 和Java一样要重写equals方法和hashcode方法 class Student(val id: Int, val name: String) { over ...

  6. 快学Scala 第五课 (构造映射,获取映射值,更新映射值,迭代映射,与Java互操作)

    构造映射: val score = Map[String, Int]() val score1 = HashMap[String, Int]() val value1 = Map[String, In ...

  7. 快学Scala 第四课 (多维数组,与Java集合的互操作)

    Scala二维数组的定义: val arr2 = Array.ofDim[String](2, 2) arr2(0)(0) = "aa" arr2(1)(0) = "bb ...

  8. 快学Scala 第三课 (定长数组,变长数组, 数组循环, 数组转换, 数组常用操作)

    定长数组定义: val ar = new Array[Int](10) val arr = Array("aa", "bb") 定长数组赋值: arr(0) = ...

  9. 快学Scala 第二十一课 (初始化trait的抽象字段)

    初始化trait的抽象字段: trait Logged { println("Logged constructor") def log(msg: String){ println( ...

随机推荐

  1. Codeforces Round #506 (Div. 3) 1029 F. Multicolored Markers

    CF-1029F 题意: a,b个小正方形构造一个矩形,大小为(a+b),并且要求其中要么a个小正方形是矩形,要么b个小正方形是矩形. 思路: 之前在想要分a,b是否为奇数讨论,后来发现根本不需要.只 ...

  2. codeforces 805 D. Minimum number of steps(数学)

    题目链接:http://codeforces.com/contest/805/problem/D 题意:只有一个操作就是将ab变成bba直到不能变为止,问最少边几次. 题解:这题可以多列几组来找规律, ...

  3. php preg_match pcre回溯绕过

    原理需要知识:正则NFA回溯原理,php的pcre.backtrack_limit设置. 正则NFA回溯原理正则表达式是一个可以被"有限状态自动机"接受的语言类."有限状 ...

  4. Java面试-List中的sort详细解读

    最近看了一些排序相关的文章,因此比较好奇,Java中的排序是如何做的.本片文章介绍的是JDK1.8,List中的sort方法. 先来看看List中的sort是怎么写的: @SuppressWarnin ...

  5. Marrkdown基础用法

    目录 前言 markdown简介 用法列表 标题 字符效果和横线 引用 锚点与链接 代码高亮 图片 有序列表&无序列表 表格 特殊符号与颜色处理 markdown进阶技巧 参考文章 前言 因为 ...

  6. Apache JMeter (二)性能测试 入门实例

    上一节我们说了关于Jmeter环境的配置,接下来讲一个测试的实例. 1.运行Jmeter 进入Jmeter程序所在目录,运行"bin/jmeter.bat" Jmeter支持中文, ...

  7. SqlServer 2014 Enterprise 企业版安装程序下载与安装教程

    场景 SqlServer2014 企业版比标准版 多一些功能,比如在企业版中能使用分区函数,但是在标准版Express 中就不能使用. SqlServer 2014 企业版安装包下载: 关注公众号: ...

  8. 网关高可用之keepavlived全流程(安装/配置/验证/解析)

    1.场景描述 因为要做网关的高可用,用到了keepalived+nginx,来保证nginx的高可用.(微服务时代之网关及注册中心高可用架构设计),如下图: 安装了keepavlived,走了一些弯路 ...

  9. Vscode for python ide配置

    1.文件头添加 自定义代码片段 文件>首选项>用户代码片段 搜索python 添加代码 "HEADER":{ "prefix": "hea ...

  10. Python集训营45天—Day07 (面向对象编程进阶)

    目录 1. @property装饰器 2. 魔法方法 3. 类属性和实例属性 4.静态方法和类方法 5. 单继承和多继承 6. 多态 7. del 方法 序言:上个章节我们了解了面向对象的基础知识,这 ...