logo资料库

Kotlin Programming Cookbook.pdf

第1页 / 共423页
第2页 / 共423页
第3页 / 共423页
第4页 / 共423页
第5页 / 共423页
第6页 / 共423页
第7页 / 共423页
第8页 / 共423页
资料共423页,剩余部分请下载后查看
Cover
Title Page
Copyright and Credits
Packt Upsell
Contributors
Table of Contents
Preface
Chapter 1: Installation and Working with Environment
Introduction
Creating Kotlin Android project
Getting ready
How to do it...
There's more...
How to use Gradle to run Kotlin code
Getting ready
How to do it...
There's more...
See also
How to run a Kotlin compiled class
Getting ready
How to do it...
How it works...
There's more...
How to build a self-executable JAR with Gradle and Kotlin
Getting ready
How to do it...
How it works...
Reading console input in Kotlin
Getting ready
How to do it...
How it works...
Converting Java code to Kotlin and vice versa
Getting ready
How to do it...
How it works...
How to write an idiomatic logger in Kotlin
Getting ready
How to do it...
How it works...
There's more...
See also
Escaping for Java identifiers that are keywords in Kotlin
Getting ready
How to do it...
How it works...
Disambiguating using the "as" keyword to locally rename the clashing entity
Getting ready
How to do it...
Doing bit manipulations in Kotlin
Getting ready
How to do it...
Or
and
xor
inv
shl
shr
ushr
How it works...
Parsing String to Long, Double, or Int
Getting ready
How to do it...
Converting String to Long
Converting string to Long using string.toLongOrNull()
Converting with special radix
Parsing string to Long with special radix
How it works...
There's more...
Using String templates in Kotlin
How to do it...
How it works...
There’s more...
Chapter 2: Control Flow
Introduction
Assigning result to an expression using the if keyword
Getting ready
How to do it...
There's more...
Using range with the when expression
Getting ready
How to do it...
How it works...
There's more...
Using when with custom objects
Getting ready
How to do it...
How it works...
Using try–catch as an expression
Getting ready
How to do it...
How it works...
There's more...
How to write a swap function in Kotlin using the also function
Getting ready
How to do it...
How it works...
There's more...
How to throw a custom exception in Kotlin
Getting ready
How to do it...
How it works...
How to make a multiconditional loop in Kotlin
Getting ready
How to do it...
How it works...
Chapter 3: Classes and Objects
Introduction
Initializing body of constructor
Getting ready
How to do it...
Converting one data type into another
Getting ready
How to do it...
How it works...
How to type check an object
Getting ready
How to do it...
How it works...
There's more...
How to work with an abstract class in Kotlin
Getting ready
How to do it...
How it works...
How to iterate over a class's properties in Kotlin
Getting ready
How to do it...
How it works...
There's more...
How to work with inline properties
Getting ready
How to do it...
How it works...
How to work with nested class
Getting ready
How to do it...
How it works...
There's more...
Getting class in Kotlin
How to do it...
There's more...
Working with delegated properties
Getting ready
How to do it...
There's more...
Working with enums
Getting ready
How to do it...
There's more...
Chapter 4: Functions
Introduction
Specifying default values in functions
Getting ready
How to do it...
There's more...
Using named arguments in functions
Getting ready
How to do it...
Creating the RecyclerView Adapter in Kotlin
Getting ready
How to do it...
How it works...
There's more...
Creating getter setters in Kotlin
Getting ready
How to do it...
There's more...
Passing variable arguments to a function
Getting ready
How to do it...
How it works...
There's more...
Passing a function as a parameter to another
Getting ready
How to do it...
How it works...
Declaring a static function
Getting ready
How to do it...
Using the use keyword in Kotlin
Getting ready
How to do it...
How it works...
Working with closures
Getting ready
How to do it...
There's more...
Function literals with receivers
Getting ready
How to do it...
Working with anonymous functions
Getting ready
How to do it...
Chapter 5: Object-Oriented Programming
Introduction
Working with interfaces in Kotlin
Getting ready
How to do it…
There's more…
How to implement complicated interfaces with multiple overridden methods in Kotlin
Getting ready
How to do it…
How to extend a class in Kotlin (Inheritance and Extension functions)
Getting ready
How to do it…
Extension functions
How it works…
There's more…
How to work with Generics in Kotlin
Getting ready
How to do it…
There's more…
How to implement polymorphism in Kotlin
Getting ready
How to do it…
Restricting class hierarchies
Getting ready
How to do it…
How it works…
There's more…
Chapter 6: Collections Framework
Introduction
How to merge two collections
Getting ready
How to do it…
Splitting original collection into pair of collections
Getting ready
How to do it…
How it works…
There's more…
Sorting a list by specified comparator
Getting ready
How to do it…
How it works…
Sorting in descending order
Getting ready
How to do it…
How it works…
Parsing a JSON response using Gson
Getting ready
How to do it…
There's more…
How to filter and map using lambda expressions
Getting ready
How to do it…
How to sort a list of objects and keep null objects at the end
Getting ready
How to do it…
How it works…
How to implement a lazy list in Kotlin
Getting ready
How to do it…
How it works…
How to pad a string in Kotlin
Getting ready
How to do it…
How it works…
How to flatten an array or map
Getting ready
How to do it…
How it works…
How to sort collection by multiple fields in Kotlin
Getting ready
How to do it…
How it works…
How to use limit in Kotlin list
Getting ready
How to do it…
How to create a 2D array in Kotlin
Getting ready
How to do it…
There's more…
How to skip the first "n" entries in Kotlin
Getting ready
How to do it…
How it works…
Chapter 7: Handling File Operations in Kotlin
Introduction
Reading from files using InputReader
Getting ready
How to do it…
How it works…
There's more…
Reading all lines in a file using InputReader
Getting ready
How to do it…
How it works…
Reading line by line using InputReader
Getting ready
How to do it…
How it works…
Reading from files using BufferedReader
Getting ready
How to do it…
How it works…
Reading all lines in a file using BufferedReader
Getting ready
How to do it…
How it works…
Reading line by line using bufferedReader
Getting ready
How to do it…
How it works…
There's more…
Reading string and JSON over network
Getting ready
How to do it…
How it works…
Chapter 8: Anko Commons and Extension Function
Introduction
Setting up Anko with Gradle
Getting ready
How to do it…
Extending Android framework using extension function
Getting ready
How to do it…
How it works…
There's more…
Using extensions as properties
Getting ready
How to do it…
How it works…
There's more…
Using intents with Anko
Getting ready
How to do it…
How it works…
Making a call intent using Anko
Getting ready
How to do it…
How it works…
Sending a text intent using Anko
Getting ready
How to do it…
How it works…
Browsing the web browser using Anko
Getting ready
How to do it…
How it works…
Sharing some text using intents in Anko
Getting ready
How to do it…
How it works…
Sending an email using Anko
Getting ready
How to do it…
How it works…
Creating Android dialogs with Anko
Getting ready
How to do it…
Showing an alert dialog with a list of text items
Getting ready
How to do it…
How it works…
Using Anko in Views
Getting ready
How to do it…
Logging using Anko
Getting ready
How to do it…
Handling dimensions with Anko
Getting ready
How to do it…
Version checking on Android
Getting ready
How to do it…
How it works…
Chapter 9: Anko Layouts
Introduction
Setting up Anko library for Anko layouts in Gradle
Getting ready
How to do it…
How it works…
There's more…
Creating user-interface programmatically
Getting ready
How to do it…
How it works…
Working with the old code of XML layouts
Getting ready
How to do it…
How it works…
There's more…
Using the provided AnkoComponent interface
Getting ready
How to do it…
How it works…
There's more…
Setting theme for Android views in Anko
Getting ready
How to do it…
Setting layout parameters for Anko views
Getting ready
How to do it…
How it works…
Adding listeners to Anko views
Getting ready
How to do it…
How it works…
There's more…
Inserting XML layouts into DSL
Getting ready
How to do it…
Converting XML files into DSL
Getting ready
How to do it…
Showing Snackbar
Getting ready
How to do it…
Showing Toasts
Getting started
How to do it…
Accessing views using synthetic properties
Getting ready
How to do it…
Accessing views of view groups using extension functions
Getting ready
How to do it…
How it works...
Chapter 10: Databases and Dependency Injection
Introduction
Using SQLite database in Kotlin
Getting ready
How to do it…
Creating database tables
Getting ready
How to do it…
Injecting dependencies in Kotlin
Getting ready
How to do it…
How it works…
Reading data from database
Getting ready
How to do it…
Converting database cursor into list of objects
Getting ready
How to do it…
Using parseOpt for nullable object
Getting ready
How to do it…
Inserting data into database
Getting ready
How to do it…
Creating singletons in Kotlin
Getting ready
How to do it…
How it works…
Using Dagger 2 with Kotlin
Getting ready
How to do it…
Using Butterknife with Kotlin
Getting ready
How to do it…
There's more…
Chapter 11: Networking and Concurrency
Introduction
How to fetch data over network
Getting ready
How to do it…
There's more…
How to create data class in Kotlin
Getting ready
How to do it…
There's more…
How to copy data class with modifications
Getting ready
How to do it…
There's more…
How to parse JSON data from network to data class
Getting ready
How to do it…
How to download a file in Kotlin
Getting ready
How to do it…
How to use RxJava and Retrofit with Kotlin
Getting ready
How to do it…
How to make an endless list using RecyclerView
Getting ready
How to do it…
How it works…
How to use Anko to run background tasks with Kotlin in Android
Getting ready
How to do it…
How it works…
How to use coroutines to achieve multithreading
Getting ready
How to do it…
There's more…
Chapter 12: Lambdas and Delegates
Introduction
Click listeners using lambdas
Getting ready
How to do it…
How it works…
There's more…
Using lazy delegate in Kotlin
Getting ready
How to do it…
How it works…
Using the observable delegate
Getting ready
How to do it…
There's more…
Using vetoable delegate
Getting ready
How to do it…
How it works…
There's more…
Writing your own delegates
Getting ready
How to do it…
How it works…
Using the lateinit modifier
Getting ready
How to do it…
There's more…
Working with SharedPreferences
Getting ready
How to do it…
There's more…
Creating a chain of multiple lets in Kotlin
Getting ready
How to do it…
Creating global variables
Getting ready
How to do it…
Chapter 13: Testing
Introduction
Unit testing Kotlin code
Getting ready
How to do it…
How it works…
Unit testing with Mockito
Getting ready
How to do it…
There's more…
Spy object
Mockito limitations
Running instrumentation tests
Getting ready
How to do it…
There's more…
Writing JUnit rules in Kotlin (@Rule)
Getting ready
How to do it…
How it works…
Acceptance tests using Espresso Kotlin
Getting ready
How to do it…
There's more…
Writing assertEquals in Kotlin
Getting ready
How to do it…
Chapter 14: Web Services with Kotlin
Introduction
How to run the application on Tomcat
How to do it…
Setting up dependencies for building RESTful services
Getting ready
How to do it…
How to create a REST controller
Getting ready
How to do it…
There's more…
Creating the Application class for Spring Boot
Getting ready
How to do it…
Other Books You May Enjoy
Index
Kotlin Programming Cookbook Aanand Shekhar Roy Rashi Karanpuria BIRMINGHAM - MUMBAI
Kotlin Programming Cookbook Copyright 2018 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. Commissioning Editor: Merint Mathew Acquisition Editor: Isha Raval Content Development Editor: Onkar Wani Technical Editor: Sachin Sunilkumar Copy Editor: Shaila Kusanale Project Coordinator: Devanshi Doshi Proofreader: Safis Editing Indexer: Rekha Nair Graphics: Jason Monteiro Production Coordinator: Nilesh Mohite First published: January 2018 Production reference: 1230118 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78847-214-2
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website. Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Mapt is fully searchable Copy and paste, print, and bookmark content PacktPub.com Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at for more details. At , you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Contributors About the authors Aanand Shekhar Roy is a freelance Android developer. His mobile engineering career includes working for various startups and companies, such as Netherlands-based ConceptOffice and U.S.-based startups Consciously.Life and NextFan-FantasyIndyCar. He is also a mentor for the Associate Android Developer Fast Track program by Google on Udacity and mentors a team of 20 software developers. He is also a writer at AndroidPub. Acknowledgement is probably that part of the book you least have interest in. Simply because every time you open it, it's either thanking someone else who you don't know. Not this time, this book is dedicated to only learners like you, simply because it wouldn't have come to life without you wanting to read it. Rashi Karanpuria is a professional Android developer. She started her career by working on a wallet app for a New Zealand based Fintech startup. She has developed projects in various domains such as IOT, AdTech, Business, Social, and live streaming apps. Over the years, she has acquired a deep understanding of the user experience needed to develop a successful mobile application. She combines her design skills with scalable and maintainable code using best practices to create quality products. I would like to thank each & every member of the developer community all across the world. This is the only community that I know of to give ideas and resources so freely without expecting anything in return. Did you know Kotlin is open source? This giving nature of our developer community is why I am who I am today. Thanks.
About the reviewer Mitchell Wong Ho was born in Johannesburg, South Africa, where he completed his national diploma in electrical engineering. Mitchell's software development career started on embedded systems and then moved to Microsoft desktop/server applications. Mitchell has been programming in Java since 2000 on J2ME, JEE, desktop, and Android applications, and has more recently been advocating Kotlin for Android. Packt is searching for authors like you If you're interested in becoming an author for Packt, please visit and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Table of Contents Preface Chapter 1: Installation and Working with Environment Introduction Creating Kotlin Android project How to use Gradle to run Kotlin code How to run a Kotlin compiled class 1 7 8 9 9 9 13 14 14 14 18 19 19 19 20 22 22 23 23 24 25 25 26 26 27 28 28 28 32 33 33 33 34 35 Getting ready How to do it... There's more... Getting ready How to do it... There's more... See also Getting ready How to do it... How it works... There's more... Getting ready How to do it... How it works... Getting ready How to do it... How it works... Getting ready How to do it... How it works... Getting ready How to do it... How it works... There's more... How to build a self-executable JAR with Gradle and Kotlin Reading console input in Kotlin Converting Java code to Kotlin and vice versa How to write an idiomatic logger in Kotlin
Table of Contents Escaping for Java identifiers that are keywords in Kotlin Disambiguating using the "as" keyword to locally rename the clashing entity Doing bit manipulations in Kotlin 35 35 36 36 36 37 37 38 39 39 39 40 40 41 42 42 43 44 44 45 45 45 45 46 46 47 48 48 49 50 51 51 52 52 53 53 53 56 57 See also Getting ready How to do it... How it works... Getting ready How to do it... Getting ready How to do it... Or and xor inv shl shr ushr Parsing String to Long, Double, or Int How it works... Getting ready How to do it... Converting String to Long Converting string to Long using string.toLongOrNull() Converting with special radix Parsing string to Long with special radix How it works... There's more... Using String templates in Kotlin How to do it... How it works... There’s more... Chapter 2: Control Flow Introduction Assigning result to an expression using the if keyword Getting ready How to do it... There's more... Using range with the when expression [ ii ]
分享到:
收藏