scala什么是单例对象_Scala单例和伴侣对象

scala什么是单例对象

Scala中的单例对象 (Singleton objects in Scala)

Scala being an object-oriented programming language has the capability to create an object that can define its members without a class.

Scala是一种面向对象的编程语言,它具有创建对象的能力,该对象无需类即可定义其成员。

A singleton Object has no class associated with it i.e. It exists without any class. It is defined using the object keyword not the class keyword and is an instance, not a blueprint hence it does not require an external call to execute its methods.

单例对象没有与之关联的类,即它不存在任何类。 它是使用object关键字而不是class关键字定义的,并且是一个实例,不是一个蓝图,因此它不需要外部调用即可执行其方法。

Why Singleton Object?

为什么选择单例对象?

Every program needs a point from where the execution starts. In OOPS classes need objects to get executed. But the main() method needs to be executed first to call other members of the class.

每个程序都需要一个从执行开始的点。 在OOPS中,类需要对象才能执行。 但是需要首先执行main()方法才能调用该类的其他成员。

For executing the main() method in scala many object-oriented programming languages use the static keyword but it scala programming language there is no static keyword. That is why in scala we use a singleton object that defines the main method.

为了在scala中执行main()方法,许多面向对象的编程语言都使用static关键字,但是scala编程语言中没有static关键字。 这就是为什么在Scala中我们使用定义了main方法的单例对象。

单例对象 (Singleton objects)

An object that can exist without a class is a singleton object. It objects keyword is used to define it.

没有类就可以存在的对象是单例对象。 It objects关键字用于定义它。

Syntax:

句法:

    object singleton_objname {
	    // object code , member functions and fields. 
    }

单例对象的特征 (Features of singleton object)

  • Created using object keyword.

    使用object关键字创建。

  • Members are global i.e can be called from anywhere in the program.

    成员是全局成员,即可以从程序中的任何位置调用成员。

  • Creation of instance is not possible in case of singleton objects.

    如果是单例对象,则无法创建实例。

  • Inheritance is allowed, i.e. it can extend class and traits.

    允许继承,即继承可以扩展类和特征。

  • To excess members of Singleton object, we will use the singleton object's name dot members name.

    对于Singleton对象的多余成员,我们将使用Singleton对象的名称点成员名称。

    For example,

    例如,

         Singleton_objname.member;

Program to show implementation of singleton object

显示单例对象实现的程序

object findsum{
    var a = 56
    var b = 21
    def sum(): Int ={
        return a+b;
    }
}
object Main 
{ 
    def print(){
        printf("The sum is : "+ findsum.sum());
    }
	def main(args: Array[String]) 
	{ 
        print();
	} 
} 

Output

输出量

The sum is : 77

Explanation:

说明:

This program is to illustrate the use of singleton objects in Scala. The program is to find the sum of given numbers and return it. There are 2 objects findsum and Main. The findsum object calculates the sum of the two numbers with the sum method. And the Main object has two methods print() and main(). The print method, prints the value that is returned by sum() method.

该程序用于说明Scala单例对象的用法。 该程序将查找给定数字的总和并将其返回。 有2个对象findsum和Main 。 findsum对象使用sum方法计算两个数字的和 。 Main对象有两种方法print()和main() 。 print方法打印由sum()方法返回的值 。

Scala中的同伴对象 (Companion object in Scala)

If a class and a singleton object have the same name. Then, the class is called a companion class and singleton object is called a singleton object.

如果类和单例对象具有相同的名称。 然后,该类称为同伴类,singleton对象称为singleton对象

Both the class and object are defined in the same program file.

类和对象都在同一程序文件中定义。

Program to show implementation of companion object

显示伴随对象的实现的程序

class companion{  
    var a = 23;
    var b = 78;
    def sum(){
        println("The sum is: "+ (a+b));
    }
}  

object companion{  
    def main(args:Array[String]){  
        new companion().sum(); 
    }  
}  

Output

输出量

The sum is: 101

Explanation:

说明:

This program is to illustrate the concept of a companion object. Here, the program is used to find the sum of the given integer. To calculate the sum we have a method sum in companion class. We will call this method from the companion object using the dot operator.

该程序用于说明伴侣对象的概念。 在这里,该程序用于查找给定整数的总和。 为了计算总和,我们在伴随类中有一个方法总和。 我们将使用点运算符从伴随对象中调用此方法。

翻译自: https://www.includehelp.com/scala/singleton-and-companion-objects.aspx

scala什么是单例对象

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值