scala case,scala 主構造器_Scala主構造器深度

 2023-11-19 阅读 20 评论 0

摘要:scala 主構造器In this post, we are going to discuss about Scala Primary Constructor in depth with real-time scenario examples. 在這篇文章中,我們將通過實時場景示例深入討論Scala主構造器。 發表簡短目錄 (Post Brief TOC) Introduction 介紹 Primary Constr

scala 主構造器

In this post, we are going to discuss about Scala Primary Constructor in depth with real-time scenario examples.

在這篇文章中,我們將通過實時場景示例深入討論Scala主構造器。

發表簡短目錄 (Post Brief TOC)

  • Introduction

    介紹
  • Primary Constructor in Scala

    Scala的主要構造函數
  • Scala val and var in-brief

    Scala Val和var簡要介紹
  • Scala Primary Constructor With val and var

    Scala具有val和var的主要構造函數
  • Scala Primary Constructor in-brief

    Scala主要構造函數簡述

介紹 (Introduction)

As we know, Constructor is used to create instances of a class. Scala supports constructors in different way than in Java.

眾所周知,構造函數用于創建類的實例。 Scala以與Java不同的方式支持構造函數。

scala case?In Scala Language, a class can have two types of constructors:

在Scala語言中,一個類可以具有兩種類型的構造函數:

  • Primary Constructor

    主要建設者
  • Auxiliary Constructor

    輔助構造器

A Scala class can contain only Primary Constructor or both Primary Constructor and Auxiliary Constructors. A Scala class can contain one and only one Primary constructor, but can contain any number of Auxiliary constructors. We will discuss Primary Constructor in-detail in this post and Auxiliary Constructor in-detail in my coming post.

Scala類只能包含主構造函數,也可以包含主構造函數和輔助構造函數。 一個Scala類可以包含一個并且只能包含一個Primary構造函數,但是可以包含任意數量的Auxiliary構造函數。 我們將在這篇文章中詳細討論主要構造函數,而在我的后續文章中將討論輔助構造函數的細節。

Before going to next sections, we need to understand Class Definition and Class Body as shown in the diagram below:

在進入下一部分之前,我們需要了解類定義和類主體,如下圖所示:

scala. 動態二維數組。Class Body is defined with two curly braces “{ }”. First line is Class Definition.

類主體用兩個大括號“ {}”定義。 第一行是類定義。

Scala的主要構造函數 (Primary Constructor in Scala)

In Scala, a Primary Constructor is a Constructor which starts at Class definition and spans complete Class body.

在Scala中,主要構造函數是一個從Class定義開始并跨越完整Class主體的構造函數。

We can define Primary Constructor with zero or one or more parameters. Now, we will discuss them one by one with some simple examples.

我們可以使用零個或一個或多個參數定義主構造函數。 現在,我們將通過一些簡單的示例逐一討論它們。

如何提高路面的構造深度。Example-1:-Create a Person class with default Primary Constructor.

示例1:-使用默認的Primary Constructor創建一個Person類。

class Person{// Class body goes here
}

Here we have defined No-Argument or Zero-Arguments constructor like “Person()”. It is also known as “Default Constructor” in Java.

在這里,我們定義了無參數或零參數構造函數,如“ Person()” 。 在Java中也稱為“默認構造函數”。

We can create an instance of Person class as shown below:

我們可以創建一個Person類的實例,如下所示:

val p1 = new Person()var p2 = new Person

scala主構造器?Both are valid in Scala. We can use No-Arguments constructor without parenthesis.

兩者均在Scala中有效。 我們可以使用不帶括號的No-Arguments構造函數。

Example-2:-
Primary Constructor’s parameters are declared after the class name as shown below:

示例2:-
在類名之后聲明主要構造函數的參數,如下所示:

class Person(firstName:String, middleName:String, lastName:String){// Class body goes here
}

Here Person class’s Primary Constructor has three parameters: firstName, middleName and lastName.

在此,Person類的主要構造函數具有三個參數:firstName,middleName和lastName。

scala object?Now, We can create an instance of Person class as shown below:

現在,我們可以創建一個Person類的實例,如下所示:

val p1 = new Person("First","","Last")

If we observe this example, some People may have Middle Name or not but still they have to provide all 3 parameters. It is not efficient way to deal with constructors in Scala. We can use Auxiliary Constructors to solve this problem (Please go through my next post).

