当前位置:网站首页>Scala object class basic grammar explanation
Scala object class basic grammar explanation
2022-08-07 03:02:00 【Learning Liang Programming Notes】
学习scala
https://docs.scala-lang.org/zh-cn/tour/tour-of-scala.html
- main,class,object
The main method can only be written in objectdefined file
object和class啥区别:
Review a question:java中有一个知识点 静态
分号可有可无
scalais a package-level distinction,The class name can be inconsistent with the file name
- scalaThe filename and classname can be different
- You can write more than one in a fileobject

- bare code andmainThe order of execution of the function code:

- 使用s来拼接字符串

- class和object的执行顺序:

- scala是一门真正的面向对象的编程语言

- option类型:getOrElse,优化null返回值的if判断,降低cpu开销
源码
package com.zxl.bigdata.scala
//约等于 static 单例对象
//static
//单例 new scalaThe compiler is very user-friendly Let you people write a lot less code
object ooxx {
// private val xo:xxoo = new xxoo()
private val xo = new ooxx(11)
// Integer num = 0;
// var/val var:变量 val常量 取代了final
/* var a=3 a=4 val b=4 b=5*/
private val name = "object:zhangsan"
println("ooxx....up")
def main(args: Array[String]): Unit = {
println("hello from ooxx")
xo.printMsg()
}
println("ooxx....down")
}
//类里,Exposed code is default constructed.有默认构造
//Personalized construction!!
//The parameters in the class name constructor are the member properties of the class,且默认是val类型,且默认是private
//Only parameters in the classname construct can be set tovar,Parameters in other method functions are allval类型的,and is not allowed to be set tovar类型
class ooxx(sex: String) {
var name = "class:zhangsan"
def this(xname: Int) {
//The default constructor must be invoked
this("abc")
}
var a: Int = 3
// private val value = new ooxx()
println(s"ooxx....up$a....")
def printMsg(): Unit = {
println(s"sex: ${
ooxx.name}")
}
println(s"ooxx....up${
a + 4}")
}
边栏推荐
猜你喜欢
随机推荐
haproxy实验
X base subtraction
Auto.js实现视频号点赞自动化
coco数据集解析及读取方法
laravel 读写分离&跨库查询(链接双库)
Encapsulation Encapsulation
BI技巧丨筛选重置
ansible Group 模块
KingbaseES 的闪回查询
ansible HostName 模块
83-MongoDB介绍
The process of kafka-flink-mysql
启航C语言,第一个Hello World
苹果所有设备参数大全
Coco data set analysis and reading method
德迅云安全提供国内卓越、快速、稳定的高防产品
接口项目实战
什么是传输网、核心网、承载网、接入网?
Lynk & Co 09phev is fully equipped with active and passive safety features, and it can accelerate from 100 km to 5.6s
LVS+Keepalived









