Object

  1. An object is a type of class that can have no more than one instance, known in object-oriented design as a singleton. Instead of creating an instance with a new keyword, just access the object directly by name.
  2. Objects provide similar "static" and "global" functionality but decouple them from instantiable classes.
  3. Objects do not take any parameters (they are automatically instantiated), but you can define the same fields, methods, and internal classes as you can with regular classes. 不需要传入任何参数,因为你可以直接访问object里面的内容

Companion Object

  1. A companion object is an object with the same name as a class or trait and is defined in the same source file as the associated file or trait.
  2. A companion object differs from other objects as it has access rights to the class/trait that other objects do not.
  3. A companion object is as a factory for creating instances of the class.
  4. An analog to a companion object in Java is having a class with static methods. In Scala you would move the static methods to a Companion object.
  5. When a case-class is declared a companion object is created for the case-class with a factory method that has the same signature as the primary constructor of the case class

NOTE: Because the companion object and the class must be defined in the same source file you cannot create them in the interpreter.

Case Class

Case classes differ from regular classes in that they get:

  1. pattern matching support 支持模式匹配
  2. default implementations of equals and hashCode 默认实现equals和hashCode
  3. default implementations of serialization 默认实现序列化
  4. a prettier default implementation of toString, and 默认更好实现toString
  5. the small amount of functionality that they get from automatically inheriting from scala.Product.

NOTE: Pattern matching, equals and hashCode don't matter much for singletons (unless you do something really degenerate), so you're pretty much just getting serialization, a nice toString, and some methods you probably won't ever use.

[Scala] Scala基础知识的更多相关文章

  1. Spark学习之Scala的基础知识

    Scala的变量声明 在Scala创建变量的时候,必须使用val或者var val,变量值不可修改,一旦分配不能重新指向别的值 var,分配后,可重新指向类型相同的值 举例 val lines = s ...

  2. Scala学习笔记--正则表达式基础知识、如何在scala内使用

    正则表达式语法:https://msdn.microsoft.com/zh-cn/library/ae5bf541(VS.80).aspx 基础知识:正则表达式30分钟入门教程 http://www. ...

  3. scala 基础知识总结

    在最开始处引入 log 相关的 包 import org.apache.log4j.{Logger,Level} 在需要屏蔽日志输出的地方加上这两行代码 // 屏蔽不必要的日志显示在终端上 Logge ...

  4. Scala基础知识笔记1

    上一篇文章介绍了如何下载scala, 因为在官网上点击下载按钮无法下载, 下面介绍scala的基础语法: 1 scala和java的关系 Scala是基于jvm的一门编程语言,Scala的代码最终会经 ...

  5. Scala基础知识笔记2

    1 类 1.1 定义一个简单的类 1.2 field的getter 和 setter方法 感觉成员变量定义成  var 属性名=属性值即可,  不需要定义成 val 或者 private就行, // ...

  6. Scala编程基础

    Scala与Java的关系... 4 安装Scala. 4 Scala解释器的使用... 4 声明变量... 5 数据类型与操作符... 5 函数调用与apply()函数... 5 if表达式... ...

  7. Scala 语法基础

    一 简介 Scala 是一门多范式(multi-paradigm)的编程语言,设计初衷是要集成面向对象编程和函数式编程的各种特性.Scala 运行在Java虚拟机上,并兼容现有的Java程序.Scal ...

  8. Scala(一) —— 基础

    一.输出 println("Hello World") 二.变量与常量 1.变量用var表示,常量使用val表示 2.变量类型声明 var variableName : DateT ...

  9. Scala零基础教学【1-20】

    基于王家林老师的Spark教程——共计111讲的<Scala零基础教学> 计划在9月24日内完成(中秋节假期之内) 目前18号初步学习到25讲,平均每天大约完成15讲,望各位监督. 初步计 ...

  10. Scala语言基础

    1. Scala语言的特点 a. 多范式编程语言,面向对象(抽象.封装)和函数式(过程.结果)编程 b. 代码简洁 ==>可读性较差 c. 代码会被变异成Java字节码,运行在JVM上 2. S ...

随机推荐

  1. VS2013问题与解决方法

    问题: Getting Error "'Microsoft.VisualStudio.Editor.Implementation.EditorPackage' package did not ...

  2. 从人类社会的角度看OO(独家视角)

    引言 在OO的工作中,我们一定会涉及到类,抽象类和接口.那么类和抽象类以及接口到底扮演的什么角色? 本文主要是从人类社会的角度阐述类与抽象类以及接口的"社会"关系,从而让我们抛弃书 ...

  3. Neutron 理解 (6): Neutron 是怎么实现虚拟三层网络的 [How Neutron implements virtual L3 network]

    学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...

  4. jquery选择器案例

    一.预期效果 实现一个效果,如下. 品牌列表默认精简显示,单击“显示全部品牌”按钮显示全部品牌,同时列表将推荐的品牌的名字高亮显示,按钮里的文字变成“精简显示品牌”.再次点击“精简显示品牌”回到初始页 ...

  5. AngularJS笔记---注册服务

    在前面的笔记中,了解到AngularJS的后台控制可以在Controller里面实现. 可是如果所有的逻辑代码都写到Controller会显得该Controller过于臃肿. 不方便维护, Angul ...

  6. 一个完整的TCP连接

    当我们向服务器发送HTTP请求,获取数据.修改信息时,都需要建立TCP连接,包括三次握手,四次分手. 什么是TCP连接? 为实现数据的可靠传输,TCP要在应用进程间建立传输连接.它是在两个传输用户之间 ...

  7. microstrip patch antenna

    微带天线有很多优点,低成本,易于集成,加工简单,缺点有:效率低.功率容易低.高Q值 ,可能存在表面波,极化效果差,弱电扫性能.带宽窄.微带线的金属厚度t远远小于波长(t << λ0 ,自由 ...

  8. 嵌入式Linux驱动学习之路(十三)按键驱动-异步通知

    之前的按键方式: 查询: 极度占用CPU资源 中断: 在读的时候产生休眠,在没有信号的时候永远不会返回. poll机制: 在中断的基础上加上超时时间. 异步通知就是通过信号来传送. 首先在应用程序中有 ...

  9. cf Round 613

    A.Peter and Snow Blower(计算几何) 给定一个点和一个多边形,求出这个多边形绕这个点旋转一圈后形成的面积.保证这个点不在多边形内. 画个图能明白 这个图形是一个圆环,那么就是这个 ...

  10. 83 parrted-分区和分区大小的调整

    parted命令是由GNU组织开发的一款功能强大的磁盘分区和分区大小调整工具,与fdisk不同,它支持调整分区的大小.作为一种设计用于Linux的工具,它没有构建成处理与fdisk关联的多种分区类型, ...