如果我們觀察該示例,則某些人員可能具有中間名,但仍必須提供所有3個參數。 這不是在Scala中處理構造函數的有效方法。 我們可以使用輔助構造函數來解決此問題(請閱讀我的下一篇文章)。

Example-3:-
Anything we place within the Class Body other than Method definitions, is a part of the Primary Constructor.

示例3:-
除了方法定義外,我們放在類主體中的所有內容都是主構造函數的一部分。

class Person(firstName:String, middleName:String, lastName:String){println("Statement 1")def fullName() = firstName + middleName + lastNameprintln("Statement 2")
}

構造深度尺寸。When we execute above program in Scala REPL, we can get the following output:

當在Scala REPL中執行上述程序時,我們將獲得以下輸出:

scala> var p1 = new Person("Ram","","Posa")
Statement 1
Statement 2
p1: Person = Person@3eb81efb

If we observe that output, as both println statements are defined in Class Body, they become the part of Primary Constructor.

如果我們觀察到該輸出,因為兩個println語句都在類主體中定義,則它們將成為主構造函數的一部分。

Example-4:-:-
Any Statements or Loops (like If..else, While,For etc) defined in the Class Body also become part of the Primary Constructor.

示例4:- :-
類主體中定義的任何語句或循環(如If..else,While,For等)也將成為主構造函數的一部分。

class Person(firstName:String, middleName:String, lastName:String){def fullName() = firstName + middleName + lastNameif (middleName.trim.length ==0)println("Middle Name is empty.")
}

scala元組。Output:-

輸出:-

scala> var p1 = new Person("Ram","","Posa")
Middle Name is empty.
p1: Person = Person@64a40280

Example-5:-:-
Not only Statements or Expressions, any method calls defined in the Class Body also become part of the Primary Constructor.

示例5:- :-
不僅是語句或表達式,在類主體中定義的任何方法調用也將成為主構造函數的一部分。

class Person(firstName:String, middleName:String, lastName:String){def fullName() = firstName + middleName + lastNamefullName   // A No-argument Method Call
}

Output:-

輸出:-

scala> var p1 = new Person("Ram","-","Posa")
Ram-Posa
p1: Person = Person@64a40280

Scala Val和var簡要介紹 (Scala val and var in-brief)

主變構造,Before discussing about Scala Primary Constructor, we need to revisit about Scala Field definitions concept.

在討論Scala主構造函數之前,我們需要重新了解Scala字段定義概念。

In Scala, “val” and “var” are used to define class fields, constructor parameters, function parameters etc.

在Scala中,“ val”和“ var”用于定義類字段,構造函數參數,函數參數等。

  • “val” means value that is constant. “val” is used to define Immutable Fields or variables or attributes.

    “ val”是指恒定的值。 “ val”用于定義不可變字段或變量或屬性。
  • Immutable fields means once we create we cannot modify them.

    不可變字段表示一旦創建,便無法對其進行修改。
  • “var” means variable that is NOT constant. “var” is used to define Mutable Fields or variables or attributes.

    “ var”表示不是恒定的變量。 “ var”用于定義可變字段或變量或屬性。
  • Mutable fields means once we create, we can modify them.

    可變字段表示創建后就可以對其進行修改。

Scala具有val和var的主要構造函數 (Scala Primary Constructor With val and var)

In Scala, we can use val and var to define Primary Constructor parameters. We will discuss each and every scenario with simple examples and also observe some Scala Internals.

在Scala中,我們可以使用val和var來定義Primary Constructor參數。 我們將通過簡單的示例討論每種情況,并觀察一些Scala內部原理。

scala java、We have defined three different Scala sources files as shown below:

我們定義了三個不同的Scala源文件,如下所示:

Example-1:-
In Scala, if we use “var” to define Primary Constructor’s parameters, then Scala compiler will generate setter and getter methods for them.

示例1:-
在Scala中,如果我們使用“ var”定義主要構造函數的參數,則Scala編譯器將為其生成setter和getter方法。

Person1.scala

Person1.scala

class Person1(var firstName:String,var middleName:String, var lastName:String)

主述結構、Open command prompt at Source Files available folder and compile “Person1.scala” as shown below:

在“源文件”可用文件夾中打開命令提示符,并編譯“ Person1.scala”,如下所示:

This step creates “Person1.class” file at same folder. “javap” command is the Java Class File Disassembler. Use this command to disassemble “Person1.class” to view its content as shown below:

此步驟在同一文件夾中創建“ Person1.class”文件。 “ javap”命令是Java類文件反匯編程序。 使用此命令反匯編“ Person1.class”以查看其內容,如下所示:

