Scala中的此关键字

Scala这个关键字 (Scala this keyword)

this keyword in Scala is used to refer to the object of the current class. Using this keyword you can access the members of the class like variables, methods, constructors.

Scala中的this关键字用于引用当前类的对象。 使用此关键字,您可以访问类的成员,如变量,方法,构造函数。

In Scala, this keyword can be used in two different ways,

在Scala中,可以两种不同的方式使用此关键字

带(。)点运算符 (With (.) dot operator)

The dot (.) operator in Scala is used to call members of the current class. Both data members and member functions can be called using a dot (.) operator.

Scala中点(。)运算符用于调用当前类的成员。 数据成员和成员函数都可以使用点(。)运算符进行调用。

Syntax:

句法:

    this.member_name;

this will call members associated with the current object of the class. you can call any member associated using dot (.) operator.

将调用与该类的当前对象关联的成员。 您可以使用点(。)运算符调用任何关联的成员。

Example:

例:

class students 
{ 
	var name: String = ""
	var marks = 0
	
	def info(name:String, marks:Int ) 
	{ 
		this.name = name 
		this.marks = marks 
	} 
	def show() 
	{
		println("Student " + name + " has obtained " + marks + " marks " ) 
	} 
} 

object Myobject 
{ 
	def main(args: Array[String]) 
	{ 
		var s1 = new students() 
		s1.info("Kabir", 473) 
		s1.show()
	} 
} 

Output

输出量

Student Kabir has obtained 473 marks 

Explanation:

说明:

The above code is used to display implementation of this keyword with the dot operator. In the program, we have named students which contains some variables. In methods of the student class, we have used with this keyword with the dot operator to call data member of the class.

上面的代码用于通过点运算符显示此关键字的实现 。 在程序中,我们为学生命名,其中包含一些变量。 在学生类的方法中,我们将此关键字与点运算符配合使用,以调用该类的数据成员。

使用this() (Using this())

In Scala programming language, constructors can be called using this keyword. this keyword is used to create the auxiliary constructors and Scala. For an auxiliary constructor, the first line in the constructor should be a call to another constructor to run without error.

在Scala编程语言中,可以使用此关键字调用构造函数。 此关键字用于创建辅助构造函数和Scala。 对于辅助构造函数,该构造函数中的第一行应为对另一个构造函数的调用,以确保无错误运行。

Syntax:

句法:

    this(){

    }

Here, this keyword is used to define a constructor that of the class. The constructor created is an auxiliary constructor that needs to call another auxiliary constructor or primary constructor in its call.

在此, 关键字用于定义该类的构造函数。 创建的构造函数是辅助构造函数,需要在其调用中调用另一个辅助构造函数或主构造函数。

Example:

例:

class students 
{ 
	var name: String = ""
	var marks = 0
	
	def this(name:String, marks:Int ) 
	{ 
		this();
		this.name = name 
		this.marks = marks 
	} 
	def show() 
	{
		println("Student " + name + " has obtained " + marks + " marks " ) 
	} 
} 

object Myobject 
{ 
	def main(args: Array[String]) 
	{ 
		var s1 = new students("Kabir", 473) 
		s1.show()
	} 
} 

Output

输出量

Student Kabir has obtained 473 marks 


翻译自: https://www.includehelp.com/scala/this-keyword-in-scala.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值