scala 环境变量_Scala变量的范围

scala 环境变量

Scala变量范围 (Scala variables scope)

Scope of the variable is the block of code until which the variable can be used within the scope of a variable. Any operation can be performed on it but accessing it outside the scope will give an error.

变量的范围是代码块,直到可以在变量的范围内使用变量为止。 可以对其执行任何操作,但是在范围之外访问它会产生错误。

Example:

例:

    def add(){var sum = 5+13}println(sum)

The above example will give an error as the scope of the sum is within the add() function and program are using it outside which is not allowed.

上面的示例将给出一个错误,因为总和的范围在add()函数之内,并且程序正在外部使用它,这是不允许的。

In Scala programming, there are three types of variables. They are:

在Scala编程中,存在三种类型的变量。 他们是:

  1. Fields

    领域

  2. Method Parameters

    方法参数

  3. Local variables

    局部变量

领域 (Fields)

A field is a variable that is defined in a class ie. variables defined inside the class body are known as fields. A field can be used by any method that is defined inside the class, also so you can use the object of the class to access the fields. Based on their access modifiers the object is allowed to access the fields. These access modifiers allow or disallow any member or object or subclass to access the specified field.

字段是在类(即,类)中定义的变量。 在类主体内部定义的变量称为字段。 字段可由类内部定义的任何方法使用,因此您也可以使用类的对象来访问字段。 根据其访问修饰符,允许对象访问字段。 这些访问修饰符允许或禁止任何成员,对象或子类访问指定字段。

For example:

例如:

For class, there might be a private variable. this private variable can be accessed from any other method of the class but the object of the class will not have access to it.

对于类,可能会有一个私有变量。 可以从该类的任何其他方法访问此私有变量,但该类的对象将无法访问它。

Code:

码:

class Student
{ 
var roll = 2343;
private var totalMarks = 500 // this cannot be accessed by the object
def printinfo(mark : Int) 
{ 
println("The roll no. of student is "+roll)
println("Percentage is "+(mark/totalMarks))
} 
} 
object MyClass
{ 
def main(args:Array[String]) 
{ 
var s1 = new Student
println("call using . operator" + s1.roll)
// println("call using . operator" + s1.totalMarks) // will give error
s1.printinfo(345)	    
} 
} 

Output

输出量

call using . operator2343
The roll no. of student is 2343
Percentage is 0

方法参数 (Method parameters)

Parameters are those variables which are passed to the method while calling the method. These variables can be accessed only inside the method in which they are defined.

参数是在调用方法时传递给方法的那些变量。 这些变量只能在定义它们的方法内部访问。

For example:

例如:

    def add(a:Int , b:Int){
var sum = a+b
}
println(a)

As in this code, the variables a and b are parameters that are passed to the function add(). They cannot be used outside the block of the function. This means the println() line will give an error.

就像在此代码中一样,变量a和b是传递给函数add()的参数 。 不能在功能块之外使用它们。 这意味着println()行将给出错误。

Code:

码:

class Student
{ 
var roll = 2343;
private var totalMarks = 500 // this cannot be accessed by the object
def printinfo(mark : Int) 
{ 
println("The roll no. of student is "+roll)
println("Percentage is "+(mark/totalMarks)) // marks is a parementer passed to method
} 
def printmarks(){
println(mark)
}
} 
object MyClass
{ 
def main(args:Array[String]) 
{ 
var s1 = new Student
println("call using . operator" + s1.roll)
// println("call using . operator" + s1.totalMarks) // will give error
s1.printinfo(345)
} 
} 

Output

输出量


/home/jdoodle.scala:11: error: not found: value markprintln(mark)^
one error found
Command exited with non-zero status 1

局部变量 (Local variables)

Variables are those variables which are r define inside a class And if there scope also lies within the method in which they are defined.

变量是在类内定义的那些变量,如果范围也位于定义它们的方法之内。

For example:

例如:

    def add(a:Int , b:Int){
var sum = a+b
}
println(sum)