As per this output, we can say that “var” is used to generate setter and getter for constructor parameters.

根據此輸出,我們可以說“ var”用于生成構造函數參數的setter和getter。

scala object和class區別。As per Scala Notation, setter and getter methods for firstName Parameter:

按照Scala表示法,firstName參數的setter和getter方法:

Getter Method

吸氣法

public java.lang.String firstName();

Setter Method

設置方法

public void firstName_$eq(java.lang.String);

構造深度是什么意思。This “firstName_$eq” method name is equal to “firstName_=”. When we use “=” in Identifiers Definition(Class Name, Parameter Name, Method names etc.), it will automatically convert into “$eq” Identifier by Scala Compiler.

此“ firstName_ $ eq”方法名稱等于“ firstName_ =”。 當我們在標識符定義(類名稱,參數名稱,方法名稱等)中使用“ =”時,它將由Scala編譯器自動轉換為“ $ eq”標識符。

NOTE:-
Scala does not follow the JavaBeans naming convention for accessor and mutator methods.

注意:-
Scala的訪問器和更改器方法不遵循JavaBeans命名約定。

Example-2:-
In Scala, if we use “val” to define Primary Constructor’s parameters, then Scala compiler will generate only getter methods for them.

示例2:-
在Scala中,如果我們使用“ val”來定義主要構造函數的參數,則Scala編譯器將僅為其生成getter方法。

構造深度設計值。Person2.scala

Person2.scala

class Person1(val firstName:String,val middleName:String, val lastName:String)

Open command prompt, compile and use “javap” to disassemble to see the generated Java Code as shown below:

打開命令提示符,進行編譯并使用“ javap”進行反匯編以查看生成的Java代碼,如下所示:

If we observe this output, we can say that “val” is used to generate only getter for constructor parameters.

如果觀察到此輸出,則可以說“ val”僅用于生成構造函數參數的getter。

django常用組件、As per Scala Notation, getter methods for firstName, middleName and lastName Parameters:

按照Scala表示法,firstName,middleName和lastName參數的getter方法:

Getter Methods

吸氣方法

public java.lang.String firstName();
public java.lang.String middleName();
public java.lang.String lastName();

Example-3:-
In Scala, if we don’t use “var” and “val” to define Primary Constructor’s parameters, then Scala compiler does NOT generate setter and getter methods for them.

示例3:-
在Scala中,如果我們不使用“ var”和“ val”來定義主要構造函數的參數,則Scala編譯器不會為它們生成setter和getter方法。

主變結構圖、Person3.scala

Person3.scala

class Person1(firstName:String,middleName:String, lastName:String)

Open command prompt, compile and use “javap” to disassemble to see the generated Java Code as shown below:

打開命令提示符,進行編譯并使用“ javap”進行反匯編以查看生成的Java代碼,如下所示:

If we observe this output, we can say that no setter and getter methods are generated for firstName, middleName and lastName Constructor Parameters.

如果觀察到此輸出,則可以說沒有為firstName,middleName和lastName構造函數參數生成setter和getter方法。

Scala主要構造函數簡述 (Scala Primary Constructor in-brief)

java 構造器、The Scala Primary Constructor consist of the following things:

Scala主要構造函數包括以下內容:

  • The constructor parameters declare at Class Definition

    構造函數參數在類定義中聲明
  • All Statements and Expressions which are executed in the Class Body

    在類主體中執行的所有語句和表達式
  • Methods which are called in the Class Body

    在類主體中調用的方法
  • Fields which are called in the Class Body

    在類主體中調用的字段

In Simple words, anything defined within the Class Body other than Method Declarations is a part of the Scala Primary Constructor.

用簡單的話來說,在類主體中定義的任何方法(方法聲明除外)都是Scala主要構造函數的一部分。

That’s it all about Scala Primary Constructor. We will discuss Scala Auxiliary Constructors in-depth in coming posts.

關于Scala主要構造函數的所有內容。 我們將在以后的文章中深入討論Scala輔助構造函數。

影響構造深度的因素,Please drop me a comment if you like my post or have any issues/suggestions.

如果您喜歡我的帖子或有任何問題/建議,請給我評論。

翻譯自: https://www.journaldev.com/9810/scala-primary-constructor-indepth

scala 主構造器

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

原文链接:https://hbdhgg.com/1/183315.html

发表评论:

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

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

底部版权信息