scala 官网文档---notes(1)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_40042143/article/details/84671896

scala类型的层次结构如下图:

最顶层Any有两个子类:AnyVal和AnyRef

AnyVal:表示值类型的

     Unit:不包含任何有意义的信息的值类型

AnyRef:表示引用类型的

      所有非值类型都定义为引用类型

值之间的转换关系如下图,单向的


class 声明

Scala中class的定义

scala是一类面向对象的程序语言,强类型。

如下图:

通过class声明类Point,其包含4个成员,两个成员变量x,y,两个成员函数move和toString.

类在使用的时候需要实例化对象  关键词  new

构造方法在声明的时候可以有默认值

私有成员和getter\setter的设置 

私有成员 _x,_y通过getter和setter进行访问和设置值


Traits

Traits are used to share interfaces and fields between classes. (用于类之前接口和字段的共享)They are similar to Java 8’s interfaces. Classes and objects can extend traits but traits cannot be instantiated and therefore have no parameters.

trait的声明

关键词 trait

Extending the trait Iterator[A] requires a type A and implementations of the methods hasNext and next

父类和子类

Pet含有抽象字段name,子类Cat,Dog在构造器里面需要实现抽象字段


tuples:类

a tuple is a class that can hold elements of different types(可以含有不同类型的元素)

Desstructuring重构tuple data

模式匹配


混合类的合成CLASS COMPOSITION WITH MIXINS

Mixins:多态,类具有多态的特性


高阶函数

将其他函数作为参数或返回结果为一个函数

猜你喜欢

转载自blog.csdn.net/weixin_40042143/article/details/84671896
今日推荐