请选择 进入手机版 | 继续访问电脑版
楼主: zli08
10430 55

Beginning Scala 2nd Edition [推广有奖]

  1. Listing 3-10. Call by name
  2. def nano() = {
  3. println("Gettingnano")
  4. System.nanoTime
  5. }
复制代码

已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

使用道具

  1. Listing 3-11. Call by name parameter
  2. def delayed(t:=> Long) = {
  3. println("Indelayed method")
  4. println("Param:"+t)
  5. t
  6. }
  7. Let’s see what happens when we call delayed with nano as a parameter:
  8. scala> delayed(nano())
  9. Indelayed method
  10. Gettingnano
  11. Param: 4475258994017
  12. Gettingnano
  13. res3:Long = 4475259694720
复制代码

已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

使用道具

lhf8059 发表于 2015-5-5 16:54:53 |显示全部楼层 |坛友微信交流群
  1. Listing 3-12. Call by reference
  2. def notDelayed(t:Long) = {
  3. println("Innotdelayed method")
  4. println("Param:"+t)
  5. t
  6. }
  7. Let’s try calling notDelayed:
  8. scala> notDelayed(nano())
  9. Gettingnano
  10. Innotdelayed method
  11. Param: 4513758999378
  12. res4:Long = 4513758999378
复制代码

已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

使用道具

wfldragon 发表于 2015-5-5 18:34:05 |显示全部楼层 |坛友微信交流群
  1. Listing 3-13. Companion object
  2. trait Shape {
  3. def area :Double
  4. }
  5. object Shape {
  6. private class Circle(radius: Double) extends Shape{
  7. override val area = 3.14*radius*radius
  8. }
  9. private class Rectangle (height: Double, length: Double)extends Shape{
  10. override val area = height * length
  11. }
  12. def apply(height :Double , length :Double ) : Shape = new Rectangle(height,length)
  13. def apply(radius :Double) : Shape = new Circle(radius)
  14. }
复制代码

已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

使用道具

iorent 发表于 2015-5-5 20:25:03 |显示全部楼层 |坛友微信交流群
  1. Listing 3-14. Using import inside class body
  2. classFrog {
  3. importscala.xml._
  4. defn:NodeSeq= NodeSeq.Empty
  5. }
复制代码

使用道具

lancerthz 发表于 2015-5-6 00:59:59 |显示全部楼层 |坛友微信交流群
  1. Listing 3-15. Combining local scope import andimporting objects
  2. scala> objectMoose{
  3. defbark = "woof"
  4. }
  5. definedmodule Moose
  6. scala> importMoose._
  7. import Moose._
  8. scala> bark
复制代码

已有 1 人评分论坛币 收起 理由
Nicolle + 20 鼓励积极发帖讨论

总评分: 论坛币 + 20   查看全部评分

使用道具

sunyiping 发表于 2015-5-6 01:21:48 |显示全部楼层 |坛友微信交流群
  1. Listing 3-16. Vehicle class
  2. class Vehicle (speed : Int){
  3. val mph :Int = speed
  4. def race() = println("Racing")
  5. }
  6. The Vehicle class takes one argument, which is the speed of the vehicle. This argument must be passed
  7. when creating an instance of class Vehicle, as follows: new Vehicle(100). The class contains one method,
  8. called race.
复制代码

使用道具

oyjy1986 在职认证  发表于 2015-5-6 06:55:21 来自手机 |显示全部楼层 |坛友微信交流群
  1. Listing 3-17. Overriding methods inherited from a super class
  2. class Car (speed : Int) extends Vehicle(speed) {
  3. override val mph: Int= speed
  4. override def race() = println("Racing Car")
  5. }
  6. The class Car extends Vehicle class using the keyword extends. The field mph and the method race
  7. needs to be overridden using the keyword override.
  8. Listing 3-18 shows another class Bike that extends Vehicle.
复制代码

使用道具

tbs20 发表于 2015-5-6 09:14:19 |显示全部楼层 |坛友微信交流群
  1. Listing 3-18. Vehicle hierarchy
  2. class Vehicle (speed : Int){
  3. val mph :Int = speed
  4. def race() = println("Racing")
  5. }
  6. class Car (speed : Int) extends Vehicle(speed) {
  7. override val mph: Int= speed
  8. override def race() = println("Racing Car")
  9. }
  10. class Bike(speed : Int) extends Vehicle(speed) {
  11. override val mph: Int = speed
  12. override def race() = println("Racing Bike")
  13. }
复制代码

使用道具

condmn 发表于 2015-5-6 17:29:30 |显示全部楼层 |坛友微信交流群
  1. Listing 3-20. Using with
  2. class Batmobile(speed : Int) extends Vehicle(speed) with flying with gliding{
  3. override val mph: Int = speed
  4. override def race() = println("Racing Batmobile")
  5. override def fly() = println("Flying Batmobile")
  6. override def float() = println("Gliding Batmobile")
  7. }
  8. In Scala, traits can inherit classes. The keyword extends is also used when a class inherits a trait as its
  9. parent. The keyword extends is also used even when the class mixes in other traits using the with keyword.
  10. Also, extends is used when one trait is the child of another trait or class.
  11. You can now create a Batmobile in the REPL as illustrated here:
  12. scala> val vehicle3 = new Batmobile(300)
  13. vehicle3: Batmobile = Batmobile@374ed5
  14. Now
复制代码

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-4-16 14:06