scala 辅助构造函数_Scala辅助构造函数的深入介绍

2021-02-07 00:00:00 函数 构造 辅助

scala 辅助构造函数

Before reading this post, please read my previous post about “Scala Primary Constructor In-Depth”.

在阅读这篇文章之前,请阅读我之前关于“ Scala Primary Constructor Inpth”的文章 。

发表简短目录 (Post Brief TOC)

  • Introduction

    介绍

  • What is an Auxiliary Constructor?

    什么是辅助构造函数?

  • Auxiliary Constructor Advantages and Disadvantages

    辅助构造函数的优缺点

  • Scala Auxiliary Constructor Examples

    Scala辅助构造函数示例

  • Scala Auxiliary Constructor Rules

    Scala辅助构造函数规则

  • Constructor Overloading With Auxiliary constructors

    辅助构造函数的构造函数重载

介绍 (Introduction)

A Scala class can contain two kinds of constructors:

Scala类可以包含两种构造函数:

  • Primary Constructor

    主要建设者

  • Auxiliary Constructor

    辅助构造器

We have already discussed about Primary Constructor in my previous post. In this post, we are going to discuss about Auxiliary Constructors in-depth with suitable examples.

在我以前的文章中,我们已经讨论了有关Primary Constructor的问题。 在本文中,我们将通过适当的示例深入讨论辅助构造函数。

什么是辅助构造函数? (What is an Auxiliary Constructor?)

In Scala, We can define Auxiliary Constructors like methods by using “def” and “this” keywords. “this” is the constructor name.

在Scala中,我们可以使用“ def”和“ this”关键字来定义类似于方法的辅助构造函数。 “ this”是构造函数名称。

Auxiliary Constructor is also know as Secondary Constructor. A Scala class can contain zero or one or more Auxiliary Constructors.

辅助构造函数也称为辅助构造函数。 Scala类可以包含零个或一个或多个辅助构造函数。

辅助构造方法的优缺点 (Auxiliary Constructor Advantage and Disadvantage)

What is the main Advantage of Auxiliary Constructors in Scala?
In Scala, Auxiliary Constructors are used to provide Constructors Overloading.

Scala辅助构造函数的主要优点是什么?
在Scala中,辅助构造函数用于提供构造函数重载。

What is the main Disadvantage of Auxiliary Constructors in Scala?
We need to write lot of code to provide Constructors Overloading using Auxiliary Constructors. Then how to solve this problem? Please go through last section in this post.

Scala辅助构造函数的主要缺点是什么?
我们需要编写大量代码以使用辅助构造函数提供构造函数重载。 那么如何解决这个问题呢? 请仔细阅读这篇文章的最后一部分。

We should use same “this” as Auxiliary Constructor name, then how do we define more Auxiliary Constructors in Scala. We can define multiple Auxiliary Constructors by using different parameters list.

我们应该使用与辅助构造函数名称相同的“ this”,然后如何在Scala中定义更多的辅助构造函数。 我们可以使用不同的参数列表定义多个辅助构造函数。

What is Different Parameters list to a Constructor or Method or Function?
Different parameters list means:
a) Different in number of parameters or
b) Different in Parameter type

什么是构造函数,方法或函数的“不同参数”列表?
不同的参数列表意味着:
a)参数数量不同或
b)参数类型不同

Now we will discuss Auxiliary Constructors with syntax and examples in next section.

现在,我们将在下一节中讨论辅助构造器以及语法和示例。

Scala辅助构造函数示例:- (Scala Auxiliary Constructor Examples:-)

We define Auxiliary Constructors in Class Body with “def” and “this” keywords, but not in Class Definition. We use Class Definition to declare Primary Constructor.

我们在类主体中使用“ def”和“ this”关键字定义辅助构造函数,但不在类定义中定义。 我们使用类定义来声明主构造函数。

Important Point to Remember:-
NOTE:-Each Auxiliary Constructor should call one of the previous defined constructor. An Auxiliary Constructor can call either Primary Constructor or another Auxiliary constructors by using “this” name.

要记住的重点:
注意:-每个辅助构造函数都应调用以前定义的构造函数之一。 辅助构造函数可以使用“ this”名称调用主构造函数或另一个辅助构造函数。

Example-1:-
Create a Scala class with one Primary Constructor and Zero-Argument Auxiliary Constructor.

示例1:-
使用一个主构造函数和零参数辅助构造函数创建一个Scala类。

Employee1.scala

员工1.scala

class Employee1(val empId : Int, val empName:String){	
    println("From Primary Constructor")	
    def this(){
	this(0,null)
	println("From Zero-Argument Auxiliary Constructor")
    }	
}

Test Employee1.scala:-
To test the about program, we are going to write some sample examples as shown below:

测试Employee1.scala:-
为了测试about程序,我们将编写一些示例示例,如下所示:

Employee1Test1.scala

Employee1Test1.scala

object Employee1Test1 extends App{
  val emp1 = new Employee1()  
}

