Scala中的值类

Value classes are a special mechanism in Scala that is used to help the compiler to avoid allocating run time objects.

值类是Scala中的一种特殊机制,用于帮助编译器避免分配运行时对象。

This is done by defining a subclass of AnyVal. The only parameter it excepts is the datatype whose value is to be redefined. This is used to redefine all value types except the hashCode and equals.

这是通过定义AnyVal的子类来完成的 。 它唯一的参数是要重新定义其值的数据类型。 这用于重新定义除hashCode和equals之外的所有值类型。

The value types that can be redefined are Char, Int, Float, Double, Long, Short, Byte, Unit, and Boolean.

可以重新定义的值类型为CharIntFloatDoubleLongShortByteUnitBoolean

Syntax:

句法:

The syntax for creating a value class in Scala,

在Scala中创建值类的语法,

case class class_name(val val_ name : datatype) extends AnyVal 

The value class can have methods that are used to redefine the values or wrap the value used at the time of object creation to a different value.

值类可以具有用于重新定义值或将对象创建时使用的值包装为其他值的方法。

Some points to remember about value class,

关于价值舱,要记住一些要点,

  1. The main purpose of value class is to optimize the program by avoiding run time allocations.

    值类的主要目的是通过避免运行时分配来优化程序。

  2. The value class cannot be inherited by other classes.

    值类不能被其他类继承。

  3. The value class cannot be nested.

    值类不能嵌套。

  4. The value class is used to redefine all value types except the hashCode and equals.

    值类用于重新定义除hashCodeequals之外的所有值类型。

  5. It can have only methods (def) as its member i.e. no variable is allowed.

    它只能有方法( def )作为其成员,即不允许任何变量 。

Scala程序了解价值类的概念 (Scala program to understand the concept of value class)

// Program to show the working of 
// value class in Scala
class valueClass(val str: String) extends AnyVal {
def doubleprint() = str + str
}
object myObject {
def main(args: Array[String]) {
val obj = new valueClass("Scala ")
println(obj.doubleprint())
}
}

Output:

输出:

Scala Scala 

Explanation:

说明:

Here, we have created a value class in Scala named valueClass for string type which has a function named doubleprint that stores the string twice in the object.

在这里,我们在Scala中为字符串类型创建了一个名为valueClass的值类,该值类具有一个名为doubleprint的函数,该函数将字符串在对象中存储两次。

Generally, the value Class is used to store the same data as in the object and it just functions to improve the performance.

通常,值Class用于存储与对象中相同的数据,它仅用于提高性能。

什么是价值类? (What value class does?)

The value class acts as a wrapper class to other types. And the instances of wrapper class are not created at the compile time which improves the performance as less initialization is to be done and compiler time.

值类充当其他类型的包装器类。 而且,包装类的实例不是在编译时创建的,这可以提高性能,因为需要执行的初始化和编译时间更少。

翻译自: https://www.includehelp.com/scala/value-class.aspx

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377050.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

《MySQL8.0.22:Lock(锁)知识总结以及源码分析》

目录1、关于锁的一些零碎知识,需要熟知事务加锁方式:Innodb事务隔离MVCC多版本并发控制常用语句 与 锁的关系意向锁行级锁2、锁的内存结构以及一些解释3、InnoDB的锁代码实现锁系统结构lock_sys_tlock_t 、lock_rec_t 、lock_table_tbitmap锁的基本模式的…

关于ORA-04021解决办法(timeout occurred while waiting to lock object)

某个应用正在锁定该表或者包 表为 select b.SID,b.SERIAL#,c.SQL_TEXT from v$locked_object a, v$session b, v$sqlarea c where a.SESSION_ID b.SID and b.SQL_ADDRESS c.ADDRESS and c.sql_text like %table_name% 包为 select B.SID,b.USERNAME,b.MACHINE FROM V$ACCESS …

HtmlAutoTestFrameWork

前段时间做的自动化测试的是Silverlight的,框架都已经搭好。突然测试发现这里还有一个要发送邮件的html页面,并且将另外启动浏览器,于是今天下午把这个html的也写出来。用法 : HtmlAutoTestFrameWork htf new HtmlAutoTestFrameW…

L8ER的完整形式是什么?

L8ER:稍后 (L8ER: Later) L8ER is an abbreviation of "Later". L8ER是“ Later”的缩写 。 It is an expression, which is commonly used in messaging or chatting on social media networking sites like Facebook, Yahoo Messenger, and Gmail, etc…

Randomize select algorithm 随机选择算法

从一个序列里面选择第k大的数在没有学习算法导论之前我想最通用的想法是给这个数组排序,然后按照排序结果返回第k大的数值。如果使用排序方法来做的话时间复杂度肯定至少为O(nlgn)。 问题是从序列中选择第k大的数完全没有必要来排序&#xff…

