scala 线性回归_Scala的特征线性化

scala 线性回归

Scala | 特性线性化 (Scala | Trait Linearization)

In Scala programming language, trait linearization is a property that helps to rectify ambiguity when instances of a class that are defined using multiple inheritances from different classes and traits are created. 

在Scala编程语言中, 特征线性化是一种属性,当创建使用来自不同类和特征的多个继承定义的类的实例时,该属性有助于纠正歧义。

It resolves ambiguity that may arise when class or trait inherits property from 2 different parents (they may be classes or traits).

它解决了当类或特征从2个不同的父代(它们可能是特征 )继承财产时可能出现的歧义。

Syntax:

句法:

trait t1{}
class c1{}
class main{}
object obj1 = new class main extents c1 with t1

Here linearization will make the inheritance structure clear so that no problem could arise in the future.

在这里线性化将使继承结构清晰明了,以便将来不会出现任何问题。

Here, we will consider two root classes, AnyRef root for all reference types. Any root for all classes in Scala.

在这里,我们将考虑两个根类, 所有引用类型的AnyRef根。 Scala中所有类的任何根。

线性化 (Linearization)

t1 -> AnyRef -> Any
c1 -> AnyRef -> Any
main -> AnyRef -> Any
obj1 -> main -> t1 -> c1 -> AnyRef -> Any

Here the linearization will go in the following order: main class -> t1 trait -> c1 class -> AnyRef -> Any

这里线性化将按照以下顺序进行: 主类-> t1特征-> c1类-> AnyRef-> Any

This sample program will make the concept more clear,

该示例程序将使概念更清晰,

class vehicle 
{ 
	def method: String= "vehicle "
} 

trait bike extends vehicle 
{ 
	override def method: String = "Bike-> "+ super.method 
} 

trait muscleBike extends vehicle 
{ 
	override def method: String = "Muscle Bike -> "+ super.method 
} 

trait harley extends vehicle 
{ 
	override def method: String ="Harley Davidson-> "+ super.method 
} 

class iron extends bike with
muscleBike with harley 
{ 
	override def method: String = "Iron 833 -> "+ super.method 
} 

object myObject 
{ 
	def main(args: Array[String]) 
	{ 
		var myBike = new iron 
		println(myBike.method) 
	} 
} 

Output

输出量

Iron 833 -> Harley Davidson-> Muscle Bike -> Bike-> vehicle 

特征线性化的特征 (Features of trait linearization)

  • It is used to solve ambiguity in Scala which arises in the case of multiple inheritances.

    它用于解决Scala中的多重继承时出现的歧义。

  • The calling of a super method by subclasses is managed using stack.

    子类对super方法的调用是使用堆栈管理的。

  • Linearization comes into play when a new class instance is created.

    当创建一个新的类实例时,线性化就起作用了。

  • Linearization may not be the same as inherited mixins because mixins are defined by the programmer.

    线性化可能与继承的mixin不同,因为mixin由程序员定义。

  • Linearization avoids repeated inheritance and if we explicitly try to add a class to inheritance an error will be thrown.

    线性化避免了重复的继承,如果我们显式地尝试向继承中添加类,则会抛出错误。

翻译自: https://www.includehelp.com/scala/trait-linearization.aspx

scala 线性回归

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值