scala 抽象方法泛型_Scala中的抽象类型与泛型

scala 抽象方法泛型

Scala抽象类型与泛型 (Scala Abstract types vs Generics)

抽象类型 (Abstract type)

Abstract types have abstract members i.e. their members do not have the definition or the values. Traits and abstract classes are abstract type variables.

抽象类型具有抽象成员,即其成员没有定义或值。 特性和抽象类是抽象类型变量。

For example,

例如,

    trait  trai { 
        type T
        val variable : T
    }

泛型 (Generics)

Generic classes are those classes that have classes or other types as parameters.

通用类是具有类或其他类型作为参数的那些类。

    class division[d]{
	    def division(u:z, v:z):z =>{
		    z=u/v;
	    }
    }
    // Here, division is a generic that accept type z values

抽象类型和泛型之间的区别 (difference between Abstract types and Generics)

Some points of the difference between Abstract types and Generics in Scala...

Scala中的抽象类型和泛型之间的一些区别...

  • Abstract type in implement or extended in a Scala Program whereas Generics take a class type as the parameter.

    在实现中或在Scala程序中扩展的抽象类型,而泛型则将类类型作为参数。

  • In Abstract type methods do not have definitions whereas the generics use definition and use other types.

    在Abstract类型方法中没有定义,而泛型使用定义并使用其他类型。

  • Abstract type uses a 'has-a' relation and generics use an 'of' relation.

    抽象类型使用“ has-a”关系,泛型使用“ of”关系。

  • Example of an abstract type, Baleno is a car.

    Baleno是一种抽象类型的示例,它是一辆汽车

    Example of a generic type,

    通用类型的示例,

    array of maps.

    地图数组

In Scala, an abstract class or a trait can be abstract type whereas a class that is abstract or simple can be generic if it accepts any type.

在Scala中, 抽象类或特征可以是抽象类型,抽象类或简单类可以接受任何通用类型。

A class can be abstract as well as generic also, as both concepts can work together as well as individually. You can create a Scala class that generic and abstract as:

一个类既可以是抽象的,也可以是泛型的,因为这两个概念可以一起工作,也可以单独工作。 您可以创建一个通用的和抽象的Scala类:

    abstract class product[x] {
	    def product(a:x , b:x ): x 
    }

The method to this class has no definition which means it is abstract and the class uses data of type x which means it is a generic one.

此类的方法没有定义,这意味着它是抽象的,并且该类使用类型为x的数据,这意味着它是通用的。

翻译自: https://www.includehelp.com/scala/abstract-types-vs-generics-in-scala.aspx

scala 抽象方法泛型

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值