1. Trang chủ
  2. » Công Nghệ Thông Tin

Scala for Java Developers A Practical Primer

246 16 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Nội dung

Scala for Java Developers A Practical Primer — Toby Weston Scala for Java Developers A Practical Primer Toby Weston Scala for Java Developers: A Practical Primer Toby Weston London, United Kingdom ISBN-13 (pbk): 978-1-4842-3107-4 https://doi.org/10.1007/978-1-4842-3108-1 ISBN-13 (electronic): 978-1-4842-3108-1 Library of Congress Control Number: 2017963118 Copyright © 2018 by Toby Weston This work is subject to copyright All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed Trademarked names, logos, and images may appear in this book Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made The publisher makes no warranty, express or implied, with respect to the material contained herein Cover image by Freepik (www.freepik.com) Managing Director: Welmoed Spahr Editorial Director: Todd Green Acquisitions Editor: Steve Anglin Development Editor: Matthew Moodie Technical Reviewer: Jeff Friesen Coordinating Editor: Mark Powers Copy Editor: Francesca Louise White Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013 Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springersbm.com, or visit www.springeronline.com Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc) SSBM Finance Inc is a Delaware corporation For information on translations, please e-mail rights@apress.com, or visit http://www.apress.com/ rights-permissions Apress titles may be purchased in bulk for academic, corporate, or promotional use eBook versions and licenses are also available for most titles For more information, reference our Print and eBook Bulk Sales web page at http://www.apress.com/bulk-sales Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/9781484231074 For more detailed information, please visit http://www.apress.com/source-code Printed on acid-free paper In memory of Félix Javier García López Table of Contents About the Author��������������������������������������������������������������������������������������������������� xiii About the Technical Reviewer���������������������������������������������������������������������������������xv Acknowledgments�������������������������������������������������������������������������������������������������xvii Preface�������������������������������������������������������������������������������������������������������������������xix Part I: Scala Tour Chapter 1: The Scala Language As a Functional Programming Language�������������������������������������������������������������������������������������� The Past���������������������������������������������������������������������������������������������������������������������������������������� The Future������������������������������������������������������������������������������������������������������������������������������������� Chapter 2: Installing Scala Getting Started������������������������������������������������������������������������������������������������������������������������������ The Scala Interpreter�������������������������������������������������������������������������������������������������������������������� Scala Scripts��������������������������������������������������������������������������������������������������������������������������������� scalac�������������������������������������������������������������������������������������������������������������������������������������������� Chapter 3: Some Basic Syntax 11 Defining Values and Variables����������������������������������������������������������������������������������������������������� 11 Defining Functions���������������������������������������������������������������������������������������������������������������������� 12 Operator Overloading and Infix Notation������������������������������������������������������������������������������������� 14 Collections���������������������������������������������������������������������������������������������������������������������������������� 15 Tuples������������������������������������������������������������������������������������������������������������������������������������������ 17 Java Interoperability������������������������������������������������������������������������������������������������������������������� 18 Primitive Types���������������������������������������������������������������������������������������������������������������������������� 19 v Table of Contents Chapter 4: Scala’s Class Hierarchy 21 AnyVal����������������������������������������������������������������������������������������������������������������������������������������� 22 Unit���������������������������������������������������������������������������������������������������������������������������������������������� 23 AnyRef����������������������������������������������������������������������������������������������������������������������������������������� 24 Bottom Types������������������������������������������������������������������������������������������������������������������������������ 25 Chapter 5: ScalaDoc����������������������������������������������������������������������������������������������� 29 Chapter 6: Language Features�������������������������������������������������������������������������������� 33 Working with Source Code���������������������������������������������������������������������������������������������������������� 33 Working with Methods���������������������������������������������������������������������������������������������������������������� 34 Functional Programming������������������������������������������������������������������������������������������������������������� 36 Chapter 7: Summary 37 Part II: Key Syntactical Differences 39 Chapter 8: Classes and Fields 43 Creating Classes������������������������������������������������������������������������������������������������������������������������� 43 Derived Setters and Getters�������������������������������������������������������������������������������������������������������� 44 Redefining Setters and Getters��������������������������������������������������������������������������������������������������� 48 Summary������������������������������������������������������������������������������������������������������������������������������������ 52 Chapter 9: Classes and Objects 53 Classes Without Constructor Arguments������������������������������������������������������������������������������������� 53 Additional Constructors��������������������������������������������������������������������������������������������������������������� 55 Using Default Values�������������������������������������������������������������������������������������������������������������� 57 Singleton Objects������������������������������������������������������������������������������������������������������������������������ 58 Companion Objects��������������������������������������������������������������������������������������������������������������������� 61 Other Uses for Companion Objects���������������������������������������������������������������������������������������� 63 Chapter 10: Classes and Functions������������������������������������������������������������������������ 67 Anonymous Functions����������������������������������������������������������������������������������������������������������������� 67 Anonymous Classes�������������������������������������������������������������������������������������������������������������������� 68 vi Table of Contents First-class Functions������������������������������������������������������������������������������������������������������������������� 69 Passing in Functions������������������������������������������������������������������������������������������������������������� 70 Returning Functions�������������������������������������������������������������������������������������������������������������� 71 Storing Functions������������������������������������������������������������������������������������������������������������������ 72 Function Types���������������������������������������������������������������������������������������������������������������������������� 74 Functions vs Methods���������������������������������������������������������������������������������������������������������������� 74 Lambdas vs Closures����������������������������������������������������������������������������������������������������������������� 75 Chapter 11: Inheritance 79 Subtype Inheritance�������������������������������������������������������������������������������������������������������������������� 79 Anonymous Classes�������������������������������������������������������������������������������������������������������������������� 82 Interfaces/Traits�������������������������������������������������������������������������������������������������������������������������� 84 Methods on Traits������������������������������������������������������������������������������������������������������������������ 87 Converting Anonymous Classes to Lambdas������������������������������������������������������������������������� 94 Concrete Fields on Traits������������������������������������������������������������������������������������������������������� 95 Abstract Fields on Traits�������������������������������������������������������������������������������������������������������� 96 Abstract Classes������������������������������������������������������������������������������������������������������������������������� 97 Polymorphism����������������������������������������������������������������������������������������������������������������������������� 98 Traits vs Abstract Classes����������������������������������������������������������������������������������������������������� 99 Deciding Between the Options�������������������������������������������������������������������������������������������������� 103 Chapter 12: Control Structures����������������������������������������������������������������������������� 105 Conditionals������������������������������������������������������������������������������������������������������������������������������ 105 Ifs and Ternaries������������������������������������������������������������������������������������������������������������������ 105 Switch Statements�������������������������������������������������������������������������������������������������������������� 108 Looping Structures: do, while and for��������������������������������������������������������������������������������������� 113 Breaking Control Flow (break and continue)����������������������������������������������������������������������������� 116 Exceptions��������������������������������������������������������������������������������������������������������������������������������� 117 vii Table of Contents Chapter 13: Generics 121 Parametric Polymorphism��������������������������������������������������������������������������������������������������������� 121 Class Generics��������������������������������������������������������������������������������������������������������������������� 122 Method Generics����������������������������������������������������������������������������������������������������������������� 123 Stack Example��������������������������������������������������������������������������������������������������������������������� 123 Bounded Classes����������������������������������������������������������������������������������������������������������������������� 126 Upper Bounds ()������������������������������������������������������������������������������������������� 127 Lower Bounds ()���������������������������������������������������������������������������������������������� 128 Wildcard Bounds (

Ngày đăng: 25/12/2020, 14:18