In this example, the code tries to access a variable outside its scope. This local variable cannot be used outside of the function.

在此示例中,代码尝试访问其范围之外的变量。 此局部变量不能在函数外部使用。

Code:

码:

class Student
{ 
var roll = 2343;
private var totalMarks = 500 // this cannot be accessed by the object
def printinfo(mark : Int) 
{ 
println("The roll no. of student is "+roll)
val percent = ((mark/totalMarks)*100)
println("Percentage is "+(percent)) // marks is a parementer passed to method
} 
def printmarks(){
println(percent)
}
} 
object MyClass
{ 
def main(args:Array[String]) 
{ 
var s1 = new Student
println("call using . operator" + s1.roll)
// println("call using . operator" + s1.totalMarks) // will give error
s1.printinfo(345)
} 
} 

Output

输出量


/home/jdoodle.scala:12: error: not found: value percentprintln(percent)^
one error found
Command exited with non-zero status 1

翻译自: https://www.includehelp.com/scala/scope-of-scala-variables.aspx

scala 环境变量

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543840.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

使用Eclipse-Maven-git做Java开发(13)--导入git仓库的代码到eclipse

2019独角兽企业重金招聘Python工程师标准>>> 前面讲到了怎么使用osc的git服务进行代码托管。至此,我们已经可以使用git进行文件的版本管理了,甚至可以进行不需要IDE的编程了,但是我们绝大多数时候还是需要IDE的,接下来…

python 三维图直方图_Python | 阶梯直方图

python 三维图直方图A histogram is a graphical technique or a type of data representation using bars of different heights such that each bar groups numbers into ranges (bins or buckets). Taller the bar higher the data falls in that bin. A Histogram is one o…

ExtJS4.2学习(21)动态菜单与表格数据展示操作总结篇2

运行效果&#xff1a; 此文介绍了根据操作左侧菜单在右面板展示相应内容。 一、主页 先看一下跳转主页的方式&#xff1a;由在webapp根目录下的index.jsp跳转至demo的index.jsp 下面是demo的index.jsp的代码 <% page language"java" contentType"text/html; …

数据分析 数据清理_数据清理| 数据科学

数据分析 数据清理数据清理 (Data Cleaning) Data cleaning is the way toward altering information to guarantee that it is right, precise, and significant. The definition may be straightforward, yet information cleaning is utilized in numerous situations. Like…

jQuery之call()方法的使用

最近在做项目时候&#xff0c;写了几行关于DOM操作的代码&#xff0c;在方法中使用了this&#xff0c;在后期重构的时候&#xff0c;想将这段分离出来做成一个方法。 最开始想的很简单&#xff0c;就直接分离出来使用方法名称调用即可。 但是实际操作的时候没有效果&#xff0c…

GMTA的完整形式是什么?

GMTA&#xff1a;伟大的思想一致 (GMTA: Great Minds Think Alike) GMTA is an abbreviation of "Great Minds Think Alike". GMTA是“ Great Minds Think Alike”的缩写 。 It is an expression, which is commonly used in messaging or chatting on social media…

github的使用

GitHub操作总结 : 总结看不明白就看下面的详细讲解. GitHub操作流程 : 第一次提交 : 方案一 : 本地创建项目根目录, 然后与远程GitHub关联, 之后的操作一样; -- 初始化git仓库 :git init ; -- 提交改变到缓存 :git commit -m description ; -- 本地git仓库关联GitHub仓库 : g…

sql更改完整模式报错_SQL的完整形式是什么?

sql更改完整模式报错SQL&#xff1a;结构化查询语言 (SQL: Structured Query Language) SQL is an abbreviation of Structured Query Language. It is a programming language developed and designed for handling structured data in Relational Database Management System…

基于微服务架构,改造企业核心系统之实践

