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.

  • 相关阅读:
    在Ubuntu 20.04 LTS Focal Fossa上安装Deluge
    如何使用命令行快速检查Linux系统的版本
    如何在Debian 10上安装NVM
    如何在CentOS 8上安装Apache ActiveMQ
    如何在Linux中引导时列出启动服务?
    如何检查Linux Mint 20磁盘错误的方法
    如何在Firewalld中打开特定IP地址的端口?
    如何在CentOS 8服务器安装oVirt开源虚拟化管理系统
    如何在Centos 8服务器上安装LogAnalyzer?
    如何在Debian中使用apt从命令行安装程序
  • 原文地址:https://www.cnblogs.com/qingwen/p/5160909.html
Copyright © 2011-2022 走看看