实例介绍
【实例简介】teach_your_self_java_in_21_days
【实例截图】
【核心代码】
Overview
Introduction xxi
Week 1 at a Glance
Day 1 An Introduction to Java Programming 3
2 Object-Oriented Programming and Java 19
3 Java Basics 41
4 Working with Objects 61
5 Arrays, Conditionals, and Loops 79
6 Creating Classes and Applications in Java 95
7 More About Methods 111
Week 2 at a Glance
Day 8 Java Applet Basics 129
9 Graphics, Fonts, and Color 149
10 Simple Animation and Threads 173
11 More Animation, Images, and Sound 195
12 Managing Simple Events and Interactivity 217
13 User Interfaces with the Java Abstract Windowing Toolkit 237
14 Windows, Networking, and Other Tidbits 279
Week 3 at a Glance
Day 15 Modifiers 305
16 Packages and Interfaces 323
17 Exceptions 341
18 Multithreading 353
19 Streams 375
20 Native Methods and Libraries 403
21 Under the Hood 421
Appendixes
A Language Summary 473
B The Java Class Library 483
C How Java Differs from C and C 497
D How Java Differs from C and C 507
Index 511
030-4 FM
1/29/96, 8:11 PM
7
M T W R F S S
Sams.net
Learning
Centerix
abcd
P2/V4SQC6 TY Java in 21 Days 030-4 louisa 12.31.95 FM LP#4
Contents
Introduction xxi
Week 1 at a Glance 1
Day 1 An Introduction to Java Programming 3
What Is Java? .............................................................................................. 4
Java’s Past, Present, and Future................................................................... 6
Why Learn Java? ......................................................................................... 7
Java Is Platform-Independent................................................................. 7
Java Is Object-Oriented ......................................................................... 9
Java Is Easy to Learn .............................................................................. 9
Getting Started with
Programming in Java .............................................................................. 10
Getting the Software ............................................................................ 10
Applets and Applications ..................................................................... 11
Creating a Java Application .................................................................. 11
Creating a Java Applet ......................................................................... 13
Summary .................................................................................................. 16
Q&A ........................................................................................................ 16
Day 2 Object-Oriented Programming and Java 19
Thinking in Objects: An Analogy ............................................................. 20
Objects and Classes .................................................................................. 21
Behavior and Attributes ............................................................................ 23
Attributes ............................................................................................. 23
Behavior .............................................................................................. 24
Creating a Class ................................................................................... 24
Inheritance, Interfaces, and Packages ........................................................ 28
Inheritance........................................................................................... 29
Creating a Class Hierarchy................................................................... 30
How Inheritance Works ...................................................................... 32
Single and Multiple Inheritance ........................................................... 34
Interfaces and Packages ........................................................................ 34
Creating a Subclass ................................................................................... 35
Summary .................................................................................................. 38
Q&A ........................................................................................................ 39
Day 3 Java Basics 41
Statements and Expressions ...................................................................... 42
Variables and Data Types ......................................................................... 43
Declaring Variables .............................................................................. 43
Notes on Variable Names .................................................................... 44
030-4 FM
1/29/96, 8:11 PM
9
M T W R F S S
Sams.net
Learning
CenterTeach Yourself JAVA in 21 Days
21
x
P2/V4SQC6 TY Java in 21 Days 030-4 louisa 12.31.95 FM LP#4
Variable Types ..................................................................................... 45
Assigning Values to Variables ............................................................... 46
Comments ................................................................................................ 47
Literals...................................................................................................... 47
Number Literals................................................................................... 47
Boolean Literals ................................................................................... 48
Character Literals ................................................................................. 48
String Literals ...................................................................................... 49
Expressions and Operators ........................................................................ 50
Arithmetic ........................................................................................... 50
More About Assignment ...................................................................... 52
Incrementing and Decrementing ......................................................... 52
Comparisons........................................................................................ 54
Logical Operators ................................................................................ 55
Bitwise Operators ................................................................................ 55
Operator Precedence ............................................................................ 56
String Arithmetic ...................................................................................... 57
Summary .................................................................................................. 58
Q&A ........................................................................................................ 60
Day 4 Working with Objects 61
Creating New Objects .............................................................................. 62
Using new ............................................................................................ 63
What new Does ................................................................................... 64
A Note on Memory Management ........................................................ 64
Accessing and Setting Class and Instance Variables ................................... 65
Getting Values ..................................................................................... 65
Changing Values.................................................................................. 65
Class Variables ..................................................................................... 66
Calling Methods ....................................................................................... 67
Class Methods ..................................................................................... 69
References to Objects ............................................................................... 70
Casting and Converting Objects and Primitive Types .............................. 71
Casting Primitive Types ...................................................................... 71
Casting Objects .................................................................................. 72
Converting Primitive Types
to Objects and Vice Versa ................................................................. 73
Odds and Ends ......................................................................................... 73
Comparing Objects ............................................................................. 74
Copying Objects .................................................................................. 75
Determining the Class of an Object ..................................................... 76
The Java Class Libraries ............................................................................ 76
Summary .................................................................................................. 77
Q&A ........................................................................................................ 78
030-4 FM
1/29/96, 8:11 PM
10
M T W T
F S Sxi
abcd
P2/V4SQC6 TY Java in 21 Days 030-4 louisa 12.31.95 FM LP#4
Day 5 Arrays, Conditionals, and Loops 79
Arrays ....................................................................................................... 80
Declaring Array Variables .................................................................... 80
Creating Array Objects ........................................................................ 81
Accessing Array Elements..................................................................... 81
Changing Array Elements .................................................................... 82
Multidimensional Arrays...................................................................... 83
Block Statements ...................................................................................... 83
if Conditionals .......................................................................................... 83
The Conditional Operator ................................................................... 84
switch Conditionals ................................................................................... 85
for Loops .................................................................................................. 86
while and do Loops ................................................................................... 88
while Loops .......................................................................................... 88
do...while Loops ................................................................................... 89
Breaking Out of Loops ............................................................................. 89
Labeled Loops ...................................................................................... 90
Summary .................................................................................................. 91
Q&A ........................................................................................................ 92
Day 6 Creating Classes and Applications in Java 95
Defining Classes ....................................................................................... 96
Creating Instance and Class Variables ....................................................... 96
Defining Instance Variables ................................................................. 97
Constants............................................................................................. 97
Class Variables ..................................................................................... 98
Creating Methods ..................................................................................... 99
Defining Methods ............................................................................... 99
The this Keyword ............................................................................... 101
Variable Scope and Method Definitions ............................................ 101
Passing Arguments to Methods .......................................................... 102
Class Methods ................................................................................... 104
Creating Java Applications ...................................................................... 105
Java Applications and Command-Line Arguments ................................. 106
Passing Arguments to Java Programs .................................................. 106
Handling Arguments in Your Java Program ....................................... 106
Summary ................................................................................................ 108
Q&A ...................................................................................................... 109
Day 7 More About Methods 111
Creating Methods with the Same Name, Different Arguments ............... 112
Constructor Methods ............................................................................. 115
Basic Constructors ............................................................................. 116
Calling Another Constructor ............................................................. 117
Overloading Constructors .................................................................. 117
030-4 FM
1/29/96, 8:11 PM
11
Sams.net
Learning
CenterTeach Yourself JAVA in 21 Days
21
xii
P2/V4SQC6 TY Java in 21 Days 030-4 louisa 12.31.95 FM LP#4
Overriding Methods ............................................................................... 119
Creating Methods that Override Existing Methods............................ 119
Calling the Original Method ............................................................. 121
Overriding Constructors .................................................................... 122
Finalizer Methods ................................................................................... 123
Summary ................................................................................................ 124
Q&A ...................................................................................................... 124
Week 2 at a Glance 127
Day 8 Java Applet Basics 129
How Applets and Applications Are Different .......................................... 130
Creating Applets ..................................................................................... 131
Major Applet Activities ...................................................................... 132
A Simple Applet................................................................................. 134
Including an Applet on a Web Page........................................................ 136
The <APPLET> Tag.......................................................................... 136
Testing the Result .............................................................................. 137
Making Java Applets Available to the Web......................................... 137
More About the <APPLET> Tag............................................................ 138
ALIGN .............................................................................................. 138
HSPACE and VSPACE ...................................................................... 140
CODE and CODEBASE .................................................................... 141
Passing Parameters to Applets ................................................................. 141
Summary ................................................................................................ 146
Q&A ...................................................................................................... 147
Day 9 Graphics, Fonts, and Color 149
The Graphics Class ................................................................................. 150
The Graphics Coordinate System ...................................................... 151
Drawing and Filling ............................................................................... 151
Lines .................................................................................................. 152
Rectangles .......................................................................................... 152
Polygons ............................................................................................ 155
Ovals ................................................................................................. 156
Arc ..................................................................................................... 157
A Simple Graphics Example............................................................... 161
Copying and Clearing ........................................................................ 163
Text and Fonts ....................................................................................... 163
Creating Font Objects ....................................................................... 163
Drawing Characters and Strings......................................................... 164
Finding Out Information About a Font ............................................. 166
Color ...................................................................................................... 168
Using Color Objects .......................................................................... 168
Testing and Setting the Current Colors ............................................. 169
A Single Color Example ..................................................................... 170
Summary ................................................................................................ 171
Q&A ...................................................................................................... 171
030-4 FM
1/29/96, 8:11 PM
12
M T W T
F S Sxiii
abcd
P2/V4SQC6 TY Java in 21 Days 030-4 louisa 12.31.95 FM LP#4
Day 10 Simple Animation and Threads 173
Creating Animation in Java .................................................................... 174
Painting and Repainting .................................................................... 174
Starting and Stopping
an Applet’s Execution ...................................................................... 175
Putting It Together ............................................................................ 175
Threads: What They Are
and Why You Need Them ................................................................... 177
The Problem with the Digital Clock Applet ...................................... 178
Writing Applets with Threads ............................................................ 179
Fixing The Digital Clock ................................................................... 180
Reducing Animation Flicker ................................................................... 182
Flicker and How to Avoid It .............................................................. 182
How to Override Update ................................................................... 183
Solution One: Don’t Clear the Screen ............................................... 183
Solution Two: Redraw
Only What You Have To ................................................................ 186
Summary ................................................................................................ 192
Q&A ...................................................................................................... 192
Day 11 More Animation, Images, and Sound 195
Retrieving and Using Images .................................................................. 196
Getting Images .................................................................................. 196
Drawing Images ................................................................................. 198
Modifying Images .............................................................................. 201
Creating Animation Using Images .......................................................... 201
An Example: Neko............................................................................. 201
Retrieving and Using Sounds .................................................................. 209
Sun’s Animator Applet ........................................................................... 211
More About Flicker: Double-Buffering................................................... 212
Creating Applets with Double-Buffering............................................ 212
An Example: Checkers Revisited ........................................................ 213
Summary ................................................................................................ 214
Q&A ...................................................................................................... 215
Day 12 Managing Simple Events and Interactivity 217
Mouse Clicks .......................................................................................... 218
mouseDown and mouseUp................................................................... 219
An Example: Spots............................................................................. 220
Mouse Movements ................................................................................. 223
mouseDrag and mouseMove................................................................. 223
mouseEnter and mouseExit .................................................................. 223
An Example: Drawing Lines .............................................................. 224
Keyboard Events ..................................................................................... 228
The keyDown Method ....................................................................... 228
Default Keys ...................................................................................... 229
030-4 FM
1/29/96, 8:11 PM
13
Sams.net
Learning
CenterTeach Yourself JAVA in 21 Days
21
xiv
P2/V4SQC6 TY Java in 21 Days 030-4 louisa 12.31.95 FM LP#4
An Example: Entering, Displaying, and Moving Characters .............. 229
Testing for Modifier Keys .................................................................. 232
The AWT Event Handler ....................................................................... 233
Summary ................................................................................................ 235
Q&A ...................................................................................................... 235
Day 13 The Java Abstract Windowing Toolkit 237
An AWT Overview................................................................................. 238
The Basic User Interface Components .................................................... 240
Labels ................................................................................................ 241
Buttons .............................................................................................. 242
Checkboxes........................................................................................ 243
Radio Buttons.................................................................................... 244
Choice Menus ................................................................................... 245
Text Fields ......................................................................................... 247
Panels and Layout................................................................................... 249
Layout Managers ............................................................................... 249
Insets ................................................................................................. 254
Handling UI Actions and Events ............................................................ 255
Nesting Panels and Components ............................................................ 258
Nested Panels .................................................................................... 258
Events and Nested Panels .................................................................. 258
More UI Components ............................................................................ 259
Text Areas .......................................................................................... 259
Scrolling Lists .................................................................................... 261
Scrollbars and Sliders ......................................................................... 262
Canvases ............................................................................................ 265
More UI Events ...................................................................................... 265
A Complete Example:
RGB to HSB Converter ....................................................................... 266
Create the Applet Layout ................................................................... 267
Create the Panel Layout ..................................................................... 267
Define the Subpanels ......................................................................... 269
Handle the Actions ............................................................................ 272
Update the Result .............................................................................. 272
The Complete Source Code ............................................................... 274
Summary ................................................................................................ 277
Q&A ...................................................................................................... 277
Day 14 Windows, Networking, and Other Tidbits 279
Windows, Menus, and Dialog Boxes ...................................................... 280
Frames ............................................................................................... 280
Menus ............................................................................................... 282
Dialog Boxes...................................................................................... 285
File Dialogs........................................................................................ 287
Window Events ................................................................................. 288
Using AWT Windows in Stand-Alone Applications .......................... 288
030-4 FM
1/29/96, 8:11 PM
14
M T W T
F S Sxv
abcd
P2/V4SQC6 TY Java in 21 Days 030-4 louisa 12.31.95 FM LP#4
Networking in Java ................................................................................. 289
Creating Links Inside Applets ............................................................ 290
Opening Web Connections ............................................................... 292
openStream() ...................................................................................... 293
The URLconnection Class ................................................................... 296
Sockets............................................................................................... 296
Other Applet Hints ................................................................................ 297
The showStatus Method ..................................................................... 297
Applet Information ............................................................................ 298
Communicating Between Applets ...................................................... 298
Summary ................................................................................................ 299
Q&A ...................................................................................................... 300
Week 3 at a Glance 303
Day 15 Modifiers 305
Method and Variable Access Control...................................................... 307
The Four P’s of Protection................................................................. 307
The Conventions for Instance Variable Access ................................... 312
Class Variables and Methods .................................................................. 314
The final Modifier .................................................................................. 316
final Classes ....................................................................................... 316
final Variables .................................................................................... 317
final Methods .................................................................................... 317
abstract Methods and Classes .................................................................. 319
Summary ................................................................................................ 320
Q&A ...................................................................................................... 320
Day 16 Packages and Interfaces 323
Packages ................................................................................................. 324
Programming in the Large ................................................................. 324
Programming in the Small ................................................................. 327
Hiding Classes ................................................................................... 329
Interfaces ................................................................................................ 331
Programming in the Large ................................................................. 331
Programming in the Small ................................................................. 335
Summary ................................................................................................ 338
Q&A ...................................................................................................... 339
Day 17 Exceptions 341
Programming in the Large ...................................................................... 342
Programming in the Small ...................................................................... 345
The Limitations Placed on the Programmer ........................................... 348
The finally Clause ................................................................................... 349
Summary ................................................................................................ 350
Q&A ...................................................................................................... 351
030-4 FM
1/29/96, 8:11 PM
15
Sams.net
Learning
CenterTeach Yourself JAVA in 21 Days
21
xvi
P2/V4SQC6 TY Java in 21 Days 030-4 louisa 12.31.95 FM LP#4
Day 18 Multithreading 353
The Problem with Parallelism ................................................................. 354
Thinking Multithreaded ......................................................................... 355
Points About Points ........................................................................... 357
Protecting a Class Variable................................................................. 360
Creating and Using Threads ................................................................... 361
The Runnable Interface ...................................................................... 362
ThreadTester ...................................................................................... 363
NamedThreadTester ........................................................................... 365
Knowing When a Thread has Stopped ................................................... 366
Thread Scheduling ................................................................................. 367
Preemptive Versus Nonpreemptive .................................................... 367
Testing Your Scheduler ...................................................................... 368
Summary ................................................................................................ 371
Q&A ...................................................................................................... 372
Day 19 Streams 375
Input Streams ......................................................................................... 377
The abstract Class InputStream ........................................................... 377
ByteArrayInputStream ......................................................................... 381
FileInputStream .................................................................................. 382
FilterInputStream ............................................................................... 383
PipedInputStream ............................................................................... 389
SequenceInputStream .......................................................................... 389
StringBufferInputStream ..................................................................... 390
Output Streams ...................................................................................... 391
The abstract Class OutputStream ........................................................ 391
ByteArrayOutputStream ...................................................................... 392
FileOutputStream ............................................................................... 393
FilterOutputStream............................................................................. 394
PipedOutputStream ............................................................................ 399
Related Classes ....................................................................................... 399
Summary ................................................................................................ 399
Q&A ...................................................................................................... 400
Day 20 Native Methods and Libraries 403
Disadvantages of native Methods ............................................................ 404
The Illusion of Required Efficiency ........................................................ 405
Built-In Optimizations ...................................................................... 407
Simple Optimization Tricks............................................................... 407
Writing native Methods .......................................................................... 408
The Example Class ............................................................................ 409
Generating Header and Stub Files ..................................................... 410
Creating SimpleFileNative.c .............................................................. 414
030-4 FM
1/29/96, 8:11 PM
16
M T W T
F S Sxvii
abcd
P2/V4SQC6 TY Java in 21 Days 030-4 louisa 12.31.95 FM LP#4
A Native Library ..................................................................................... 417
Linking It All ..................................................................................... 418
Using Your Library ............................................................................ 418
Summary ................................................................................................ 418
Q&A ...................................................................................................... 419
Day 21 Under the Hood 421
The Big Picture ...................................................................................... 422
Why It’s a Powerful Vision ................................................................ 423
The Java Virtual Machine ....................................................................... 423
An Overview ...................................................................................... 424
The Fundamental Parts...................................................................... 426
The Constant Pool ............................................................................ 430
Limitations ........................................................................................ 430
Bytecodes in More Detail ....................................................................... 431
The Bytecode Interpreter ................................................................... 431
The “Just-in-Time” Compiler ............................................................ 432
The java2c Translator ........................................................................ 433
The Bytecodes Themselves ................................................................ 434
The _quick Bytecodes ........................................................................ 450
The .class File Format ............................................................................. 452
Method Signatures ................................................................................. 454
The Garbage Collector ........................................................................... 455
The Problem ...................................................................................... 455
The Solution ...................................................................................... 456
Java’s Parallel Garbage Collector ........................................................ 459
The Security Story .................................................................................. 459
Why You Should Worry .................................................................... 459
Why You Might Not Have To .......................................................... 460
Java’s Security Model ......................................................................... 460
Summary ................................................................................................ 470
Q&A ...................................................................................................... 470
A Language Summary 473
Reserved Words ...................................................................................... 474
Comments .............................................................................................. 475
Literals .................................................................................................... 475
Variable Declaration ............................................................................... 476
Variable Assignment ............................................................................... 476
Operators ............................................................................................... 477
Objects ................................................................................................... 478
Arrays ..................................................................................................... 478
Loops and Conditionals .......................................................................... 478
Class Definitions .................................................................................... 479
Method and Constructor Definitions ..................................................... 479
Packages, Interfaces, and Importing ........................................................ 480
Exceptions and Guarding ....................................................................... 481
030-4 FM
1/29/96, 8:12 PM
17
Sams.net
Learning
CenterTeach Yourself JAVA in 21 Days
21
xviii
P2/V4SQC6 TY Java in 21 Days 030-4 louisa 12.31.95 FM LP#4
B Class Hierarchy Diagrams 483
About These Diagrams ........................................................................... 495
C The Java Class Library 497
java.lang ................................................................................................. 498
Interfaces ........................................................................................... 498
Classes ............................................................................................... 498
java.util .................................................................................................. 499
Interfaces ........................................................................................... 499
Classes ............................................................................................... 499
java.io ..................................................................................................... 500
Interfaces ........................................................................................... 500
Classes ............................................................................................... 500
java.net ................................................................................................... 501
Interfaces ........................................................................................... 501
Classes ............................................................................................... 502
java.awt .................................................................................................. 502
Interfaces ........................................................................................... 502
Classes ............................................................................................... 502
java.awt.image ........................................................................................ 504
Interfaces ........................................................................................... 504
Classes ............................................................................................... 504
java.awt.peer ........................................................................................... 505
java.applet ............................................................................................... 505
Interfaces ........................................................................................... 505
Classes ............................................................................................... 505
D How Java Differs from C and C 507
Pointers .................................................................................................. 508
Arrays ..................................................................................................... 508
Strings .................................................................................................... 508
Memory Management ............................................................................ 509
Data Types ............................................................................................. 509
Operators ............................................................................................... 509
Control Flow .......................................................................................... 510
Arguments .............................................................................................. 510
Other Differences ................................................................................... 510
Index 511
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论