scala. 動態二維數組,scala 輔助構造函數_Scala輔助構造函數的深入介紹

 2023-11-19 阅读 21 评论 0

摘要:scala 輔助構造函數Before reading this post, please read my previous post about “Scala Primary Constructor In-Depth”. 在閱讀這篇文章之前,請閱讀我之前關于“ Scala Primary Constructor Inpth”的文章 。 發表簡短目錄 (Post Brief TOC) Introduction 介紹

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.scalaE:\>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.scalaE:\>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 Employee3scala> var e31 = new Employee3
From Primary Constructor
Empid = true
Empname = true
e31: Employee3 = Employee3@78d6692fscala> var e32 = new Employee3(1003)
From Primary Constructor
Empid = false
Empname = true
e32: Employee3 = Employee3@1e097d59scala> 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 輔助構造函數

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/183265.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息