logo资料库

Head-First-Java英文版.pdf

第1页 / 共690页
第2页 / 共690页
第3页 / 共690页
第4页 / 共690页
第5页 / 共690页
第6页 / 共690页
第7页 / 共690页
第8页 / 共690页
资料共690页,剩余部分请下载后查看
Intro
1 Breaking the Surface: a quick dip
2 A Trip to Objectville: yes, there will be objects
3 Know Your Variables: primitives and references
4 How Objects Behave: object state affects method behavior
5 Extra-Strength Methods: flow control, operations, and more
6 Using the Java Library: so you don’t have to write it all yourself
7 Better Living in Objectville: planning for the future
8 Serious Polymorphism: exploiting abstract classes and interfaces
9 Life and Death of an Object: constructors and memory management
10 Numbers Matter: math, formatting, wrappers, and statics
11 Risky Behavior: exception handling
12 A Very Graphic Story: intro to GUI, event handling, and inner classes
13 Work on Your Swing: layout managers and components
14 Saving Objects: serialization and I/O
15 Make a Connection: networking sockets and multithreading
16 Data Structures: collections and generics
17 Release Your Code: packaging and deployment
18 Distributed Computing: RMI with a dash of servlets, EJB, and Jini
A Appendix A: Final code kitchen
B Appendix B: Top Ten Things that didn’t make it into the rest of the book 659 Index
Table of Contents (summary) Breaking the Surface: a quick dip A Trip to Objectville: yes, there will be objects Know Your Variables: primitives and references Extra-Strength Methods: flow control, operations, and more Using the Java Library: so you don’t have to write it all yourself Better Living in Objectville: planning for the future Serious Polymorphism: exploiting abstract classes and interfaces Life and Death of an Object: constructors and memory management Intro 1 2 3 4 How Objects Behave: object state affects method behavior 5 6 7 8 9 10 Numbers Matter: math, formatting, wrappers, and statics 11 Risky Behavior: exception handling 12 A Very Graphic Story: intro to GUI, event handling, and inner classes 13 Work on Your Swing: layout managers and components 14 15 Make a Connection: networking sockets and multithreading 16 Data Structures: collections and generics 17 Release Your Code: packaging and deployment 18 Distributed Computing: RMI with a dash of servlets, EJB, and Jini A B Saving Objects: serialization and I/O xxi 1 27 49 71 95 125 165 197 235 273 315 353 399 429 471 529 581 607 649 659 677 Appendix A: Final code kitchen Appendix B: Top Ten Things that didn’t make it into the rest of the book Index Table of Contents (the full version) i Intro Your brain on Java. Who is this book for? What your brain is thinking Metacognition Bend your brain into submission What you need for this book Technical editors Acknowledgements xxii xxiii xxv xxvii xxviii xxx xxxi ix
1 Breaking the Surface Java takes you to new places. Virtual Machines Method Party() 0 aload_0 1 invokespe- cial #1 4 return Compiled bytecode Yo u B e t S h o o t M e The way Java works Code structure in Java Anatomy of a class The main() method Looping Conditional branching (if tests) Coding the “99 bottles of beer” app Phrase-o-matic Fireside chat: compiler vs. JVM Exercises and puzzles 2 7 8 9 11 13 14 16 18 20 2 A Trip to Objectville I was told there would be objects. Chair Wars (Brad the OO guy vs. Larry the procedural guy) Inheritance (an introduction) Overriding methods (an introduction) What’s in a class? (methods, instance variables) Making your fi rst object Using main() Guessing Game code Exercises and puzzles 28 31 32 34 36 38 39 42 x
3 Know Your Variables Variables come in two flavors: primitive and reference. 24 size int Dog o bject fido Dog reference Declaring a variable (Java cares about type) Primitive types (“I’d like a double with extra foam, please”) Java keywords Reference variables (remote control to an object) Object declaration and assignment Objects on the garbage-collectible heap Arrays (a fi rst look) Exercises and puzzles 50 51 53 54 55 57 59 63 4 How Objects Behave State affects behavior, behavior affects state. pass-by-value means pass-by-copy copy of x 0 111 0 0 0 0 0 0 0 0 0111 X int Z int void go(int z){ } foo.go(x); Methods use object state (bark different) Method arguments and return types Pass-by-value (the variable is always copied) Getters and Setters Encapsulation (do it or risk humiliation) Using references in an array Exercises and puzzles 73 74 77 79 80 83 88 xi
5 Extra-Strength Methods Let’s put some muscle in our methods. Building the Sink a Dot Com game Starting with the Simple Dot Com game (a simpler version) Writing prepcode (pseudocode for the game) Test code for Simple Dot Com Coding the Simple Dot Com game Final code for Simple Dot Com Generating random numbers with Math.random() Ready-bake code for getting user input from the command-line Looping with for loops Casting primitives from a large size to a smaller size Converting a String to an int with Integer.parseInt() Exercises and puzzles 96 98 100 102 103 106 111 112 114 117 117 118 il d u b C o m g t e h a m e W e ’ r k n S i e a n n o g D o a t 6 Using the Java Library Java ships with hundreds of pre-built classes. - Julia, 31, hand model Analying the bug in the Simple Dot Com Game ArrayList (taking advantage of the Java API) Fixing the DotCom class code Building the real game (Sink a Dot Com) Prepcode for the real game Code for the real game boolean expressions Using the library (Java API) Using packages (import statements, fully-qualifi ed names) Using the HTML API docs and reference books Exercises and puzzles 126 132 138 140 144 146 151 154 155 158 161 xii
7 Better Living in Objectville Plan your programs with the future in mind. Make it Stick Understanding inheritance (superclass and subclass relationships) Designing an inheritance tree (the Animal simulation) Avoiding duplicate code (using inheritance) Overriding methods IS-A and HAS-A (bathtub girl) What do you inherit from your superclass? What does inheritance really buy you? Polymorphism (using a supertype reference to a subclass object) Rules for overriding (don’t touch those arguments and return types!) Method overloading (nothing more than method name re-use) Exercises and puzzles 168 170 171 172 177 180 182 183 190 191 192 8 Serious Polymorphism Inheritance is just the beginning. Object o = al.get(id); Dog d = (Dog) o; d.bark(); Object e c t Dog ob j cast the Object back to a Dog we know is there. o Object d Dog Some classes just should not be instantiated Abstract classes (can’t be instantiated) Abstract methods (must be implemented) Polymorphism in action Class Object (the ultimate superclass of everything) Taking objects out of an ArrayList (they come out as type Object) Compiler checks the reference type (before letting you call a method) Get in touch with your inner object Polymorphic references Casting an object reference (moving lower on the inheritance tree) Deadly Diamond of Death (multiple inheritance problem) Using interfaces (the best solution!) Exercises and puzzles 200 201 203 206 208 211 213 214 215 216 223 224 230 xiii
9 Life and Death of an Object Objects are born and objects die. l s W h e n s o m e o n e c a l t h e g o ( ) m e t h o d , t h i s D u c k i s a b a n d o n e d . H i s o n l y r e f e r e n c e h a s b e e n r e p r o g r a m m e d f o r a d i f f e r e n t D u c k . Duck o b j e ct d Duck o b j e ct p a e H ‘d’ is assigned a new Duck object, leaving the original (first) Duck object abandoned. That first Duck is toast.. The stack and the heap, where objects and variables live Methods on the stack Where local variables live Where instance variables live The miracle of object creation Constructors (the code that runs when you say new) Initializing the state of a new Duck Overloaded constructors Superclass constructors (constructor chaining) Invoking overloaded constructors using this() Life of an object Garbage Collection (and making objects eligible) Exercises and puzzles 236 237 238 239 240 241 243 247 250 256 258 260 266 10 Numbers Matter Do the Math. Static variables are shared by all instances of a class. static variable: iceCream kid instance two kid instance one instance variables: one per instance static variables: one per class xiv Math class (do you really need an instance of it?) static methods static variables Constants (static fi nal variables) Math methods (random(), round(), abs(), etc.) Wrapper classes (Integer, Boolean, Character, etc.) Autoboxing Number formatting Date formatting and manipulation Static imports Exercises and puzzles 274 275 277 282 286 287 289 294 301 307 310
11 Risky Behavior Stuff happens. thr o w s an exception back 2 class Bar { void go() { moo(); } int stuff() { x.beep(); } } your code 1 calls risky method class Cow { void moo() { if (serverDown){ explode(); } } } class with a risky method Making a music machine (the BeatBox) What if you need to call risky code? Exceptions say “something bad may have happened...” The compiler guarantees (it checks) that you’re aware of the risks Catching exceptions using a try/catch (skateboarder) Flow control in try/catch blocks The fi nally block (no matter what happens, turn off the oven!) Catching multiple exceptions (the order matters) Declaring an exception (just duck it) Handle or declare law Code Kitchen (making sounds) Exercises and puzzles 316 319 320 321 322 326 327 329 335 337 339 348 12 A Very Graphic Story Face it, you need to make GUIs. class MyOuter { class MyInner { void go() { } } } The outer and inner objects are now intimately linked. T h e s e t w o o b j e c t s o n t h e h e a p h a v e a s p e c i a l b o n d . T h e i n n e r c a n u s e t h e o u t e r ’ s v a r i a b l e s ( a n d v i c e - v e r s a ) . inn e r o u ter Your fi rst GUI Getting a user event Implement a listener interface Getting a button’s ActionEvent Putting graphics on a GUI Fun with paintComponent() The Graphics2D object Putting more than one button on a screen Inner classes to the rescue (make your listener an inner class) Animation (move it, paint it, move it, paint it, move it, paint it...) Code Kitchen (painting graphics with the beat of the music) Exercises and puzzles 355 357 358 360 363 365 366 370 376 382 386 394 xv
分享到:
收藏