2019独角兽企业重金招聘Python工程师标准>>> 1. 背景与挑战 随着公司国际化战略的推行以及本土业务的高速发展&#xff0c;后台支撑系统已经不堪重负。在吞吐量、稳定性以及可扩展性上都无法满足日益增长的业务需求。对于每10万元额度的合同&#xff0c;从销售团队…

bkg bnc_BNC的完整形式是什么?

bkg bncBNC&#xff1a;刺刀Neill–Concelman (BNC: Bayonet Neill–Concelman) BNC is an abbreviation of "Bayonet Neill–Concelman". BNC是“刺刀Neill–Concelman”的缩写 。 It is also known as "British Naval Connector" or "Bayonet Nut …

使用visio 提示此UML形状所在的绘图页不是UML模型图的一部分 请问这个问题怎么解决?...

解决方法新建->选择软件与数据库模板->选择UML模型图->注意&#xff1a;如果不选择UML模型图的话&#xff0c;可能会出现无法编辑形状文本&#xff0c;提示“此UML形状所在的绘图页不是UML模型图的一部分&#xff0c;该形状设计用于利用UML模型图模板创建的绘图”关注…

iOS之 开发常用到的宏定义

不久前做过一个小项目种用到了就记录下来方便自己以后使用&#xff0c;一个非常实用的宏定义来打印函数名称等 #ifdef DEBUG #define DebugLog(fmt, ...) NSLog(("\n[文件名:%s]\n""[函数名:%s]\n""[行号:%d] \n" fmt), __FILE__, __FUNCTION__,…

agp模式_AGP的完整形式是什么?

agp模式AGP&#xff1a;加速图形端口 (AGP: Accelerated Graphics Port ) AGP is an abbreviation of the "Accelerated Graphics Port". AGP是“加速图形端口”的缩写 。 It was created and developed as a high-speed point-to-point channel for putting togeth…

XCopy命令实现增量备份

xcopy XCOPY是COPY的扩展&#xff0c;可以把指定的目录连文件和目录结构一并拷贝&#xff0c;但不能拷贝系统文件&#xff1b;使用时源盘符、源目标路径名、源文件名至少指定一个&#xff1b;选用/S时对源目录下及其子目录下的所有文件进行COPY。除非指定/E参数&#xff0c;否则…

dbms_DBMS | 并发控制

dbmsManagement of concurrent transaction execution is known as “Concurrency Control”. Transaction management in DBMS handles all transaction, to ensure serializability and isolation of transaction. DBMS implement concurrency control technique so that the…

ruby 发送post请求_使用Ruby发送电子邮件

ruby 发送post请求Ruby发送电子邮件 (Ruby sending email) Sending emails and routing email among mail servers are handled by Simple Mail Transfer Protocol commonly known as SMTP. Net::SMTP class is a predefined class in Ruby’s library which is purposefully d…

Centos Git1.7.1升级到Git2.2.1

Centos Git1.7.1升级到Git2.2.1安装需求&#xff1a;># yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel asciidoc ># yum install asciidoc xmlto -y ># yum install gcc perl-ExtUtils-MakeMaker error: /utf8.c:463: undefined r…

tgc 什么意思 tgt_TGT的完整形式是什么?

tgc 什么意思 tgtTGT&#xff1a;训练有素的研究生老师 (TGT: Trained Graduate Teacher) TGT is an abbreviation of Trained Graduate Teacher. It is a title, not a teaching program that is given to a graduate person who has done completion of training in teaching…

svn的使用(Mac)

2019独角兽企业重金招聘Python工程师标准>>> 从服务器下载代码 在终端中输入svn checkout svn://localhost/mycode --username用户名 --password密码 /Users/apple/Documents/code指令意思&#xff1a;将服务器中mycode仓库的内容下载到/Users/apple/Documents/myCo…

scala语言示例_标有示例的Scala关键字

scala语言示例Scala | 任一关键字 (Scala | Either Keyword) Either is a container similar to the option which has two values, they are referred to as children. The left and right children are named as the right child and left child. 这是一个类似于选项的容器&a…