本文共 10704 字,大约阅读时间需要 35 分钟。
scala案例
In this post, we are going to discuss about one of the important concept of Scala Language: Case Class and Case Object.
在本文中,我们将讨论Scala语言的重要概念之一:案例类和案例对象。
The main advantage or aim of Case Class concept is that to ease the development by avoiding lot of boilerplate code. We can use Case Classes in Pattern Matching very easily.
案例类概念的主要优点或目的是通过避免大量样板代码来简化开发。 我们可以很容易地在模式匹配中使用案例类。
As this concept is very important, I’m going to deliver this into two different posts:
由于这个概念非常重要,因此我将其介绍给两个不同的职位:
We will discuss “Case Class and Case Object Basics” in detail in this post with some suitable examples. Will deliver one more post for some advanced concepts.
我们将在本文中通过一些合适的示例详细讨论“案例类和案例对象基础”。 将再发布一些高级概念的文章。
Case class is also a class, which is defined with “case” modifier. Because of this “case” keyword, we will get some benefits to avoid boilerplate code.
案例类也是一个用“案例”修饰符定义的类。 由于使用了“ case”关键字,我们将获得一些避免样板代码的好处。
Example:-
例:-
scala> case class Employee(name:String)defined class Employee
Here we have defined a case class with name “Employee” and with one parameter “name”.
在这里,我们定义了一个案例类,名称为“ Employee”,参数为“ name”。
Case object is also an object which is defined with “case” modifier. Because of this “case” keyword, we will get some benefits to avoid boilerplate code.
案例对象也是使用“案例”修饰符定义的对象。 由于使用了“ case”关键字,我们将获得一些避免样板代码的好处。
Example:-
例:-
scala> case object Employeedefined object Employee
Here we have defined a case object with name “Employee”.
在这里,我们定义了一个名称为“ Employee”的案例对象。
Let us explore the benefits of Case Classes in-detail with examples.
让我们通过示例详细探讨案例类的好处。
First and foremost benefit of Case Class is that Scala Compiler adds a factory method with the name of the class with same number of parameters defined in the class definition.
Case类的首要优势是Scala编译器添加了一个工厂方法,该方法的类名与在类定义中定义的参数数量相同。
Because of this benefit, we can create objects of the Case Class without using “new” keyword.
因此,我们无需使用“ new”关键字就可以创建Case类的对象。
Example:-
例:-
scala> case class Person(name:String, age:Int)defined class Personscala> val person1 = Person("Posa",30)person1: Person = Person(Posa,30)scala> val person2 = new Person("Posa",30)person2: Person = Person(Posa,30)
If we observe above code snippet, we can create Case class objects without using “new” keyword. It is also allowed “new” keyword, but not recommended to use it here.
如果我们观察以上代码片段,则可以创建Case类对象,而无需使用“ new”关键字。 还允许使用“ new”关键字,但不建议在此处使用它。
By default, Scala compiler prefixes “val” for all constructor parameters. That’s why without using val or var, Case class’s constructor parameters will become class members, it is not possible for normal classes.
默认情况下,Scala编译器为所有构造函数参数添加“ val”前缀。 这就是为什么不使用val或var的情况下,Case类的构造函数参数将成为类成员,而普通类则不可能。
Example:-
例:-
scala> case class Person(name:String, age:Int)defined class Personscala> val person1 = Person("Posa",30)person1: Person = Person(Posa,30)scala> person1.nameres0: String = Posascala> person1.ageres1: Int = 30scala> person1.age = 32:13: error: reassignment to val person1.age = 32 ^
By observing above example, we can say that Case class constructor parameters are by default val. We cannot reassign a new value to them once that class object is created. We can access their values by using getter methods, but they don’t have setter methods.
通过观察上面的示例,我们可以说Case类的构造函数参数默认为val。 创建该类对象后,我们无法为它们重新分配新值。 我们可以使用getter方法访问它们的值,但是它们没有setter方法。
NOTE:- Before discussing the next benefit, we need to understand one point about Case classes.
When we compile a Case class using scalac, what will happen? How many “*.class” files are generated for one Case classes? What are they?注意:-在讨论下一个好处之前,我们需要了解关于案例类的一点。
当我们使用scalac编译Case类时,会发生什么? 一个Case类生成了多少个“ * .class”文件? 这些是什么?Let us explore this with a simple example:
让我们通过一个简单的示例对此进行探讨:
Person.scala
人标
case class Person(name:String, age:Int)
Here we can observe that only one source file “Persona.scala” is available.
在这里我们可以看到只有一个源文件“ Persona.scala”可用。
Here we can observe that Scala Compiler has generated two class files: “Person.class” and “Person$.class”
在这里我们可以看到Scala编译器已经生成了两个类文件:“ Person.class”和“ Person $ .class”
NOTE:- We will get similar kind of two classes even for Case Objects.
注意:-即使对于Case Objects,我们也会得到类似的两个类。
It is most important feature and very useful. Scala compiler automatically adds “Default Implementation” to toString, hashCode and equals and copy methods.
这是最重要的功能,非常有用。 Scala编译器会自动将“默认实现”添加到toString,hashCode以及equals和copy方法。
We can observe this in the following diagram.
我们可以在下图中观察到这一点。
Scala compiler also adds a copy method to Case class automatically. It is used to create a copy of same instance with modifying few attributes or without modifying it.
Scala编译器还自动将复制方法添加到Case类。 它用于创建同一实例的副本,而只需修改几个属性或不修改它。
We can observe this feature in the above diagram.
我们可以在上图中观察到此功能。
Example-1:- To create a new copy of same object without changing the object attributes.
示例1:-在不更改对象属性的情况下创建同一对象的新副本。
scala> case class Student(name:String, marks:Int)defined class Studentscala> val s1 = Student("Rams",550)s1: Student = Student(Rams,550)scala> val s2 = s1.copy()s2: Student = Student(Rams,550)
Here, we have created new object s2 by using copy method on s1 object without changing s1 object attributes. That means both are equal as shown below:
在这里,我们通过在s1对象上使用复制方法创建了新的对象s2,而没有更改s1对象的属性。 这意味着两者相等,如下所示:
scala> s1 == s2res3: Boolean = true
Example:- To create a new copy of same object with changing the object attributes.
示例:-通过更改对象属性创建相同对象的新副本。
scala> case class Student(name:String, marks:Int)defined class Studentscala> val s1 = Student("Rams",550)s1: Student = Student(Rams,550)scala> val s3 = s1.copy(marks=590)s3: Student = Student(Rams,590)
NOTE:- Here we are using Scala’s Named Parameter Feature. Please read my Scala’s Named Parameter tutorial to understand it well.
注意:-这里我们使用Scala的命名参数功能。 请阅读我的Scala的命名参数教程,以更好地理解它。
Here, we have created new object s3 by using copy method on s1 object without changing s1 object attributes. That means both are NOT equal as shown below:
在这里,我们通过对s1对象使用复制方法创建了新对象s3,而没有更改s1对象属性。 这意味着两者不相等,如下所示:
scala> s1 == s3res3: Boolean = false
Scala compiler also creates a companion object to that Case class and adds apply and unapply methods. We can observe this in the following diagram.
Scala编译器还会为该Case类创建一个伴随对象,并添加apply和unapply方法。 我们可以在下图中观察到这一点。
Example:-
If we create a Case Class as shown below:例:-
如果我们创建一个Case类,如下所示:case class Person(name:String)
Scala Compiler adds a Companion Object with apply and unapply methods as shown below:
Scala编译器添加了一个带有apply和unapply方法的Companion对象,如下所示:
object Person{ def unapply(p:Person):Option[String] = Some(p.name) def apply(name:String):Person = new Person(name)}
So far, we have discussed what are the benefits of Scala’s Case Class. I’m going to list out all those benefits one by one in brief in next section.
到目前为止,我们已经讨论了Scala案例类的好处。 我将在下一节中简要列出所有这些好处。
NOTE:- Java does NOT have Case class or Case object concept.
注意: Java没有Case类或Case对象的概念。
Case class is also a class, however when we compare it with normal class, it gives us some extra features or benefits.
案例类也是一类,但是当我们将其与普通类进行比较时,它为我们提供了一些额外的功能或好处。
The following are the complete list of Advantages/Benefits of Scala’s Case class:
以下是Scala Case类的优点/好处的完整列表:
All these features are added by Scala Compiler at compile-time. It is not possible with normal class.
所有这些功能都是由Scala编译器在编译时添加的。 普通班不可能。
As we discussed in the above section, Scala’s Case Class/Object have many benefits. They ease the Development process and improve the the Productivity. However they have only few drawbacks and they are ignorable:
正如我们在上一节中讨论的那样,Scala的Case类/对象有很多好处。 它们简化了开发过程并提高了生产率。 但是,它们只有很少的缺点,并且可以忽略:
If we see compiled class using any Java/Scala Decompiler, we can observe the following code snippet.
如果我们看到使用任何Java / Scala Decompiler编译的类,则可以观察以下代码段。
public class Person implements scala.Product ... { // Some methods are inherited from Product trait}
That’s it all about Scala Case Class and Case Object Basics. We will discuss Scala Case Class and Case Object Advanced concepts in my coming posts.
这就是有关Scala Case类和Case对象基础的全部内容。 我们将在我的后续文章中讨论Scala案例类和案例对象高级概念。
Please drop me a comment if you like my post or have any issues/suggestions.
如果您喜欢我的帖子或有任何问题/建议,请给我评论。
翻译自:
scala案例
转载地址:http://tuqzd.baihongyu.com/