Output:- All my Scala Examples are placed “E:\>” Drive in my Windows machine.

输出:-我所有的Scala示例都放置在Windows计算机中的“ E:\>”驱动器中。

E:\>scalac Employee1.scala
E:\>scalac Employee1Test1.scala
E:\>scala Employee1Test1
From Primary Constructor
From Zero-Argument Auxiliary Constructor

By observing this output, we can say that Zero-Argument Auxiliary Constructor made a call to Primary Constructor. So it executes first Primary Constructor, then Zero-Argument Auxiliary Constructor. Because Auxiliary Constructor contains a call to Primary Constructor.

通过观察此输出,我们可以说零参数辅助构造函数调用了主要构造函数。 因此,它首先执行主构造函数,然后执行零参数辅助构造函数。 因为辅助构造函数包含对主构造函数的调用。

Employee1Test2.scala

Employee1Test2.scala

object Employee1Test2 extends App{
  val emp2 = new Employee1(1001, "Scala")  
}

Output:-

输出:-

E:\>scalac Employee1.scala
E:\>scalac Employee1Test2.scala
E:\>scala Employee1Test2
From Primary Constructor

javap output:-

javap输出:-

E:\>scalac Employee1.scala

E:\>javap Employee1.class
Compiled from "Employee1.scala"
public class Employee1 {
  public int empId();
  public java.lang.String empName();
  public Employee1(int, java.lang.String);
  public Employee1();
}

Example-2:-
Create a Scala class with one Primary Constructor and Multiple Auxiliary Constructors.

示例2:-
用一个主构造函数和多个辅助构造函数创建一个Scala类。

Employee2.scala

员工2.scala

class Employee2(val empId : Int, val empName:String){	
	println("From Primary Constructor")	
    def this(){
	this(0,null)
	println("From Zero-Argument Auxiliary Constructor")
    }	
    def this( empId : Int){
	this(empId, null)
	println("From One-Argument Auxiliary Constructor")
    }	 
}

Here we have developed a Scala Program with one Primary Constructor and two Auxiliary Constructors.

在这里,我们开发了一个Scala程序,其中包含一个主构造函数和两个辅助构造函数。

Test Employee2.scala:-
To test the about program, we are going to write some sample examples as shown below:

测试Employee2.scala:-
为了测试about程序,我们将编写一些示例示例,如下所示:

Employee2Test1.scala

Employee2Test1.scala

object Employee2Test1 extends App{
  val emp21 = new Employee2  
}

Output:- All my Scala Examples are placed “E:\>” Drive in my Windows machine.

输出:-我所有的Scala示例都放置在Windows计算机中的“ E:\>”驱动器中。

E:\>scalac Employee2.scala
E:\>scalac Employee2Test1.scala
E:\>scala Employee2Test1
From Primary Constructor
From Zero-Argument Auxiliary Constructor

Here we are making a call to Zero-Argument Auxiliary Constructor. It has a call to Primary Constructor so that we are seeing output both from Primary Constructor and Zero-Argument Auxiliary Constructor.

在这里,我们正在调用零参数辅助构造函数。 它调用了主构造函数,因此我们可以同时看到主构造函数和零参数辅助构造函数的输出。

Employee2Test2.scala

Employee2Test2.scala

object Employee2Test2 extends App{
  val emp22 = new Employee2(1001)   
}

Output:- All my Scala Examples are placed “E:\>” Drive in my Windows machine.

输出:-我所有的Scala示例都放置在Windows计算机中的“ E:\>”驱动器中。

E:\>scalac Employee2.scala
E:\>scalac Employee2Test2.scala
E:\>scala Employee2Test2
From Primary Constructor
From One-Argument Auxiliary Constructor

Here we are making a call One-Argument Auxiliary Constructor. It has a call to Primary Constructor so that we are seeing output both from Primary Constructor and One-Argument Auxiliary Constructor.

在这里,我们将调用一个单参数辅助构造函数。 它调用了主构造函数,因此我们可以同时看到主构造函数和单参数辅助构造函数的输出。

Employee2Test3.scala

Employee2Test3.scala

object Employee2Test3 extends App{
  val emp23 = new Employee2(1001, "Scala")   
}

Output:- All my Scala Examples are placed “E:\>” Drive in my Windows machine.

输出:-我所有的Scala示例都放置在Windows计算机中的“ E:\>”驱动器中。

E:\>scalac Employee2.scala
E:\>scalac Employee2Test3.scala
E:\>scala Employee2Test3
From Primary Constructor

Here we are calling Primary Constructor directly so that we are seeing output only from that constructor.

在这里,我们直接调用Primary Constructor,这样我们只能看到该构造函数的输出。

javap output:-

javap输出:-

E:\>scalac Employee2.scala

E:\>javap Employee2.class
Compiled from "Employee2.scala"
public class Employee2 {
  public int empId();
  public java.lang.String empName();
  public Employee2(int, java.lang.String);
  public Employee2();
  public Employee2(int);
}

