scala 类中的对象是类_Scala中的类和对象

scala 类中的对象是类

Scala中的课程 (Classes in Scala)

A class is a blueprint for objects. It contains the definition of all the members of the class. There are two types of members of the class in Scala,

类是对象的蓝图。 它包含该类的所有成员的定义。 Scala中有两种类型的班级成员,

  1. Fields: the variables in Scala that are used to define data in the class.

    字段: Scala中用于定义类中数据的变量。

  2. Methods: The functions of a class that are used to manipulate the fields of the class and do some stuff related to the functioning of a class.

    方法:类的功能,用于操纵类的字段并做一些与类的功能有关的事情。

Usage of a class member to the outer worlds is limited and can be excessed by only two ways,

类成员对外部世界的使用是有限的,并且只能通过两种方式加以超越:

  1. Inheritance: Property by which members of one class are used by child class which inherits it.

    继承:继承该子类的子类使用一个类的成员的属性。

  2. Objects: It is creating instances of a class to use its members.

    对象:它正在创建类的实例以使用其成员。

类的例子 (Example of a class)

We will use the classical student class for the explanation of class creation in Scala. Here, we have a class student, with fields: roll no, name, and percentage. There are methods to this class: getpercentage(), printresult().

我们将使用经典的学生课堂来解释Scala中课堂创建 。 在这里,我们有一个班级学生 ,具有以下字段: 卷号 , 名称和百分比 。 此类有一些方法: getpercentage() , printresult() 。

The blueprint is shown in the below figure...

蓝图如下图所示。

Syntax of class in Scala:

Scala中类的语法:

Class student{
	// Class Variables
	var rollno;
	var name : string;
	var percentage; 

	//Class methods…
	def getpercentage(int percent){
		percentage = percent;
	}
	
	def printresult(){
		print("Roll number : " + rollno);
		print("\nName  : "+ name);
		print("\nHas scored " + percentage + "% and is ");
		if(percentage > 40)
			print("passed")
		else 
			print("failed")
	}
}

Syntax explanation:

语法说明:

The about is a code snippet to declare a class in Scala,

about是在Scala中声明类的代码段,

First, the class is declared keyword class is used to create a class followed by the name of the class. Next is the definition of the class members, the class contains three members all are public (because of var declaration). It also contains two member functions(methods) that are declared using def keyword (no need of return type), followed by the name of the function and then within the "(" are the arguments that are passed when the function is called.

首先,该类被声明为关键字class ,用于创建一个类,后跟该类的名称。 接下来是类成员的定义,该类包含三个成员,所有成员都是公共的(由于var Declaration )。 它还包含两个成员函数(方法),这些成员函数使用def关键字(不需要返回类型)声明,然后是函数名称,然后在“(”内)是调用函数时传递的参数。

classes in scala

Scala的主要构造函数 (Primary Constructor in Scala )

There is a new declaration of class in Scala, and you will see it very it is more efficient than the classical one.

Scala中有一个新的类声明,您会发现它比经典的声明效率更高。

It is the use of primary constructor definition in Scala.

它在Scala中使用了主要的构造函数定义。

    class student (var rlno , var stname){
	    var rollno = rlno;
	    var name = stname; 
    }

Explanation:

说明:

Here the class body is acting as a constructor and is used to initialize the values of fields.

在这里,类主体充当构造函数,并用于初始化字段的值。

This is all about classes in Scala we will learn about objects, their creation, and usage in the next tutorial.

这一切都与Scala中的类有关,我们将在下一个教程中了解对象,对象的创建和用法。

翻译自: https://www.includehelp.com/scala/classes-and-objects-in-scala.aspx

scala 类中的对象是类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值