scala代码示例
Scala Annotations are metadata or extra information added to the program source code. Like comments, annotations can be attached to a variable, method, expression or any other program element.
Scala注释是添加到程序源代码中的元数据或其他信息。 像注释一样,注释可以附加到变量,方法,表达式或任何其他程序元素上。
Annotations are allowed on any kind of definition or declaration including vars, vals, classes, objects, traits, defs and types.
允许在任何种类的定义或声明中使用注释,包括var,val,类,对象,特征,def和类型。
The syntax for declaring an annotation can be of the form;
声明注释的语法可以是以下形式:
@annot(exp_{1}, exp_{2}, ...) {val name_{1}=const_{1}, ..., val name_{n}=const_{n}}
The “annot” specifies the class of the annotation and all annotations must include this. Some of the annotations might not need any arguments and hence the parentheses can be excluded or an empty () can be used.
“ annot”指定注释的类别,并且所有注释都必须包括该注释。 一些注释可能不需要任何参数,因此可以排除括号或可以使用空()。
The precise form of the arguments given to the annotation depends on the annotation class in particular. Most annotation processors allow immediate constants such as “Hi”, 678 etc. Keyword “this” can be used to refer to other variables in the scope.
赋予注释的参数的精确形式尤其取决于注释类。 大多数注释处理器允许使用立即数,例如“ Hi”,678等。关键字“ this”可用于引用范围中的其他变量。
The “name=const” are available for more complicated annotations having optional arguments. These arguments are optional, and they can be specified in any order. The right hand side value after equals sign is recommended to be a constant.
“ name = const”可用于具有可选参数的更复杂的注释。 这些参数是可选的,可以以任何顺序指定。 等号后的右侧值建议为常数。
Scala has several standard annotations. They are;
Scala有几个标准注释。 他们是;
scala.SerialVersionUID → This annotation specifies the static SerialVersionUID field of a serializable class.
scala.SerialVersionUID →此注释指定可序列化类的静态SerialVersionUID字段。
scala.deprecated → This annotations tells that the definition is removed.
scala.deprecated →此注释告诉您定义已删除。
scala.volatile → This annotations allows programmers to use mutable state in concurrent programs.
scala.volatile →此注释允许程序员在并发程序中使用可变状态。
scala.transient → This annotation marks a field to be non-persistent.
scala.transient →此注释将字段标记为非持久性。
scala.throws → This annotation specifies the exceptions thrown by a method.
scala.throws →此注释指定方法抛出的异常。
scala.cloneable → This annotation designates the class to which it is applied as cloneable
scala.cloneable →此批注指定将其应用为可克隆的类
scala.native → This annotation is a marker for native methods.
scala.native →此注释是本机方法的标记。
scala.inline → This annotation on methods requests that the compiler should try hard to inline the annotated method.
scala.inline →在方法上的此注释要求编译器应尽量内联已注释的方法。
scala.remote → This annotation designates the class to which it is applied as remotable.
scala.remote →此批注指定将其应用于远程的类。
scala.serializable → This annotation designates the class to which it is applied as serializable
scala.serializable →此批注指定将其应用为可序列化的类
scala.unchecked → This annotation gets applied to a selector in a match expression. If present, exhaustiveness warnings for that expression will be suppressed.
scala.unchecked →此注释将应用于匹配表达式中的选择器。 如果存在,则将禁止该表达式的穷举性警告。
scala.reflectBeanProperty → This annotation adds getter and setter methods following the JavaBean convention when attached to a field.
scala.reflectBeanProperty →当附加到字段时,此注释将遵循JavaBean约定添加getter和setter方法。
Let’s look at some of the annotations with examples.
我们来看一些带有示例的注释。
There arises a need to write a class or a method that is not needed for later use. Deprecation allows us to add a notification in the method or a class that we don’t want others to use, but we can’t remove for backward compatibility. The method or class can be marked as @deprecated so that when others try to use this a deprecation warning is shown.
产生了编写以后使用不需要的类或方法的需要。 弃用允许我们在不希望其他人使用的方法或类中添加通知,但由于向后兼容而无法删除。 该方法或类可以标记为@deprecated,以便在其他人尝试使用此方法或类时显示弃用警告。
For example create a Scala object as below.
例如,如下创建一个Scala对象。
deprecated.scala
deprecated.scala
package com.annotations
import scala.deprecated;
object Deprecation1 {
@deprecated def printMessage() = {
println("This method is deprecated")
}
def main(args: Array[String]) {
printMessage()
}
}
The method printMessage is marked as deprecated. The Scala compiler will emit the deprecation warnings whenever Scala code accesses printMessage method in the main as shown in the below image.
方法printMessage被标记为已弃用。 每当Scala代码访问主程序中的printMessage方法时,Scala编译器都会发出弃用警告,如下图所示。
Sometimes programmers want to use mutable state in the concurrent programs. The @volatile
annotation helps in such cases and informs the compiler that the variable will be used by multiple threads. The @volatile keyword gives different guarantees on different platforms such as on the Java platform, however, you get the same behavior as if you wrote the field in Java code and marked it with the Java volatile modifier.
有时程序员希望在并发程序中使用可变状态。 @volatile
注释在这种情况下会有所帮助,并通知编译器该变量将被多个线程使用。 @volatile关键字在不同的平台(例如Java平台)上提供了不同的保证,但是,您得到的行为与使用Java代码编写该字段并用Java volatile修饰符标记该字段的行为相同。
Serialization framework converts objects into stream of bytes and vice versa which would be useful while saving objects to the disk or transferring them over a network. Scala does not have its own serialization framework and hence an underlying framework should be used. The @serializable
annotation indicates whether a class is serializable. By default the class is considered as not serializable and hence the @serializable annotation is added.
序列化框架将对象转换为字节流,反之亦然,这在将对象保存到磁盘或通过网络传输它们时很有用。 Scala没有自己的序列化框架,因此应使用基础框架。 @serializable
注释指示类是否可序列化。 默认情况下,该类被视为不可序列化的,因此添加了@serializable注释。
The @SerialVersionUID
deals with the serializable classes that changes with time. The serial number can be attached to current version as @SerialVersionUID(678)
where 678 is the serial id.
@SerialVersionUID
处理随时间变化的可序列化类。 序列号可以作为@SerialVersionUID(678)
附加到当前版本,其中678是序列号。
Scala provides @transient
annotation for fields. If the field is marked as @transient, then the framework should not save the field even when the surrounding object is serialized. When the object is loaded, the field will be restored to the default value for the type of the field annotated as @transient.
Scala为字段提供@transient
注释。 如果将该字段标记为@transient,则即使序列化周围的对象,框架也不应保存该字段。 加载对象时,该字段将恢复为标为@transient的字段类型的默认值。
Scala provides the @scala.reflect.BeanProperty
annotation that adds this annotation to a field and the compiler automatically generate getter and setter methods for the field.
Scala提供了@scala.reflect.BeanProperty
批注,该批注@scala.reflect.BeanProperty
批注添加到字段中,并且编译器会自动为该字段生成getter和setter方法。
For example – If you annotate a field named id, the getter method will be named getId
and the setter method will be named setId
. The getter and setter methods are available only after the compilation pass completes. Scala code fields can be accessed directly. This feature is intended to support frameworks that expect regular getter and setter methods and do not compile the framework, code that used it at the same time.
例如–如果注释名为id的字段,则getter方法将命名为getId
,setter方法将命名为setId
。 只有在编译过程完成后,getter和setter方法才可用。 Scala代码字段可以直接访问。 此功能旨在支持期望使用常规getter和setter方法并且不编译框架(同时使用该框架的代码)的框架。
For instance Open the Scala IDE and create the scala class as;
例如,打开Scala IDE并按以下方式创建scala类:
Car.scala
Car.scala
package scala.annotations.car
import scala.beans.BeanProperty
class Car {
@BeanProperty
var cname = "Alto"
@BeanProperty
var cno = 67
}
We are using the @BeanProperty annotation which generates getter setter methods for properties cname and cno.
我们使用@BeanProperty批注,该批注为属性cname和cno生成getter setter方法。
Now create a java program and use the scala Car class created as
现在创建一个Java程序,并使用按以下方式创建的scala Car类:
Bean.java
Bean.java
package com.annotations;
import scala.annotations.car.*;
public class Bean {
public static void main(String[] args){
Car c = new Car();
System.out.println("Car Name:" +c.getCname());
System.out.println("Car Number: "+c.getCno());
}
}
The getter methods for cname and cno are generated by the scala @BeanProperty methods that can be used in Java programs as shown in above class code.
cname和cno的getter方法由scala @BeanProperty方法生成,可在Java程序中使用,如上面的类代码所示。
The @unchecked annotation is interpreted by the compiler during pattern matches. It tells the compiler not to worry if the match expression seems to leave out some cases.
@unchecked批注在模式匹配期间由编译器解释。 它告诉编译器不要担心match表达式是否忽略了某些情况。
That’s all for annotations in Scala programming, you can explore other annotations listed above yourself.
这就是Scala编程中的注释的全部内容,您可以浏览自己上方列出的其他注释。
翻译自: https://www.journaldev.com/8426/scala-annotations-example
scala代码示例