Читать книгу Programming Kotlin Applications - Бретт Мак-Лахлин, Brett McLaughlin - Страница 2
Table of Contents
Оглавление1 COVER
8 INTRODUCTION WHAT DOES THIS BOOK COVER?
9 1 Objects All the Way Down KOTLIN: A NEW PROGRAMMING LANGUAGE WHAT IS KOTLIN? KOTLIN IS OBJECT-ORIENTED INTERLUDE: SET UP YOUR KOTLIN ENVIRONMENT CREATING USEFUL OBJECTS INITIALIZE AN OBJECT AND CHANGE A VARIABLE
10 2 It's Hard to Break Kotlin UPGRADE YOUR KOTLIN CLASS GAME KOTLIN HAS A LARGE NUMBER OF TYPES OVERRIDING PROPERTY ACCESSORS AND MUTATORS CLASSES CAN HAVE CUSTOM BEHAVIOR TYPE SAFETY CHANGES EVERYTHING WRITING CODE IS RARELY LINEAR
11 3 Kotlin Is Extremely Classy OBJECTS, CLASSES, AND KOTLIN ALL CLASSES NEED AN EQUALS(X) METHOD EVERY OBJECT INSTANCE NEEDS A UNIQUE HASHCODE() SEARCHING (AND OTHER THINGS) DEPEND ON USEFUL AND FAST EQUALS(X) AND HASHCODE() BASIC CLASS METHODS ARE REALLY IMPORTANT
12 4 Inheritance Matters GOOD CLASSES ARE NOT ALWAYS COMPLEX CLASSES CLASSES CAN DEFINE DEFAULT VALUES FOR PROPERTIES SECONDARY CONSTRUCTORS PROVIDE ADDITIONAL CONSTRUCTION OPTIONS HANDLE DEPENDENT VALUES WITH CUSTOM MUTATORS NEED SPECIFICS? CONSIDER A SUBCLASS YOUR SUBCLASS SHOULD BE DIFFERENT THAN YOUR SUPERCLASS
13 5 Lists and Sets and Maps, Oh My! LISTS ARE JUST A COLLECTION OF THINGS LISTS (AND COLLECTIONS) CAN BE TYPED LISTS ARE ORDERED AND CAN REPEAT SETS: UNORDERED BUT UNIQUE MAPS: WHEN A SINGLE VALUE ISN'T ENOUGH FILTER A COLLECTION BY … ANYTHING COLLECTIONS: FOR PRIMITIVE AND CUSTOM TYPES
14 6 The Future (in Kotlin) Is Generic GENERICS ALLOW DEFERRING OF A TYPE GENERICS TRY TO INFER A TYPE WHEN POSSIBLE COVARIANCE: A STUDY IN TYPES AND ASSIGNMENT CONTRAVARIANCE: BUILDING CONSUMERS FROM GENERIC TYPES UNSAFEVARIANCE: LEARNING THE RULES, THEN BREAKING THEM TYPEPROJECTION LETS YOU DEAL WITH BASE CLASSES
15 7 Flying through Control Structures CONTROL STRUCTURES ARE THE BREAD AND BUTTER OF PROGRAMMING IF AND ELSE: THE GREAT DECISION POINT WHEN IS KOTLIN'S VERSION OF SWITCH FOR IS FOR LOOPING USE WHILE TO EXECUTE UNTIL A CONDITION IS FALSE DO … WHILE ALWAYS RUNS ONCE GET OUT OF A LOOP IMMEDIATELY WITH BREAK GO TO THE NEXT ITERATION IMMEDIATELY WITH CONTINUE RETURN RETURNS
16 8 Data Classes CLASSES IN THE REAL WORLD ARE VARIED BUT WELL EXPLORED A DATA CLASS TAKES THE WORK OUT OF A CLASS FOCUSED ON DATA DESTRUCTURING DATA THROUGH DECLARATIONS YOU CAN “COPY” AN OBJECT OR MAKE A COPY OF AN OBJECT DATA CLASSES REQUIRE SEVERAL THINGS FROM YOU DATA CLASSES ADD SPECIAL BEHAVIOR TO GENERATED CODE DATA CLASSES ARE BEST LEFT ALONE
17 9 Enums and Sealed, More Specialty Classes STRINGS ARE TERRIBLE AS STATIC TYPE REPRESENTATIONS COMPANION OBJECTS ARE SINGLE INSTANCE ENUMS DEFINE CONSTANTS AND PROVIDE TYPE SAFETY SEALED CLASSES ARE TYPE-SAFE CLASS HIERARCHIES
18 10 Functions and Functions and Functions REVISITING THE SYNTAX OF A FUNCTION FUNCTIONS FOLLOW FLEXIBLE RULES FUNCTIONS IN KOTLIN HAVE SCOPE FUNCTION LITERALS: LAMBDAS AND ANONYMOUS FUNCTIONS LOTS OF FUNCTIONS, LOTS OF ROOM FOR PROBLEMS
19 11 Speaking Idiomatic Kotlin SCOPE FUNCTIONS PROVIDE CONTEXT TO CODE USE LET TO PROVIDE IMMEDIATE ACCESS TO AN INSTANCE WITH IS A SCOPE FUNCTION FOR PROCESSING AN INSTANCE RUN IS A CODE RUNNER AND SCOPE FUNCTION APPLY HAS A CONTEXT OBJECT BUT NO RETURN VALUE ALSO GIVES YOU AN INSTANCE … BUT OPERATES ON THE INSTANCE FIRST SCOPE FUNCTIONS SUMMARY
20 12 Inheritance, One More Time, with Feeling ABSTRACT CLASSES REQUIRE A LATER IMPLEMENTATION INTERFACES DEFINE BEHAVIOR BUT HAVE NO BODY DELEGATION OFFERS ANOTHER OPTION FOR EXTENDING BEHAVIOR INHERITANCE REQUIRES FORETHOUGHT AND AFTERTHOUGHT
21 13 Kotlin: The Next Step PROGRAMMING KOTLIN FOR ANDROID KOTLIN AND JAVA ARE GREAT COMPANIONS WHEN KOTLIN QUESTIONS STILL EXIST NOW WHAT?
22 INDEX