Important Point to Remember:-
NOTE:-In all examples in this post, Auxiliary Constructors are making a call to Primary Constructor only. However, they can make a call to previous defined other Auxiliary Constructors too.

要记住的重点:
注意:-在本文中的所有示例中,辅助构造函数仅对主构造函数进行调用。 但是,它们也可以调用以前定义的其他辅助构造函数。

Scala辅助构造函数规则 (Scala Auxiliary Constructor Rules)

In Scala Programming, we need to follow these rules to define Auxiliary Constructors:

在Scala编程中,我们需要遵循以下规则来定义辅助构造函数:

  • Like Methods, Auxiliary Constructors are defined by using “def” keyword.

    与方法类似,辅助构造函数是使用“ def”关键字定义的。

  • Like Method Overloading, All Auxiliary Constructors should use same name: “this”.

    与方法重载一样,所有辅助构造函数都应使用相同的名称:“ this”。

  • Each Auxiliary Constructor must have a different signature i.e. Different Parameters list.

    每个辅助构造函数必须具有不同的签名,即“不同参数”列表。

  • Each Auxiliary Constructor must call a previously defined constructor: it may be either Primary Constructor or Auxiliary Constructors. This call should be first statement in that Constructor.

    每个辅助构造函数都必须调用先前定义的构造函数:它可以是主构造函数或辅助构造函数。 此调用应该是该构造方法中的第一个语句。

  • One Auxiliary Constructor calls Primary Constructor or another Auxiliary constructors by using “this” name.

    一个辅助构造函数使用“此”名称调用主构造函数或另一个辅助构造函数。

辅助构造函数的构造函数重载 (Constructor Overloading With Auxiliary constructors)

So for, we have discussed about “How to develop Constructors Overloading using Auxiliary constructors”. But my question is that “Is it possible to provide Constructors Overloading without using Auxiliary constructors”? Is it possible to overload constructors just by using Primary Constructor.

因此,我们讨论了“如何使用辅助构造函数开发构造函数重载”。 但是我的问题是“是否可以在不使用辅助构造函数的情况下提供构造函数重载”? 是否可以仅使用Primary Constructor重载构造函数。

Thanks to Scala’ New Feature. Yes, it is possible. Let us explore this concept in this section.

感谢Scala的新功能。 是的,有可能。 让我们在本节中探讨这个概念。

We can use “Scala’s Default Arguments concept” to solve this problem. Scala has introduced this feature in Scala Version 2.9.

我们可以使用“ Scala的默认参数概念”来解决此问题。 Scala在Scala 2.9版中引入了此功能。

Example:-
Let us re-write same Employee class with Primary Constructor and Default Arguments, without using Auxiliary constructors.

例:-
让我们用主构造函数和默认参数重写相同的Employee类,而不使用辅助构造函数。

Employee3.scala

员工3.scala

class Employee3(val empId : Int = 0, val empName:String = "No-Name"){	
   println("From Primary Constructor")
}

If we execute this example in Scala REPL, we can see the following output:

如果在Scala REPL中执行此示例,则可以看到以下输出:

scala> class Employee3(val empId : Int = 0, val empName:String = ""){
     |    println("From Primary Constructor")
     |    println("Empid = " + (empId == 0))
     |    println("Empname = " + empName.isEmpty)
     | }
defined class Employee3

scala> var e31 = new Employee3
From Primary Constructor
Empid = true
Empname = true
e31: Employee3 = Employee3@78d6692f

scala> var e32 = new Employee3(1003)
From Primary Constructor
Empid = false
Empname = true
e32: Employee3 = Employee3@1e097d59

scala> var e33 = new Employee3(1004,"Scala Play")
From Primary Constructor
Empid = false
Empname = false
e33: Employee3 = Employee3@554e218

We can observe that with only one Primary Constructor, we have implemented “Constructors Overloading”.

我们可以观察到,只有一个主要构造函数,我们实现了“构造函数重载”。

NOTE:-
Please go through my previous post at “Named Parameters and Default Parameter Values In Scala” to learn more about Scala’s Default Parameters concept.

注意:-
请浏览我以前的文章“ Scala中的命名参数和默认参数值”,以了解有关Scala默认参数概念的更多信息。

That’s it all about Scala Auxiliary Constructors. We will discuss “How Scala Constructors work in Inheritance” concept in my coming posts.

这就是Scala辅助构造函数的全部内容。 我们将在我的后续文章中讨论“ Scala构造函数如何在继承中工作”的概念。

Please drop me a comment if you like my post or have any issues/suggestions.

如果您喜欢我的帖子或有任何问题/建议,请给我评论。

翻译自: https://www.journaldev.com/9821/scala-auxiliary-constructors-in-depth

scala 辅助构造函数

    原文作者:cunchi4221
    原文地址: https://blog.csdn.net/cunchi4221/article/details/107477648
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。

相关文章