《Linux杂记:一》

目录CPU负载和CPU利用率CPU负载很高,利用率却很低的情况负载很低,利用率却很高常用linux命令常用的文件、目录命令常用的权限命令常用的压缩命令CPU负载和CPU利用率 可以通过 uptime , w 或者 top 命令看到CPU的平均负载。 Load Average :负载的3个数字,比如上图的0.57、0.4…

IOS Plist操作

代码:copy BUNDLE下的plist文件 到 library下面。 bundle下不支持些,library,doc路径支持读与写。 (void)copyUserpigListToLibrary {NSFileManager *fileManager [NSFileManager defaultManager];NSArray *paths NSSearchPathForDirector…

《线程管理:线程基本操作》

目录线程管理启动线程与(不)等待线程完成特殊情况下的等待(使用trycath或rall)后台运行线程线程管理 启动线程与(不)等待线程完成 提供的函数对象被复制到新的线程的存储空间中,函数对象的执行…

scala特质_Scala的特质

scala特质Scala特质 (Scala traits) Traits in Scala are like interfaces in Java. A trait can have fields and methods as members, these members can be abstract and non-abstract while creation of trait. Scala中的特性类似于Java中的接口 。 特征可以具有作为成员的…

优化PHP代码的40条建议(转)

优化PHP代码的40条建议 40 Tips for optimizing your php Code 原文地址:http://reinholdweber.com/?p3 英文版权归Reinhold Weber所有,中译文作者yangyang(aka davidkoree)。双语版可用于非商业传播,但须注明英文版作…

Iptables入门教程

转自:http://drops.wooyun.org/tips/1424 linux的包过滤功能,即linux防火墙,它由netfilter 和 iptables 两个组件组成。 netfilter 组件也称为内核空间,是内核的一部分,由一些信息包过滤表组成,这些表包含内…

《线程管理:传递参数、确定线程数量、线程标识》

参考《c Concurrency In Action 》第二章做的笔记 目录传递参数量产线程线程标识传递参数 thread构造函数的附加参数会拷贝至新线程的内存空间中,即使函数中的采纳数是引用类型,拷贝操作也会执行。如果我们期待传入一个引用,必须使用std::re…

手把手玩转win8开发系列课程(14)

这节的议程就是——添加appbar appbar是出现在哪儿了,出现在屏幕的底部。他能使用户能用手势或者使用鼠标操作程序。metro UI 重点是在主要的控件使用许多控件,使其用户使用win8电脑更加的方便。而appBar使其用户体验更好。在这节中,我将告诉…

No identities are available for signing 的解决办法

今天重新上传做好的app提交到app store,结果就出现标题上的错误。“No identities are available for signing”。 以后碰到这样的问题按照下面几个步骤来做: 进入Distribution -----下载发布证书 -----双击安装-----重启Xcode就能上传了 其他细节 如果再…

半连接反连接

半连接&反连接 1. 半连接 半连接返回左表中与右表至少匹配一次的数据行,通常体现为 EXISTS 或者 IN 子查询。左表驱动右表。只返回左表的数据,右表作为筛选条件。 可以用 EXISTS、 IN 或者 ANY 举例:表t1和表t2做半连接,t…

匿名方法和Lambda表达式

出于MVVM学习的需要,复习下匿名方法和Lambda表达式,因为之前用的也比较少,所以用的也不是很熟练,Baidu下相关的知识,写了这个Demo,目标是用简单的方法展示这个怎么用。 这里偏重的和LINQ中的Lambda表达式 …

烂橘子

Problem Statement: 问题陈述: Given a matrix of dimension r*c where each cell in the matrix can have values 0, 1 or 2 which has the following meaning: 给定尺寸r * C的矩阵,其中矩阵中的每个单元可以具有其具有以下含义的值0,1或2…

android junit 测试程序

http://blog.csdn.net/to_cm/article/details/5704783 Assert.assertEquals(2, t); 断言转载于:https://www.cnblogs.com/wjw334/p/3714120.html

MySQL 8.0.22执行器源码分析HashJoin —— BuildHashTable函数细节步骤

BuildHashTable函数细节步骤 该函数位置处于hash_join_iterator.cc 403 ~ 560行 step1:如果被驱动表迭代器没有更多的行数,更新m_state为EOR,然后返回false,表明创建hash表失败 if (!m_build_iterator_has_more_rows) {m_state…

《那些年啊,那些事——一个程序员的奋斗史》——125

距离离职交接的一个月时间还剩几天,本来应该是平淡无事的,却没想到最后还是波澜四起。昨天下班前,公司突然停了电。这本是件普通得不能再普通的事情,可没想到过了一会来电了,或许是波峰电压太大,或许是稳压…