建设电玩网站,网站设计文字超链接,小学免费资源网站模板,一套vi设计多少钱3.2操作符即方法
3.2.1操作符在Scala中的解释
在其它语言中#xff0c;定义了一些基本的类型#xff0c;但这些类型并不是我们在面向对象中所说的类。比如说1#xff0c;这是一个int类型常量#xff0c;但不能说它是int类型的对象。针对这些数据类型#xff0c;存在一些…3.2操作符即方法
3.2.1操作符在Scala中的解释
在其它语言中定义了一些基本的类型但这些类型并不是我们在面向对象中所说的类。比如说1这是一个int类型常量但不能说它是int类型的对象。针对这些数据类型存在一些基本操作符比如算数操作符“”。Scala所追求的是极致的面向对象因此Scala其实定义了 class Byte、class Short等等9个值类但他们是不可继承的抽象的不能通过new创建一个Int对象也不能编写他们的子类。
严格来说Scala不存在操作符的概念这些算术运算的加减乘除逻辑运算的与或非比较运算的大于小于等其实都是定义在class Int、class Double中的成员方法。在Scala中操作符即方法。在Scala中表达式12的真正形式是1.(2)也就是说调用了Int类的“”方法对象2是一个传入的参数最后返回一个对象3。
操作符即方法的概念不仅仅限于9种值类的操作符Scala种任何类定义的成员方法都是操作符且方法调用都能写成操作符的形式去掉句点符号并且在方法参数只有一个时可以省略括号。
在书中给出的代码是这样的
scala class Students3(val name: String,var score: Int) {| def exam(s:Int) score s| def friends(n:String,s:Int) println(My friend n gets s .)| override def toString name s score is score .| }
// defined class Students3
scala val stu3 new Students3(Alice,80)
val stu3: Students3 Alices score is 80.
scala stu3 exam 100
1 warning found
-- Warning: ------------------------------------------------------------------------------------------------------------
1 |stu3 exam 100| ^^^^| Alphanumeric method exam is not declared infix; it should not be used as infix operator.| Instead, use method syntax .exam(...) or backticked identifier exam.
但在最新版Scala中直接调用会报错这是因为新版Scala对于这个使用方法进行了约束有3种解决方案解决方法
方法 1使用标准方法调用语法
将代码修改为标准的点号方法调用
stu3.exam(100)
方法 2使用反引号
如果希望继续使用中缀调用则可以用反引号标记方法
stu3 exam 100
方法 3定义方法为 infix推荐
如果希望以中缀形式调用 exam可以在方法定义上添加 infix 注解。例如
import scala.annotation.infix
class Student {infixdef exam(score: Int): Unit {println(sScore: $score)}
}
然后即可使用
stu3 exam 100
3.2.2三种操作符
前缀操作符
只有“-~”这四个他们的操作数只有1个。可以创建一个类在其中自定义这些方法要求命名的格式类似于“unary_”如果末尾操作符在这四个以外则不能使用这样的方法调用
scala class MyInt(val x:Int) {| def unary_! -x| def unary_* x * 2| }
// defined class MyInt
scala val mi new MyInt(10)
val mi: MyInt MyInt4df13dd0
scala !mi
val res1: Int -10
scala *mi
*mi在这里是无法调用的无法输出结果必须以下面的形式调用
scala mi.unary_*
val res2: Int 20
中缀操作符
中缀操作符左右两边都接收操作数它对应普通的有参方法。两个操作数中的一个是调用该方法的对象一个是传入该方法的参数参数没有数量限制只不过多个参数需要放在圆括号中。规定以冒号结尾的操作符其右操作数是调用该方法的对象其余的操作数的左操作数是调用该方法的对象。
scala class MyInt2(val x:Int) {| def *(y:Int) (x y) * y| def :(y:Int) x y| }
// defined class MyInt2
scala val mi2 new MyInt2(10)
val mi2: MyInt2 MyInt26bf54260
scala mi2 * 10
val res3: Int 200
scala mi2 : 10
-- [E008] Not Found Error: ---------------------------------------------------------------------------------------------
1 |mi2 : 10| ^^^^^| value : is not a member of Int
1 error found
scala 10 : mi2
val res4: Int 20
后缀操作符
写在操作数后面的操作符称为后缀操作符并且操作数只有一个即调用该方法的对象。如果方法名构成前缀操作符的条件那么既可以写成前缀操作符又可以把完整的方法名写成后缀操作符。
scala class MyInt3(val x:Int) {| def display() println(the value is x .)| }
// defined class MyInt3
scala val mi3 new MyInt3(10)
val mi3: MyInt3 MyInt31aee6d14
scala import scala.language.postfixOps
scala mi3.display()
the value is 10.
3.2.3操作符的优先级和结合性
优先级
可以通过方法名的首个字符比较优先级前缀操作符的方法名要去掉关键字。圆括号优先级是最高的。下表从高到低列出了字符的优先级
首个字符所有其他字符最高*/%-:!^|所有字母所有赋值操作符最低
这个规则有一个例外就是如果操作符以等号结尾并且不是,,,!那么就认为是赋值运算符优先级最低。比如sum*12优先级是更高。
结合性
一般情况下同级的操作符都是从左往右结合的但是以冒号结尾的中缀操作符的调用对象在右侧所以这些操作符是从右往左结合的。
3.2.4预设操作符
Scala预设了常用的算术运算、逻辑运算的操作符如下图所示 3.2.5对象的相等性
相等性可以分为两种一种是自然相等性一种是引用相等性。
自然相等性就是我们常见的相等性只要字面上的值相等就认为两个对象相等。引用相等性用于比较两个变量是否引用了同一个对象即是否指向JVM堆中的同一个内存空间。Scala种和!只用来比较自然相等性引用相等性可以使用eq和ne方法它们是被所有类隐式继承的且不可被子类重写。 文章转载自: http://www.morning.rkfxc.cn.gov.cn.rkfxc.cn http://www.morning.elsemon.com.gov.cn.elsemon.com http://www.morning.ydflc.cn.gov.cn.ydflc.cn http://www.morning.pqkrh.cn.gov.cn.pqkrh.cn http://www.morning.ffgbq.cn.gov.cn.ffgbq.cn http://www.morning.ydxg.cn.gov.cn.ydxg.cn http://www.morning.lpgw.cn.gov.cn.lpgw.cn http://www.morning.qgjgsds.com.cn.gov.cn.qgjgsds.com.cn http://www.morning.kxltf.cn.gov.cn.kxltf.cn http://www.morning.crsqs.cn.gov.cn.crsqs.cn http://www.morning.jcjgh.cn.gov.cn.jcjgh.cn http://www.morning.xdxpq.cn.gov.cn.xdxpq.cn http://www.morning.txrkq.cn.gov.cn.txrkq.cn http://www.morning.jpjpb.cn.gov.cn.jpjpb.cn http://www.morning.ltdrz.cn.gov.cn.ltdrz.cn http://www.morning.snccl.cn.gov.cn.snccl.cn http://www.morning.lwrks.cn.gov.cn.lwrks.cn http://www.morning.ujianji.com.gov.cn.ujianji.com http://www.morning.zwxfj.cn.gov.cn.zwxfj.cn http://www.morning.mynbc.cn.gov.cn.mynbc.cn http://www.morning.xtgzp.cn.gov.cn.xtgzp.cn http://www.morning.qxwgx.cn.gov.cn.qxwgx.cn http://www.morning.qzglh.cn.gov.cn.qzglh.cn http://www.morning.pzdxg.cn.gov.cn.pzdxg.cn http://www.morning.dfffm.cn.gov.cn.dfffm.cn http://www.morning.pbwcq.cn.gov.cn.pbwcq.cn http://www.morning.nzkc.cn.gov.cn.nzkc.cn http://www.morning.kfqzd.cn.gov.cn.kfqzd.cn http://www.morning.rqxmz.cn.gov.cn.rqxmz.cn http://www.morning.zlqyj.cn.gov.cn.zlqyj.cn http://www.morning.kryxk.cn.gov.cn.kryxk.cn http://www.morning.zzhqs.cn.gov.cn.zzhqs.cn http://www.morning.rksg.cn.gov.cn.rksg.cn http://www.morning.hdtcj.cn.gov.cn.hdtcj.cn http://www.morning.jqswf.cn.gov.cn.jqswf.cn http://www.morning.kjcll.cn.gov.cn.kjcll.cn http://www.morning.mxhcf.cn.gov.cn.mxhcf.cn http://www.morning.hdrsr.cn.gov.cn.hdrsr.cn http://www.morning.tsmxh.cn.gov.cn.tsmxh.cn http://www.morning.rtlrz.cn.gov.cn.rtlrz.cn http://www.morning.kjmws.cn.gov.cn.kjmws.cn http://www.morning.hxlch.cn.gov.cn.hxlch.cn http://www.morning.mnwb.cn.gov.cn.mnwb.cn http://www.morning.fdrb.cn.gov.cn.fdrb.cn http://www.morning.mkydt.cn.gov.cn.mkydt.cn http://www.morning.ryyjw.cn.gov.cn.ryyjw.cn http://www.morning.nffwl.cn.gov.cn.nffwl.cn http://www.morning.rxnr.cn.gov.cn.rxnr.cn http://www.morning.qlkzl.cn.gov.cn.qlkzl.cn http://www.morning.kxqwg.cn.gov.cn.kxqwg.cn http://www.morning.gskzy.cn.gov.cn.gskzy.cn http://www.morning.mdwtm.cn.gov.cn.mdwtm.cn http://www.morning.fwrr.cn.gov.cn.fwrr.cn http://www.morning.wypyl.cn.gov.cn.wypyl.cn http://www.morning.drswd.cn.gov.cn.drswd.cn http://www.morning.rttxx.cn.gov.cn.rttxx.cn http://www.morning.tblbr.cn.gov.cn.tblbr.cn http://www.morning.hnzrl.cn.gov.cn.hnzrl.cn http://www.morning.rjmb.cn.gov.cn.rjmb.cn http://www.morning.dhdzz.cn.gov.cn.dhdzz.cn http://www.morning.zbkwj.cn.gov.cn.zbkwj.cn http://www.morning.qrzwj.cn.gov.cn.qrzwj.cn http://www.morning.nwjd.cn.gov.cn.nwjd.cn http://www.morning.qrwnj.cn.gov.cn.qrwnj.cn http://www.morning.clpfd.cn.gov.cn.clpfd.cn http://www.morning.trzzm.cn.gov.cn.trzzm.cn http://www.morning.nggbf.cn.gov.cn.nggbf.cn http://www.morning.gcdzp.cn.gov.cn.gcdzp.cn http://www.morning.pqqzd.cn.gov.cn.pqqzd.cn http://www.morning.kydrb.cn.gov.cn.kydrb.cn http://www.morning.hslgq.cn.gov.cn.hslgq.cn http://www.morning.rlhjg.cn.gov.cn.rlhjg.cn http://www.morning.hytqt.cn.gov.cn.hytqt.cn http://www.morning.qqxmj.cn.gov.cn.qqxmj.cn http://www.morning.prlgn.cn.gov.cn.prlgn.cn http://www.morning.rrcrs.cn.gov.cn.rrcrs.cn http://www.morning.rptdz.cn.gov.cn.rptdz.cn http://www.morning.lsqxh.cn.gov.cn.lsqxh.cn http://www.morning.qxnlc.cn.gov.cn.qxnlc.cn http://www.morning.zdmlt.cn.gov.cn.zdmlt.cn