scala类的序列化

A sequence comprehension statement consists of a generator part which generates a list of values from the specified range of inputs and a statement which operates on these generated elements which is then stored in the output list to be returned at the end of computation. For example;

序列理解语句由一个生成器部分和一个对这些生成的元素进行操作的语句组成,该生成器部分从指定的输入范围生成一个值列表,这些语句然后存储在输出列表中,以在计算结束时返回。 例如;

object Sequence {def main(args:Array[String]) {def increment(from: Int, to:Int):List[Int] =for(i <- List.range(from,to) if  i % 5 == 0) yield i
println(increment(1,30))
}
}

In this example we are creating a method that accepts two Integer parameters from and to that takes the range of the numbers. For every number starting from 1 we are checking whether the number is divisible by 5 as i%5 == 0 which prints only the numbers divisible by 5 in the range 1-30.

在此示例中,我们将创建一个方法,该方法从到接受两个Integer参数,并采用数字范围。 对于从1开始的每个数字,我们都在检查该数字是否可被5整除,因为i%5 == 0,它仅打印在1-30范围内被5整除的数字。

Below image shows the output produced and you can see that they all are divisible by 5.

下图显示了产生的输出,您可以看到它们都可以被5整除。

Consider an example which returns a pair of numbers when multiplied is equal to specified value y.

考虑一个示例,该示例在相乘等于指定值y时返回一对数字。

object Seq {def main(args:Array[String]){def multiples(x:Int, y:Int) =for(a <- 0 until x;b <- a until x if a * b == y) yieldTuple2(a,b);multiples(42,40) foreach {case(a,b) =>println("(" + a + " ," + b + ")")}}
}

In this example we are defining a method “multiples” that accepts two integer argument x and y . We are checking for the condition that variables a and b when multiplied gives the value equal to the value of y specified by the user. The result is a pair of numbers as shown in the below image.

在此示例中,我们定义了一个“ multiples”方法,该方法接受两个整数参数x和y。 我们正在检查变量a和b相乘时得出的值等于用户指定的y的条件。 结果是一对数字,如下图所示。

Consider an example of a comprehension which returns Unit.

考虑一个返回Unit的理解示例。

object Comprehension {def main(args:Array[String]){for (i <- Iterator.range(0, 15);j <- Iterator.range(i, 15) if i*j == 12)println("(" + i + ", " + j + ")")}
}

The yield keyword should not be used for such comprehensions. The range of numbers is specified using the Iterator.range. Above code produces following output.

yield关键字不应用于这种理解。 数字范围是使用Iterator.range指定的。 上面的代码产生以下输出。

Scala通用类 (Scala Generic Classes)

Scala supports generic classes which are useful for the development of collection classes. For example;

Scala支持泛型类,这对于开发集合类很有用。 例如;

object GenericClass {def main(args:Array[String]) {abstract class Multiply[x] {def multiply(a: x, b: x): x;}class intMultiply extends Multiply[Int] {def multiply(a: Int, b: Int): Int = a * b;}class DoubleMultiply extends Multiply[Double] {def multiply(a : Double, b : Double) : Double = a * b;}val m1 = new intMultiply().multiply(20,15);val m2 = new DoubleMultiply().multiply(20.34, 6.56);println(m1);println(m2);
}
}

In this example we declare an abstract class Multiply with x that can accept any data type and define multiply method with a and b variables of x data type. Now we define two classes intMultiply, DoubleMultiply that accepts Integer and Double types respectively. We create m1 and m2 and call the methods multiply by passing the arguments.

在此示例中,我们声明一个抽象类Multiply with x,它可以接受任何数据类型,并使用x数据类型的a和b变量定义乘法方法。 现在,我们定义两个类intMultiply和DoubleMultiply分别接受Integer和Double类型。 我们创建m1和m2并通过传递参数来调用方法相乘。

Scala内部类 (Scala Inner Classes)

A class can be defined inside another class in Scala which is termed as an inner class. The inner class is bound to the outer object. For example;

可以在Scala中的另一个类(称为内部类)中定义一个类。 内部类绑定到外部对象。 例如;

class Add {class Addtwonumbers {var a = 12;var b = 31;var c = a + b;}
}object Innerclass {def main(args:Array[String]) {val a1 = new Add
val a2 = new Add
val b1 = new a1.Addtwonumbers
val b2 = new a2.Addtwonumbersb1.a = 30;
b1.b = 45;
b2.a = 55;
b2.b = 24;
println(s"b1.a = ${b1.a}")
println(s"b1.b = ${b1.b}")
println(s"b2.a = ${b2.a}")
println(s"b2.b = ${b2.b}")
println(s"Result = ${b2.c}")}
}

In this example we define an Outer class Add and inside this class we define one more class Addtwonumbers which is the inner class. In the inner class Addtwonumbers, we are defining variables a, b, c and storing result of a+b in c.

在此示例中,我们定义了外部类Add,并且在该类内部定义了另一个类Addtwonumbers,它是内部类。 在内部类Twotwonumbers中,我们定义变量a,b,c,并将a + b的结果存储在c中。

We are assigning values for variables a and b and accessing it by creating instances of the outer class and then instance of inner class through this outer class instance. Below image shows the output produced when main method of Innerclass is invoked.

