当前位置:网站首页>Scala学习:类和对象
Scala学习:类和对象
2022-07-30 18:45:00 【我爱夜来香A】
一、类
class ChecksumAccumulator {
private var sum = 0
def add(b:Byte):Unit = {
sum += b }
def checksum():Int = ~(sum & 0xFF) + 1
}
- Scala中类、方法、成员变量都是默认public级别,不用声明
- Scala中分号是可选的
- Scala返回值类型是可选的,如上述eg中Unit和Int可以不用声明,Scala会做类型推断
二、单例对象
package Test
import scala.collection.mutable
class ChecksumAccumulator {
private var sum = 0
def add(b:Byte):Unit = {
sum += b }
def checksum():Int = ~(sum & 0xFF) + 1
}
object ChecksumAccumulator{
private val cache = mutable.Map.empty[String,Int]
def calculate(s:String):Int = {
if (cache.contains(s))
cache(s)
else{
val acc = new ChecksumAccumulator
for (c <- s)
acc.add(c.toByte)
val cs = acc.checksum()
cache += (s -> cs)
cs
}
}
}
- Scala比Java更面向对象,其中一点就是Scala中不允许有静态成员
- 但Scala提供了另一种形式-单例对象,就是上图中的object{}部分,当单例对象和某个类共用一个名字时,它被称为这个类的伴生对象,类也被称为单例对象的伴生类。必须在同一个源码文件中定义类和类的伴生对象
- 类和伴生对象可以互相访问对方的私有成员
- 没有同名的伴生类的单例对象被称为孤立对象,通常定义main方法,作为应用程序的入口
- 此外,Java和Scala的一个重大区别是:Java要求将公共的类放入跟类同名的文件中,如若上述代码为Java代码,则文件名必须为ChecksumAccumulator.java,但Scala可以随意命名文件名,不过最好文件和类同名
三、应用程序入口
import Test.ChecksumAccumulator.calculate
object SumTest {
def main(args:Array[String]) = {
val str = "lishiming"
println(calculate(str))
}
}
- Scala同Java一样,应用程序的入口都在main方法中,scala的main方法定义在单例对象中
import Test.ChecksumAccumulator.calculate
object SumTest extends App {
val str = "lishiming"
println(calculate(str))
}
- 如上所示,scala提供了另一种应用程序的入口,不用编写main方法,只需在单例对象后extends
App,然后把打算放在main方法中的代码直接写在单例对象后的花括号中即可
边栏推荐
- ctf.show_web5
- Network Basics (3) 01-Basic Concepts of Networks - Protocols, Host Addresses, Paths and Parameters of URL Addresses & 127.0.0.1 Local Loopback Address & View URL IP Address and Access Ping Space + URL
- ROS 节点初始化步骤、topic/service创建及使用
- Web结题报告
- LeetCode Exercise - Two Questions About Finding Sum of Array Elements
- The large-scale application of artificial intelligence AI products in industrial-grade mature shipping ports of CIMC World Lianda will create a new generation of high-efficiency smart ports and innova
- Chapter 4 Controlling the Execution Flow
- 荐号 | 对你有恩的人,不要请吃饭来报答
- MYSQL (Basic) - An article takes you into the wonderful world of MYSQL
- 【Prometheus】Prometheus联邦的一次优化记录[续]
猜你喜欢
natural language processing nltk
432.4 FPS 快STDC 2.84倍 | LPS-Net 结合内存、FLOPs、CUDA实现超快语义分割模型
Immersive experience iFLYTEK 2022 Consumer Expo "Official Designated Product"
6块钱1斤,日本公司为何来中国收烟头?
Mysql执行原理剖析
LeetCode Exercise - Two Questions About Finding Sum of Array Elements
深化校企合作 搭建技术技能人才成长“立交桥”
The large-scale application of artificial intelligence AI products in industrial-grade mature shipping ports of CIMC World Lianda will create a new generation of high-efficiency smart ports and innova
中集世联达飞瞳全球工业人工智能AI领军者,全球顶尖AI核心技术高泛化性高鲁棒性稀疏样本持续学习,工业级高性能成熟AI产品规模应用
微博广告分布式配置中心的构建与实践(有彩蛋)
随机推荐
ESP8266-Arduino programming example-BMP180 air pressure temperature sensor driver
第4章 控制执行流程
CCNA-ACL(访问控制列表)标准ACL 扩展ACL 命名ACL
WEBSOCKETPP使用简介+demo
【每日一道LeetCode】——191. 位1的个数
MYSQL(基本篇)——一篇文章带你走进MYSQL的奇妙世界
[Use of Qt Designer tool]
第十六期八股文巴拉巴拉说(MQ篇)
6块钱1斤,日本公司为何来中国收烟头?
ctf.show_web5
【剑指 Offer】剑指 Offer 22. 链表中倒数第k个节点
Swiper rotates pictures and plays background music
Quickly build an e-commerce platform based on Amazon cloud technology serverless service - performance
深化校企合作 搭建技术技能人才成长“立交桥”
natural language processing nltk
Redis for infrastructure
生物医学论文有何价值 论文中译英怎样翻译效果好
【Prometheus】Prometheus联邦的一次优化记录[续]
Recommended Books | Recommend 3 database books with rave reviews
Mysql执行原理剖析