当前位置:首页 > 服务端 > Scala 函数式程序设计原理(3)--Data and Abstraction

Scala 函数式程序设计原理(3)--Data and Abstraction

3.1 Class Hierarchies

Object-oriented languages (including Scala) implement dynamic method dispatch.

This means that the code invoked by a method call depends on the runtime type of the object that contains the method.

 

3.2 How Classes Are Organized

Classesand Objects are organized in packages.

To place a class or object inside a package, use a package clause at the top of your source file.

package progfun.example

object Hello { ... }

This would place Hello in the package progfun.examples. You can then refer to Hello by its fully qualified name progfun.examples.Hello. For instance, to run the Hello program.

Nothing is at the bottom of Scala's type hierarchy. It is a subtype of every other type.

There is no value of type Nothing.

Why is that useful?

  • To signal abnormal termination
  • As an element type of empty collections

Every reference class type alss has null as a value.

The type of null is Null.

Null is a subtype of every class that inherits from Objects; it is incompatible with subtypes of AnyVal.

 

3.3 Polymorphism

Type parameterization means that classes as well as methods can now have types as parameters.

Type parameters do not affect evaluation in Scala.

We can assume that all type parameters and type arguments are removed before evaluating the program.

This is also called type erasure.

Language that use type erasure include Java, Scala, Haskell, Ml, OCaml.

Some other languages keep the type parameters around at run time, these include C++, C#, F#.

Polymorghism means that a function type comes "in many forms".

In programming it means that:

  • the function can be applied to arguments of many types, or
  • tye type can have instances of many types.

We have seen two principal forms of polymorphism:

  • subtyping: instances of a subclass can be passed to a base class
  • generics: instances of a function or class are created by type parameterization.
  • trait List[T] {
      def isEmpty: Boolean
      def head: T
      def tail: List[T]
      def nth(n: Int): T
    }
    class Cons[T](val head:T, val tail: List[T]) extends List[T] {
      def isEmpty = false
      def nth(n: Int) = {
        if(n < 0) throw new IndexOutOfBoundsException("index less than 0")
        else if(n == 0) head
        else tail nth(n-1)
      }
    }
    class Nil[T] extends List[T] {
      def isEmpty = true
      def head: Nothing = throw new NoSuchElementException("Nil.head")
      def tail: Nothing = throw new NoSuchElementException("Nil.tail")
      def nth(n: Int) = throw new IndexOutOfBoundsException("Nil")
    }

     

来源链接:https://www.cnblogs.com/PaulingZhou/p/6866513.html

版权声明:
1、Java侠(https://www.javaxia.com)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaxia.com/server/125202.html

分享给朋友:

“Scala 函数式程序设计原理(3)--Data and Abstraction” 的相关文章