我们为变量a和b赋值,并通过创建外部类的实例,然后通过该外部类实例创建内部类的实例来访问它。 下图显示了调用Innerclass main方法时产生的输出。

That’s all for a brief introduction to sequence comprehensions, generics and inner classes in Scala programming language.

以上就是对Scala编程语言中的序列理解,泛型和内部类的简要介绍。

翻译自: https://www.journaldev.com/8483/scala-sequence-comprehensions-generic-classes-and-inner-class-example

scala类的序列化

scala类的序列化_Scala序列理解,通用类和内部类示例相关推荐

  1. java oracle序列化_Java序列化(Serialization)的理解

    1.什么是序列化 Java是面向对象的编程语言,有时需要保存对象,并在下次使用时可以顺利还原该对象.由于这种需求很常见,所以Java API对此提供了支持,添加相关程序代码到标准类库中,并将保存和还原 ...

  2. Python进阶:理解元类创建类ABCMeta

    Python进阶:Python进阶:理解元类创建类ABCMeta 一.理解元类(Meta class) 1.1 元类直观理解 1.2 Python官方文档给出的元类描述 二.理解抽象基类(ABC, A ...

  3. JavaSE——IO(下)(Properties类、序列化与反序列化)

    第3节 IO(下) 一..properties文件与Properties类 1.1 .properties文件介绍 .properties文件一种属性文件,以键值对 的格式存储内容,在Java中可以使 ...

  4. mysql 序列化存储_如何将类序列化并直接存储入数据库

    本文将从这两个格式器入手,先向大家介绍分别用它们如何实现序列化和反序列化,然后比较两种格式器的不同点.接着我会向大家介绍实现序列化对对象类型的一些要求,同时还要向大家介绍两种不同的序列化方式:基本序列 ...

  5. 率清华团队研发“天机芯”登《Nature》封面,他说类脑计算是发展人工通用智能的基石...

    整理 | AI科技大本营(ID:rgznai100) 8 月,清华大学教授.类脑计算研究中心主任施路平率队研发的关于"天机芯"的论文登上<Nature>封面,这实现了中 ...

  6. 【Flutter】JSON 模型转换 ( JSON 序列化工具 | JSON 手动序列化 | 根据 JSON 编写 Dart 模型类 | 在线自动根据 JSON 转换 Dart 类 )

    文章目录 一.JSON 序列化工具 二.JSON 手动序列化 三.根据 JSON 编写 Dart 模型类 四.在线自动转换 五.相关资源 一.JSON 序列化工具 JSON 格式比较简单的话 , 使用 ...

  7. 类的序列化[Serializable]

    简介 序列化是指将对象实例的状态存储到存储媒体的过程.在此过程中,先将对象的公共字段和私有字段以及类的名称(包括类所在的程序集)转换为字节流,然后再把字节流写入数据流.在随后对对象进行反序列化时,将创 ...

  8. 对象序列化机制的理解

    1.对象的序列化机制: 对象序列化机制允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点. //当其它程序获取了 ...

  9. idea生成类中序列化id

    RPC接口中要传输的对象需要序列化,需要生成序列id,idea中生成序列id的方式如下 在需要添加序列id的类中,选中类名,alt+enter就可以实现了 转载于:https://www.cnblog ...

最新文章

  1. 图灵奖得主Judea Pearl 智源大会演讲:从“大数据革命”到“因果革命”
  2. STM32(Cortex-M3)启动过程+IAR中xcl及icf文件详解
  3. 题目1179:阶乘-------------阶乘不用long long int 就不能AC
  4. 前端学习(3025):vue+element今日头条管理-关于默认子路由的问题
  5. Python List reverse()方法
  6. php邮件中文乱码,phpmailer 发送邮件中文乱码问题的解决方法总结
  7. iOS7时代我们用什么来追踪和识别用户?
  8. Java Formatter locale()方法与示例
  9. Java使用HTTPClient4.3开发的公众平台消息模板的推送功能
  10. 在plc中用c语言实现电梯控制程序,三菱FX2N PLC电梯运行控制程序设计
  11. BZOJ2241 [SDOI2011]打地鼠 【模拟】
  12. python etree htm参数_使用etree.HTML的编码问题
  13. emacs VS vim 替换为回车符
  14. [原创]测试用例设计之“功能图”法
  15. 数据结构和算法9——哈希表
  16. iCollections for Mac(桌面图标及文件整理工具)
  17. 摩莎485通讯测试软件,485串口测试软件1.6 免费版
  18. 解决python使用猴子补丁时引入ssl错误
  19. win7装xp(win7装xp双系统教程)
  20. win10正式版新功能介绍

热门文章

  1. asp.net 导出word文档
  2. Win7允许/禁用 PING命令
  3. ZFS 学习(转载)
  4. [转载] python模板字符串和格式化字符串
  5. [转载] python字符串数组字典_Python:字符串、列表、元组、字典
  6. 微软Power BI 每月功能更新系列——3月Power BI 新功能学习
  7. Gym 101246G Revolutionary Roads
  8. java取得当前日期增加一天或多天
  9. php curl iis,解决IIS运行PHP出现Call to undefined function curl_init()的问题
  10. linux远程仿真,11.5 仿真的远程桌面系统: XRDP 服务器