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 Auxil