[Scala] Scala基础知识
Object
- 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. - Objects provide similar "static" and "global" functionality but decouple them from instantiable classes.
- 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
- 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.
- A companion object differs from other objects as it has access rights to the class/trait that other objects do not.
- A companion object is as a factory for creating instances of the class.
- 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.
- 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:
- pattern matching support 支持模式匹配
- default implementations of
equals
andhashCode 默认实现equals和hashCode
- default implementations of serialization 默认实现序列化
- a prettier default implementation of
toString
, and 默认更好实现toString - 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基础知识的更多相关文章
- Spark学习之Scala的基础知识
Scala的变量声明 在Scala创建变量的时候,必须使用val或者var val,变量值不可修改,一旦分配不能重新指向别的值 var,分配后,可重新指向类型相同的值 举例 val lines = s ...
- Scala学习笔记--正则表达式基础知识、如何在scala内使用
正则表达式语法:https://msdn.microsoft.com/zh-cn/library/ae5bf541(VS.80).aspx 基础知识:正则表达式30分钟入门教程 http://www. ...
- scala 基础知识总结
在最开始处引入 log 相关的 包 import org.apache.log4j.{Logger,Level} 在需要屏蔽日志输出的地方加上这两行代码 // 屏蔽不必要的日志显示在终端上 Logge ...
- Scala基础知识笔记1
上一篇文章介绍了如何下载scala, 因为在官网上点击下载按钮无法下载, 下面介绍scala的基础语法: 1 scala和java的关系 Scala是基于jvm的一门编程语言,Scala的代码最终会经 ...
- Scala基础知识笔记2
1 类 1.1 定义一个简单的类 1.2 field的getter 和 setter方法 感觉成员变量定义成 var 属性名=属性值即可, 不需要定义成 val 或者 private就行, // ...
- Scala编程基础
Scala与Java的关系... 4 安装Scala. 4 Scala解释器的使用... 4 声明变量... 5 数据类型与操作符... 5 函数调用与apply()函数... 5 if表达式... ...
- Scala 语法基础
一 简介 Scala 是一门多范式(multi-paradigm)的编程语言,设计初衷是要集成面向对象编程和函数式编程的各种特性.Scala 运行在Java虚拟机上,并兼容现有的Java程序.Scal ...
- Scala(一) —— 基础
一.输出 println("Hello World") 二.变量与常量 1.变量用var表示,常量使用val表示 2.变量类型声明 var variableName : DateT ...
- Scala零基础教学【1-20】
基于王家林老师的Spark教程——共计111讲的<Scala零基础教学> 计划在9月24日内完成(中秋节假期之内) 目前18号初步学习到25讲,平均每天大约完成15讲,望各位监督. 初步计 ...
- Scala语言基础
1. Scala语言的特点 a. 多范式编程语言,面向对象(抽象.封装)和函数式(过程.结果)编程 b. 代码简洁 ==>可读性较差 c. 代码会被变异成Java字节码,运行在JVM上 2. S ...
随机推荐
- MySQL server has gone away报错原因分析/
在平时和开发的交流 以及 在论坛回答问题的或称中会发现这个问题被问及的频率非常高. 程序中报错: MySQL server has gone away 是什么意思? 如何避免? 因此,感觉有必要总结一 ...
- [django]l利用xlrd实现xls文件导入数据
代码: #coding:utf-8 import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "www.sett ...
- JSP连接数据库,报Unable to compile class for JSP
先看一下报错原因: HTTP Status 500 - Unable to compile class for JSP: type Exception report message Unable to ...
- spark dataframe unionall
今天本来想写一个spark dataframe unionall的demo,由于粗心报下面错误: Exception in thread "main" org.apache.spa ...
- 固态硬盘与机械硬盘 SQL Server 单表插入性能对比测试
测试环境
- 第3章 Linux常用命令(1)_文件处理命令
1. 文件处理命令 1.1 命令格式:命令 [-option] [arguments],如ls –la /etc (1)个别命令使用不遵循此格式 (2)当有多个选项时,可以写在一起,如以上的-la ( ...
- 3942: [Usaco2015 Feb]Censoring [KMP]
3942: [Usaco2015 Feb]Censoring Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 375 Solved: 206[Subm ...
- Linux 进程间通讯详解六
ftok()函数 key_t ftok(const char *pathname, int proj_id); --功能:创建系统建立IPC通讯 (消息队列.信号量和共享内存) 时key值 --参数 ...
- ZooKeeper 笔记(5) ACL(Access Control List)访问控制列表
zk做为分布式架构中的重要中间件,通常会在上面以节点的方式存储一些关键信息,默认情况下,所有应用都可以读写任何节点,在复杂的应用中,这不太安全,ZK通过ACL机制来解决访问权限问题,详见官网文档:ht ...
- Android 高清加载巨图方案 拒绝压缩图片
Android 高清加载巨图方案 拒绝压缩图片 转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/49300989: 本文出自:[张 ...