zoukankan      html  css  js  c++  java
  • [Scala] Scala基础知识

    Object 

    1. An object is a type of class that can have no more than one instance, known in object-oriented design as a singleton. Instead of creating an instance with a new keyword, just access the object directly by name. 
    2. Objects provide similar "static" and "global" functionality but decouple them from instantiable classes. 
    3. Objects do not take any parameters (they are automatically instantiated), but you can define the same fields, methods, and internal classes as you can with regular classes. 不需要传入任何参数,因为你可以直接访问object里面的内容

    Companion Object

    1. A companion object is an object with the same name as a class or trait and is defined in the same source file as the associated file or trait. 
    2. A companion object differs from other objects as it has access rights to the class/trait that other objects do not.
    3. A companion object is as a factory for creating instances of the class. 
    4. An analog to a companion object in Java is having a class with static methods. In Scala you would move the static methods to a Companion object.
    5. When a case-class is declared a companion object is created for the case-class with a factory method that has the same signature as the primary constructor of the case class

    NOTE: Because the companion object and the class must be defined in the same source file you cannot create them in the interpreter.

    Case Class

    Case classes differ from regular classes in that they get:

    1. pattern matching support 支持模式匹配
    2. default implementations of equals and hashCode 默认实现equals和hashCode
    3. default implementations of serialization 默认实现序列化
    4. a prettier default implementation of toString, and 默认更好实现toString
    5. the small amount of functionality that they get from automatically inheriting from scala.Product.

    NOTE: Pattern matching, equals and hashCode don't matter much for singletons (unless you do something really degenerate), so you're pretty much just getting serialization, a nice toString, and some methods you probably won't ever use.

  • 相关阅读:
    windows 乱码之 gbk 与 cp936
    jdcli 在命令行反编译jar包
    建议博客园向独立博客提供发布到首页的服务
    IsByRef在什么情况下为true?
    Hibernate里自定义UserType时取不到值的问题
    解决安装Visual Studio 2010 SP1时被NDP40KB2468871.exe补丁卡死以及mscorsvw.exe进程CPU占用率高的问题
    FROM WAS7/JDK5 TO WAS6/JDK4
    C++山寨C#中的DataTable
    程序员的自我修养读书笔记
    Web开发之路
  • 原文地址:https://www.cnblogs.com/qingwen/p/5160909.html
Copyright © 2011-2022 走看看