scala 碎片内容
Posted hotmemo
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了scala 碎片内容相关的知识,希望对你有一定的参考价值。
object CaseClassDemo{
def main(args: Array[String]): Unit = {
//定义样例类
//默认带有apply方法
//构造函数的参数默认是public val修饰的
case class Message(var sender: String, recipient:String, body:String)
//创建一个样例类的对象
val message1=Message("jerry","tom","hello")
// message1.sender="kate"
println(message1.sender+" ,"+message1.recipient+" ,"+message1.body)
//样例类的比较,基于值或者结构比较,而不是基于引用比较
val message2=Message("jerry","tom","hello")
println(message1==message2)
//样例类的拷贝
val message3=message1.copy()
println(message1==message3)
println(message3.sender+" ,"+message3.recipient+" ,"+message3.body)
//不完全拷贝,对部分参数赋值
val message4=message1.copy(sender = "hanmeimei")
println(message4.sender+" ,"+message4.recipient+" ,"+message4.body)
}
package com.utilcopy
class ConstructorDemo private (var b:Int) {
var a:Int=0
println("Constructor Study")
//定义辅助构造函数
//这个是public
def this(a1:Int,b1:Int)={
//首先需要调用主构造函数或者其他的辅助函数
this(b1);
this.a=a1;
}
}
//定义一个带有参数的主构造函数
//类自带有无参的构造函数
//主构造函数执行类中定义的所有的语句
//主构造函数和辅助构造函数
object ConstructorDemoTest{
def main(args: Array[String]): Unit = {
// val obj:ConstructorDemo=new ConstructorDemo()
val obj:ConstructorDemo=new ConstructorDemo(10000,10000)
println(obj.a)
}
}
package com.utilcopy
class Methods {
//def 方法名(参数列表):返回类型=方法体
//返回类型可以省,返回类型是递归方法的时候,一定要指明,其他情况,可以省
def add1(x:Int,y:Int)=x+y;
def add2(x:Int,y:Int):Int=x+y;
add1(1,2);
add2(x=1,y=2);
add2(y=2,x=1);
def add3(x:Int,y:Int)={
x+y;
println(x+y);
}
//带有参数类表的方法
def addAndMultiply1(x:Int,y:Int,z:Int)=(x+y)*z
def addAndMultiply2(x:Int,y:Int)(z:Int)=(x+y)*z
def addAndMultiply3(x:Int)(y:Int)(z:Int)=(x+y)*z
addAndMultiply3(3)(4)(5);
//无参方法,没带小括号,调用的时候不能带小括号,如果带了小括号,调用的时候可以不带小括号
def printInfo=println("I love scala");
printInfo
def printInfo1()=printInfo
printInfo1
//带有默认值参数的方法
def printInfo2(str:String="Hello Scala")=println(str)
printInfo2()
def add(a:Int=1,b:Int,c:Int=3)=println("a="+a+", b="+b+", c="+c);
add(1,2)
add(b=2)
add(1,2,3)
//可变长参数的方法
def add(a:Int*)={
for(i <- a){
println(i)
}
}
}
package com.utilcopy
class PersonS {
//val修饰的属性,系统自动生成get方法
//private final
val id:String="1234"
def getId():String={
println("hahaha")
this.id
}
//var修饰的属性,系统生成get和set方法
//private
var name:String=""
//private var修饰的属性,系统生成private修饰的get和set方法
//相当与类的私有字段
private var gender:Int=0
//private[this]系统不会生成get和set方法
//只有当前对象可以访问该属性
private[this] var age:Int=0
// def compare(obj:PersonS):Int={
// this.age-obj.age
// }
}
object test{
def main(args: Array[String]): Unit = {
var per:PersonS=new PersonS()
println(per.getId())
per.name="张三"
println(per.name)
}
}
package com.utilcopy
//定义一个特质,带有抽象方法的特质
trait Iterator[A]{
def hasNext:Boolean
def next():A
}
//定义一个带有实现的特质
trait ConsoleLogger{
def log(msg:String):Unit={
println(msg)
}
}
//定义一个类,实现特质
class IntIterator(to:Int) extends Iterator[Int] with ConsoleLogger {
private var current=0;
override def hasNext: Boolean =current < to;
override def next(): Int = {
if(hasNext){
log("has next")
val t=current;
current+=1;
t
}else{
0
}
}
}
object TraitTest {
def main(args: Array[String]): Unit = {
val iterator=new IntIterator(to=10)
println(iterator.next())
println(iterator.next())
println(iterator.next())
println(iterator.next())
println(iterator.next())
}
}
package com.utilcopy
trait Logger{
def log(msg:String)
}
//子特质实现父特质里的抽象方法
trait ConsoleLogger extends Logger{
override def log(msg: String): Unit = println(msg)
}
//给日志加上时间戳
trait TimestampLogger extends ConsoleLogger{
override def log(msg: String): Unit = super.log(msg+s"${java.time.Instant.now()}$msg")
}
//如果日志过长,对日志进行阶段显示
trait ShortterLoger extends ConsoleLogger{
val maxLength=15
override def log(msg: String): Unit = super.log(
if(msg.length<=maxLength) msg
else s"${msg.substring(0,maxLength-3)}..."
)
}
class Account{
protected var balance:Double=0.0
}
class SavingAccount extends Account with ConsoleLogger {
def withdraw(amount:Double):Unit={
if(amount>balance){
log(msg="Insufficient funds")
}else{
balance=balance-amount
}
}
}
//特质可以为类提供可以堆叠的改变
object TraitTest2 extends App {
// var acc1=new SavingAccount with ConsoleLogger with TimestampLogger with ShortterLoger
// var acc2=new SavingAccount with ConsoleLogger with ShortterLoger with TimestampLogger
var acc1=new SavingAccount with ConsoleLogger
acc1.withdraw(amount=100.0)
var acc2=new SavingAccount with ConsoleLogger with TimestampLogger
acc2.withdraw(100.0)
var acc3=new SavingAccount with ConsoleLogger with ShortterLoger
acc3.withdraw(100.0)
// acc1.withdraw(100.0)
// acc2.withdraw(100.0)
}
package com.utilcopy
trait Logger{
def log(msg:String)
def info(msg:String){
log(msg="Info:"+msg)
}
def service(msg:String): Unit ={
log(msg="service:"+msg)
}
def warn(msg:String): Unit ={
log(msg="warn:"+msg)
}
}
class Account{
protected var balance:Double=0.0
}
class SavingAccount extends Account with Logger {
override def log(msg: String): Unit = println(msg)
def withdraw(amount:Double): Unit ={
if(amount>balance) service(msg="Insufficent funds") else{
balance=balance-amount
info(s"you withdraw $amount")
}
}
}
object TraitTest3 extends App {
val acc=new SavingAccount
acc.withdraw(100.0)
}
以上是关于scala 碎片内容的主要内容,如果未能解决你的问题,请参考以下文章