Академический Документы
Профессиональный Документы
Культура Документы
75
94
681.3.07
""
. ..
. . .. , ..
. . ..
"" :
info@dialektika.com,
http://www.dialektika.com
115419, , / 783; 03150, , / 152
, , , .
94
# 2005 "".: . . . : ".. ", 2008. 576 . :
. . . .
ISBN 978-5-8459-1068-4 (.)
,
#.
. - ,
# ,
.
, #,
- ,
,
Windows-.
:
,
, .
32.973.26-018.2.75
.
, ,
, JOHN
WILEY&Sons, Inc.
Copyright 2008 by Dialektika Computer Publishing.
Original English language edition Copyright 2006 by Wiley Publishing, Inc., Indianapolis, Indiana.
All rights reserved including the right of reproduction in whole or in part in any form. This translation is pub
lished by arrangement with Wiley Publishing, Inc.
Wiley, the Wiley Publishing logo, For Dummies, the Dummies Man logo, A Reference for the Rest of Us!, The
Dummies Way, Dummies Daily, The Fun and Easy Way, Dummies.com, and related trade dress are trademarks or
registered trademarks of John Wiley & Sons, b e , and/or its affiliates in the United States and other countries, and
may not be used without written permission. All other trademarks are the property of their respective owners. Wiley
Publishing, Inc., is not associated with any product or vendor mentioned in this book.
ISBN 978-5-8459-1068-4 ()
, ,
by Wiley Publishing, Inc., 2006
- "", 2008,
19
I. #
27
1. Windows- #
29
2. #
47
II. #
55
3. -
57
4.
73
5.
85
III. -
6.
115
7.
141
8.
177
9. #
199
IV. -
223
10. -
225
11.
231
12.
261
13.
283
V.
309
14.
311
15.
339
VI.
373
16.
375
17. # ++
385
VII.
391
18.
393
19.
419
20.
445
2 1 . Visual Studio
487
22. #
525
565
17
19
I. #
27
1. Windows- #
29
, # .NET
?
#?
.NET?
Visual Studio 2005? Visual #?
Windows- #
Windows Forms
Visual Basic 6.0, !
29
30
30
31
32
32
33
36
37
42
43
44
2. #
47
47
47
49
49
51
51
51
52
II. #
55
3. -
57
int
int
57
58
59
59
60
61
62
63
'
decimal
,
?
char
.
string
-?
string char
63
64
64
65
65
66
66
66
67
67
68
69
70
4.
73
73
73
74
75
76
77
78
79
80
80
82
83
5.
85
if
else
else
if
86
86
89
90
90
while
do...while
break continue
for
switch
goto
93
93
98
98
99
103
104
104
105
106
109
111
III. -
6.
115
,
-
115
116
117
117
120
122
123
124
#
foreach
124
125
125
127
130
133
134
7.
141
141
143
149
150
-
return
DOS
Visual Studio 2005
150
152
153
154
156
162
162
163
163
166
167
169
170
173
8.
177
this
177
179
179
181
182
183
185
t h i s
this
Visual Studio
XML-
185
188
190
191
192
193
197
9. #
199
!
switch
Trim() Pad()
Split()
200
200
201
205
205
206
207
210
212
212
215
217
218
IV. -
223
10. -
225
- 1
"-"
- 2
- 3
- 4
- #
225
226
226
227
227
228
229
229
11.
231
, #
231
232
235
235
236
237
242
242
244
244
10
246
247
249
252
252
253
256
260
12.
261
BankAccount
,
#
.
is as
BankAccount
261
263
264
267
267
268
269
270
270
270
271
272
274
274
276
278
281
13.
283
is
#
,
283
284
284
285
289
291
292
293
294
297
297
302
302
304
304
308
11
309
V.
14.
311
__
, __
#
#
-
311
312
313
315
315
316
317
323
323
326
327
329
329
330
333
333
334
337
15.
339
<>
List<T>
Main()
()
340
340
341
343
343
344
344
345
345
347
348
352
353
355
356
358
360
362
363
363
364
364
365
366
12
using
StreamWriter
StreamReader
422
424
425
426
427
430
430
431
432
434
435
440
20.
445
:
:
:
foreach
:
445
451
452
461
461
462
464
465
465
465
469
473
474
475
476
479
487
Solution Explorer
!
F1
487
488
490
490
491
493
493
494
495
497
498
501
502
506
506
14
: ?
!
507
509
510
511
512
512
514
517
520
523
22. #
525
.NET
# SharpDevelop
SharpDevelop
SharpDevelop Visual Studio
SharpDevelop
SharpDevelop
# TextPad
. CS #
: Build # Debug
Debug Release
NUnit
NUnit
NUnit
Windows Forms Form Designer
,
Visual Studio
526
526
527
528
528
529
530
531
531
532
534
534
537
538
540
541
545
545
548
548
549
550
557
559
559
560
561
562
563
564
565
15
VI.
373
16.
375
The name 'memberName' does not exist in the class or namespace 'className'
Cannot implicitly convert type 'x' into 'y'
'className.memberName' is inaccessible due to its protection level
Use of unassigned local variable 'n'
Unable to copy the file 'programName.exe' to 'programName.exe'. The process cannot...
'subclassName.methodName' hides inherited member 'baseclassName.methodName'.
Use the new keyword if hiding was intended
'subclassName' : cannot inherit from sealed class 'baseclassName'
'className' does not implement interface member 'methodName'
'methodName' : not all code paths return a value
} expected
375
377
379
380
380
17. # ++
385
-
# ++
386
386
387
387
388
388
389
389
389
389
VII.
391
18.
393
381
382
383
383
384
catch-
Exception
393
395
398
400
402
405
406
408
411
412
413
19.
419
419
421
422
13
. (Stephen R. Davis) ( )
, .
, + + ( + + For Dummies) + + Weekend
Crash Course. L-3 Communications.
(Chuck Sphar) Microsoft,
++, 1997
. M F C .
( a g a i n s t r o m e . c o m )
.NET.
c s h a r p @ c h u c k s p h a r . com.
( ) .
""
.
, , .
, ,
.
, .
.
, Web-
. , ,
, ,
.
, ,
.
. :
E-mail: inf o @ w i l l i a m s p u b l i s h i n g . com
WWW:
h t t p : //www. w i l l i a m s p u b l i s h i n g . c o m
:
:
115419, , / 783
: 03150, , / 152
# ,
, ++ Java. ,
.
# Microsoft .NET. ,
Microsoft #
( "-") 2000
, .NET.
#,
, , .
, # Microsoft
, Visual Studio .NET
#. , , Visual Studio
Visual Studio 2003 , ,
Visual Studio 2005. , , #.
#
, Unix, Mono
Portable .NET .NET #.
Mono (www. g o - m o n o . com)
, Novell Corporation. 1.1.8
2005 . Mono .NET Mi
crosoft ( 1.1 Microsoft ), .
Portable .NET Southern Storm Software DotGNU (www. d o t g n u . o r g /
p n e t . h t m l ) .
Portable .NET 0.7.0.
# Windows
Unix, Linux Macintosh Apple.
, Portable .NET ,
Mono .NET.
, ,
. (
. www. a m a z o n . com.)
-
.
# .NET
. , #
Windows, ,
,
Java Sun Microsystems .
, , , Microsoft. , -
, ,
#
. Microsoft.
Visual Studio Microsoft
# .NET,
.
#, ,
#. ( ,
, Visual Studio
2005, Windows
, .)
2.0 # , #
, .
.
S
: ,
, .
, # 2.0 .
15, " ".
!
,
. 15, " ",
,
.
,
. ( ,
- .
.)
#,
. ,
Microsoft Visual Studio,
. Visual Studio, Visu
al Studio . : "
", : "
# Visual Studio <F5>". ,
#, , .
, , ,
# Windows, #
,
20
#. #,
. Windows
, #,
Windows. ,
# ,
. -
. #
. .NET
, # .NET
Framework, .
, #, ,
, (Common Language Runtime CLR). Visual Stu
dio 2005 CLR .
.NET, #
, Web- Microsoft
h t t p : / / m s d n . m i c r o s o f t . c o m .
.NET (Software Development Toolkit SDK).
, ,
Visual Studio 2003. ,
, # 2.0,
.
Visual Studio 2005 # Express 2005, ,
.
Ka
, .
? .
# Visual Studio.
.
,
. , ,
, .
. ,
.
, ,
. ,
, ,
20 .
, , " "
; -,
.
21
I, " # "
Windows Visual Studio 2005.
, #,
.
V, " "
, - .
- .
: , ,
- .
22
, .
#
, , .
# .
# ++.
CD-ROM
- .
. , -
. SharpDevelop
,
, ,
Visual Studio.
. TextPad
.
#. NUnit,
#, , Visual Studio, SharpDevelop TextPad.
- .
, , ReadMe,
.
.
,
.
,
.
, .
. ,
, .
,
-. ,
, .
#,
.
23
, . ,
" ", ,
. :
use System;
n a m e s p a c e MyNameSpace
public
class
MyClass
{
}
.
-,
.
, , , , : "
F i l e ^ O p e n W i t h O N o t e p a d " . , File.
O p e n W i t h , ,
N o t e p a d .
, # (
# 2005 , ).
#,
Windows.
Windows, ,
.
# .
, h t t p : / / m s d n . m i c r o s o f t . com/
msdn. , Web-
#, ,
,
. #:
http://msdn.microsoft.com,
, # .NET;
S
S
#;
S w w w . c s 2 t h e m a x . c o m .
Web- www. c h u c k s p h a r . com,
(FAQ). -, -
24
, , FAQ
. , , .
,
.
25
, #,
, .
Windows,
Windows Visual
Studio 2005.
# ,
.
Windows- #
...
> ? #?
> Windows-
> Visual Studio 2005
, ,
# Visual Studio 2005.
Windows-, #.
, .
, ( , ),
, .
. , "", '"".
, . ,
, : " ,
. ,
. , -".
, .
, , .
, .
.
,
,
. , ,
MOV , ( ,
Intel). ,
.
w w w . g o t d o t n e t . c o m / t e a m / l a n g ) . #
.NET. # ,
.NET.
. .NET
Visual Studio 2005
, Windows .NET.
.NET Microsoft
, XML SOAP. , , .NET
, Web (Web Services).
# Visual Studio,
Windows-. Windows
Windows, WinApps WinForms .
#, ,
, Web-,
, Windows. ,
Windows Forms .
.
32
I. #
.
, , ,
, . ,
(, #),
. ( .)
?
? , Windows
, . ,
Microsoft Word, , .
.
. .
.
. #
, #,
#. , , -
, .
# ?
# ,
.
, ++, , Visual Basic
, 6.0 . ( Visual Basic
.NET , #.
.NET, #
.) # . c s .
, "-" , "-",
, .
# .
": # ,
.
: # , ++,
: .
1 : # ,
> ++, .
: .NET, #
iHorax , ,
, ,
: , .
: # .NET,
Microsoft
Vindows . .NET -.
30
I. #
I / : , ,
.
, #
.NET.
.NET?
.NET Microsoft
, , .
, Microsoft.
, Windows.
#,
, Visual Basic .
# .NET.
++. Sun Microsystems
Java. Sun ++,
.
"",
, ,
.
Microsoft Java ,
Sun - ,
. Microsoft - Java
.
Java , Java :
,
Java, . Microsoft
,
Microsoft
. .NET.
.NET ,
Java, # Java. Java ,
. #
, #. , ,
, .
, ,
Windows.
Microsoft , .NET Web Sun, Java, . Java,
.NET .
Visual Basic , ""
Web ( , "",
). .NET Microsoft
, (
1. Windows- #
31
Windows Forms,
Visual Studio. .
Windows... , Windows.
, , Visual Studio
, .
Windows , ,
.
Windows .
Visual Studio 2005 #
WinApp. ,
, .
Visual # Windows,
Windows . , Visual Studio
2005 (Application Wizard),
.
,
. .
. ,
.
Visual Studio 2005
.
1. Visual Studio 2005 StartoAII Programs^
Visual Studio. , ...
2. F i l e ^ N e w ^ Project, . 1.2.
3. Visual Studio N e w Project,
. 1.3.
, Visual Studio
. #,
. CS. CSPROJ.
1. Windows- #
33
. 1.1. , #
34
I. #
1. Windows- #
35
Windows Forms
,
. #
Windows, ,
.
# Win
dows ( ).
, Visual #
.
Windows Forms,
.
1. Build<=>Build projectname ( projectname
W i n d o w s A p p l i c a t i o n l MyProject)
Output. ,
. V i e w ^ O t h e r W i n d o w s ^ O u t p u t .
Build. Output .
: B u i l d : 1 s u c c e e d e d , 0 f a i l e d , 0 s k i p
p e d ( - ).
" ". Output,
B u i l d s u c c e e d e d B u i l d f a i l e d Start.
. 1.4 , Visual Studio
Windows, Output.
. , .
F o r m s D e s i g n e r Output.
" F o r m l . c s
[Design]".
2. , D e b u g s
Start Without D e b u g g i n g .
, ,
F o r m s Designer, . 1.5.
# .
Minimize,
Maximize Close.
3. C l o s e
.
! # .
.
, , Visual Studio
, .
, ,
Windows. ...
36
I. #
Solution Explorer
. 1.4. Windows
. 1.5. Windows
,
, Visual Studio 2005
Windows ,
. Visual Studio " F o r m l . cs
1. Windows- #
37
F o r m s D e s i g n e r ,
Windows Forms. ,
. ( S o u r c e ) , ( T a r g e t ) .
, S o u r c e
Target. .
, Visual Studio,
(document windows) (control win
dows). ,
, , #, #. ,
Solution Explorer, . 1.4,
Visual Studio . ,
Visual Studio .
(controls). Windows,
,
Windows. F o r m s D e s i g n e r
T o o l b o x .
1
T o o l b o x , V i e w ^ T o o l b o x . . 1.6
Visual Studio T o o l b o x .
. 1.6. Toolbox
, , . 1.6. ,
T o o l b o x , .
. ,
.
38
I. #
T o o l b o x , Data, C o m p o n e n t s W i n d o w s
Forms. , ,
, . T o o l b o x
, .
"" C o m m o n Controls ( All W i n d o w s
Forms), , . 1.6.
.
, T o o l b o x .
. T o o l b o x
, .
1. Textbox. ,
F o r m l , .
, T o o l b o x .
, .
t e x t B o x l . ,
. ( N a m e ,
T e x t ,
N a m e ) . ,
.
.
, .
, ,
.
2. Textbox
.
,
.
.
3. Button
.
.
4. ,
, .
. 1.7.
.
.
b u t t o n l .
.
IM #
/. Windows- #
39
. 1.7.
,
, .
Properties.
.
1. , .
2. Properties V i e w ^ Properties
Window.
:
, .
T e x t , A p p e a r a n c e . (
, ,
AZ .)
3. Properties
Text. <Enter>.
. 1.8 Properties
. .
. 1.8. Properties
4. T e x t b o x .
,
40
I. #
,
. ,
.
5. Text
. - ,
Text <Enter>.
" ".
6. AcceptButton (
Misc Properties), Accept
Button , ,
<Enter>. buttonl.
" " , b u t t o n l . . N a m e
F o r m , .. , .
7. Behavior,
Readonly.
True , . 1.9.
Forms Designer.
B u i l d ^ B u i l d Windows Application 1,
. Windows ,
. Output 1 s u c
ceeded, 0 f a i l e d , 0 s k i p p e d .
1. Windows- #
41
. 1.10.
, ,
, .
, . Ap
pearance, , .
,
.
1. Forms Designer .
2. Properties
, .
. ,
.
Click. , ,
, . .
3. Click , .
Design .
Code, #,
. Visual Studio "",
, , #.
Forms Designer.
Click Visual Studio
Code .
b u t t o n l _ C l i c k ( ) . ,
textBoxl
textBox2.
42
I. #
, .
8, " ". .
T e x t t e x t B o x l t e x t B o x 2 .
4. buttonl " " ,
Refactor. but
Name
CopyClick.
<Enter>
( ).
.
Visual Studio 2005 Refactor
. ,
b u t t o n l _ C l i c k
- , Visual Studio .
,
: ,
. ,
. P r e v i e w C o d e C h a n g e s ,
. Refactor,
, .
5. C o p y C l i c k ( ) :
textBox2.Text
textBoxl.Text;
, #
. . 1.11
.
, .
.
( ,
<Ctrl+Space>).
6. B u i l d ^ B u i l d WindowsApplicationl
.
D e b u g o S t a r t Without D e b u g g i n g ,
. -
.
, . 1.12. ,
, .
, ,
. ,
, #!
1. Windows- #
43
. 1.11.
. 1.12. !
, ,
.
Windows, ,
,
, .
44
I. #
, ,
, Visual Basic. F o r m s Designer,
# Visual Basic .NET, Vis
ual Basic 6.0, , , , ,
. Visual Basic 6.0
, .
1. Windows- #
45
#
>
>
>
#
Windows. ? 1, "
Windows- #". # ,
,
,
.
Visual Studio
. .
, .
#.
, , #.
Visual Studio.
Visual Studio, ,
#.
#.
1. F i l e ^ N e w ^ P r o j e c t
.
Visual Studio ,
, .
...
namespace
class
ConsoleAppTemplate
Program
static
void Main(string[]
args)
I
(+)
(-) . + u s i n g . . ..
Visual Studio
,
( !).
:
48
I. #
using S y s t e m ;
using S y s t e m . C o l l e c t i o n s . G e n e r i c ;
using S y s t e m . T e x t ;
, ,
, . ,
, , .., + / -
, # r e g i o n .
, # r e g i o n ,
, # e n d r e g i o n .
, , - P u b l i c m e t h o d s . ,
. ,
( Visual Basic), .
u s i n g S y s t e m .
; - ,
.
# ,
B u i l d ^ B u i l d C o n s o l e A p p T e m p l a t e . Visual Studio
:
-Build s t a r t e d : P r o j e c t :
Debug Any CPU Csc.exe
/noconfig
ConsoleAppTemplate,
/nowarn
Configuration:
( a n d much m o r e )
Compile c o m p l e t e - - 0 e r r o r s , 0 w a r n i n g s
ConsoleAppTemplate - > : \ C # P r o g r a m s \ . . . ( a n d m o r e ) = = B u i l d :
1 s u c c e e d e d or u p - t o - d a t e , 0 f a i l e d , 0 s k i p p e d = =
1
s u c c e e d e d .
: "succeeded" , "failed"
.
D e b u g s S t a r t W i t h o u t D e
bugging. .
, . , .
.
P r o g r a m . c s , :
using S y s t e m ;
namespace C o n s o l e A p p T e m p l a t e
{ //
2. #
49
// Program ,
p u b l i c c l a s s Program
{
// -
/ / M a i n ( )
s t a t i c void Main(string[] args)
{
/ / , -
/ /
C o n s o l e . W r i t e L i n e ( " , : " ) ;
//
s t r i n g sName = C o n s o l e . R e a d L i n e ;
//
Console.WriteLine(" , " + sName);
/ /
C o n s o l e . W r i t e L i n e ( " < E n t e r > " +
"
...");
Console.Read();
/ / M a i n ( )
} // M a i n ( )
} // Program
} // C o n s o l e A p p T e m p l a t e
, I
(// / / / ) , , I
. - I
.
P r o g r a m . c s I
C o n s o l e A p p T e m p l a t e . e x e , B u i l d s Build C o n
soleAppTemplate.
Visual Studio 2005,
D e b u g ^ S t a r t Without D e b u g g i n g . , ( , ). ,
< E n t e r > . . . . <Enter> .
DOS.
DOS :
CD \ C # P r o g r a m s \ C o n s o l e A p p T e m p l a t e \ b i n \ D e b u g
C o n s o l e A p p T e m p l a t e .
, .
\ C # P r o g r a m s \ C o n s o l e A p p T e m p l a t e \ b i n \ D e b u g Windows
C o n s o l e A p p T e m p l a t e . .
DOS,
T o o l s ' ^ C o m m a n d W i n d o w .
Visual Studio T o o l s , S t a r t s Ail P r o g r a m s 1 ^
I. #
I
|
1
I
|
,
.
:
using
using
using
System;
System.Collections.Generic;
System.Text;
namespace
ConsoleAppTemplate
,
Main (), . ,
.
u s i n g
namespace C o n s o l e A p p T e m p l a t e {. .
.NET. ,
u s i n g ,
.
, ,
:
//
public s t a t i c v o i d M a i n ( s t r i n g [ ]
args)
# .
, // / / / ,
,
#. / / / / /
.
,
? .
2. #
51
. ,
, , ?
, (
- ), .
.
. .
,
# .
, M a i n ( ) :
/ /
C o n s o l e . W r i t e L i n e ( " , : " ) ;
/ /
s t r i n g sName = C o n s o l e . R e a d L i n e ( ) ;
//
C o n s o l e . W r i t e L i n e ( " , " + sName),
,
Code Snippets ( ),
, C o n s o l e . W r i t e L i n e .
<Ctrl+K>, <Ctrl+X>
Code Snippets. cw <Enter>. Visual
Studio C o n s o l e . W r i t e L i n e ()
.
c w , f o r i f , ,
: cw <>.
( ,
<Ctrl+K>, <Ctrl+S>. if
i f . ) , .
#: C o n s o l e . W r i t e L i n e .
, : .
s N a m e ( 3, "
-").
, ,
.
,
<Enter>. ,
:
/ /
C o n s o l e . W r i t e L i n e ( " < E n t e r > " +
" . . . " ) ;
Console.Read();
52
I. #
, ,
. Visual Studio .
D e b u g ^ S t a r t , Visual Studio
.
Windows.
, ,
<Enter>
.
C o n s o l e . W r i t e L i n e
, , M a i n ()
.
C o n s o l e . W r i t e L i n e C o n s o l e . Read.
.
2. #
53
,
.
,
.
.
-
> #
>
>
>
>
>
. # ,
( , ) .
. , :
, , , , ,
1 .. - (, ..).
I . # :
"" 1.
1 ,
.
, #
, .
: " 1", , 1.
,
:
2
= +2+1
= + 1 ,
. , -
. ,
, 1 , , -
" 1", - .
.
.
, # :
: "
" . , ,
. : "
1 ,
". : " , I
, , 1".
(=) .
: " 1". # :
" 1 " . # ,
. , ,
.
, .
int
.
, ,
.
, .
:
= 1;
= 1.1;
= House;
= " ! "
,
.
# .
. ,
. " ", ,
.
,
# i n t . ,
(1, 2, 3 ..), 0
1, -2, - 3 . . .
, .
i n t ,
, :
//
i n t ;
58
II. #
// m 2
int m = 2 ;
// , ,
= , ,
, .
,
. m i n t ,
2.
.
.
,
.
, m ( 2),
. 2,
( , ).
:
//
// 1
int 0 = 1 ;
int
1 i n t ,
1 .
. , ,
# , .
(, ) .
, ,
- . , :
// , m
/ /
int m;
n = m;
// ,
//
= 2;
'int ;
.
int
i n t . #
.
3. -
59
, !
. , i n t !
-2 2 .
, . -
, # , 1
( l o n g i n t ) , -
"": 8 (64 ) 4- i n t .
# , . 3.1.
, 1
. , 1 i n t . , i n t , , , 123U
( U) u i n t .
(signed), .. . (unsigned)
,
.
. 3.1,
.
. 6
, - . -'
6 , .
,
.
:
, . . .
60
II. #
// 4 5 F
int nFahr = 4 1 ;
int n C e l s i u s = ( n F a h r - 3 2 ) * ( 5 / 9 ) ;
.
, 41 F 5. ", !" 6 .
, 100F. :
100-32 = 6 8 ; 68 (5/9) 37. " , ,
37.78". ,
37.777..., 7 , , ,
.
i n t .
37.78 37. , ,
(truncation).
, (rounding).
,
. , 1.9 1, 2.
37 .
37.78 37. ,
, .
. i n t 5/9
0. , n C e l
sius n F a h r . ,
, .
- C o n v e r t T e m p e r a t u r e W i t h R o u n d O f f ,
.
,
C o n v e r t T e m p e r a t u r e W i t h R o u n d O f f . e x e ,
.
, i n t ,
.
( 64- l o n g , ,
), .
,
, (real numbers).
,
?
? #
3. -
61
,
.. 1.5
, 1.0. , 1.0 + 0.1 = 1.1.
.
, # , .
.
.
,
:
float
1.0;
f l o a t ,
.
. 3.2 # .
(.. ,
, ).
, f l o a t {
, d o u b l e .
, , 12.3, # d o u b l e .
. 3.2 ,
. , 5/9 0.555... . f l o a t
6 , , . I
, 5/9 , f l o a t ,
0 . 5555551457382
, .
f l o a t 6.5 .
- ,
82
, 10'
. ,
.
5/9 d o u b l e :
0.55555555555555557823
d o u b l e 15-16 .
62
II. #
# d o u b l e ,
d o u b l e ,
. d o u b l e f l o a t
,
.
:
dCelsius =
(dFahr - 3 2 . 0 )
(5.0 / 9 . 0 ) ;
- d o u b l e -
C o n v e r t T e m p e r a t u r e W i t h F l o a t .
ConvertTem
peratureWithFloat:
: 1 0 0
: 3 7 . 7 7 7 7 7 7 7 7 7 7 7 7 8
Press Enter to t e r m i n a t e . . .
,
. , ,
, ? ,
.
.
# (1, 2, 3 ..). , 1.0, 2.0, 3.0
, 1, 2, 3, #
. , #
, 1.000001?
.
.
, 12.5 12.500001.
, , #
12.500000 12.500001 .
, 1.1 1.1, 2.2
2.200001. : " dDouv l e v a r i a b l e . 2.2?",
, .
, , : " d D o u v l e V a r i
able 2.2 0.000001?", ,
.
3. -
63
Pentium ,
,
80 . |
64- , .
86, ]!
Windows, , |
. .
Pentium III (, ]
) , 300000000
, 3 1.
d o u b l e i n t . (
.)
, .
l o n g .
f l o a t ,
. , 1 2 3 4 5 6 7 8 9F
, 1 2 3 4 5 6 0 0 0 F . ( , F
, .)
, ,
. ,
- ,
. ,
, , :
S , ;
S
,
.. 12.5 12.5, 12.500001.
, # , d e c i m a l .
28
28
d e c i m a l 10" 10
! , .
decimal
d e c i m a l ,
:
64
II. #
decimal m l ;
decimal m2 = 1 0 0 ;
decimal m3 = 100M;
//
//
//
ml ml .
, .
, #
- .
2 100.
, 100 i n t . #
i n t d e c i m a l . , # ,
, .
,
10 d e c i m a l . ,
d e c i m a l , .
,
, d e c i m a l
, i n t d o u b l e .
.
d e c i m a l . -,
, , , ,
5, " ".
, ,
,
. 300000000
d e c i m a l 50 i n t .
. ,
, ,
d e c i m a l .
, d e c i m a l ,
, .
tnun?
, . b o o l
true f a l s e .
.
++
i n t f a l s e t r u e . #
.
b o o l :
bool t h i s I s A B o o l
= true;
b o o l
( , ). ,
3. -
65
b o o l i n t (, , f a l s e 0)
s t r i n g ( f a l s e " f a l s e " ) .
, ,
, ( -
).
, .
#
c h a r s t r i n g .
char
c h a r .
, :
char
' ' ;
, , ,
, ,
.
, c h a r , ..
, 5, " ".
, .
c h a r ,
c h a r , , ,
.
,
.
1
'
(, , ).
, . #
, . 3.3.
66
II. #
string
s t r i n g .
, .
//
string s o m e S t r i n g l ;
someStringl = " " ;
//
string s o m e S t r i n g 2 = " " ;
s t r i n g , ,
, .
, . 3.3.
#, ,
:
//
string s o m e S t r i n g = "
" ;
// -
string s o m e S t r i n g =
" \ " ;
C o n s o l e . W r i t e L i n e ,
:
, -
.
, , :
string s = " . " + " . " ;
s :
" . ."
, "" ( ),
s t r i n g , .
' \ 0 ' ,
("
").
, - (value types).
- .
#
Intel PC.
. , Intel
, ,
i n t . , ,
, , d e c i m a l s t r i n g ,
. ,
3. -
67
, : i n t . I
d o u b l e .
, ( , s t r i n g ) I
. -1
. = # !
b - , I
. -1
-.
i n t , d o u b l e , b o o l " " -1
i n t . I
d e c i m a l -. s t r i n g I
.
, 6, " ",
, ,
-. s t r i n g , #
.
string char
, s t r i n g
c h a r . , . ,
, . , c h a r
, ,
:
char cl =
char 2 =
char =
' ' ;
' 1 ;
cl + 2 ;
,
, . # cl 2
i n t ,
, .
,
i n t c h a r
. , .
, . ,
:
string si = " " ;
s t r i n g s2 = " b " ;
s t r i n g s3 = si + s2;
//
"ab"
# ,
9, " # " .
68
II. #
, .
# ,
, ,
.
, , ,
.
( ,
). , ,
, t h i s I s A L o n g V a r i a b l e N a m e .
.
f f l o a t , d d o u b l e , s s t r i n g .
i n t .
, , i, j
i n t .
,
.NET. ,
, .
Visual Studio
,
. " "
, , , .
, #:
. i n t ,
int. , + 1 i n t .
1?
:
. 2 (. . 3.1)
i n t . , , l o n g .
d o u b l e .
. 3.4 , , ..,
, .
, lu 1U .
3. -
69
. 3.'
1. 0
double
1 . OF
float
decimal
true
bool
false
bool
char
\'
1
c h a r ( )
\xl23
"a
string"
c h a r ( 123)
string
s t r i n g ( )
, , .
, ( #) ,
1, , "" "". #
, , . ,
i n t l o n g :
i n t nValue = 10;
long lvalue;
l v a l u e = nValue;
//
i n t l o n g ,
i n t l o n g
, . # ,
- .
. ,
:
long lvalue = 10;
i n t nValue;
nValue = l v a l u e ;
// !
, l o n g ,
i n t (, , 4 ). #
,
. .
, , ?
, l v a l u e l o n g ,
10 0.
l v a l u e l o n g n V a l u e i n t .
#, , ,
:
long lvalue = 10;
i n t nValue;
nValue = ( i n t ) l v a l u e ;
70
//
II. #
3.
. : "
l v a l u e i n t , ,
". , , .
,
, :
double dValue = 1 0 . 0 ;
long l v a l u e = ( l o n g ) d V a l u e ;
d e c i m a l
.
. b o o l , s t r i n g
.
# ,
"". ,
t r u e b o o l " t r u e " ;
.
.
3. -
71
>
I
>
, , ,
. 3, " -", ,
, ,
, - . ,
.
, +, -, =, < &. ,
^ .
I :
, ,
.
, .
.
. 4.1. ,
(*), ().
,
: ,
. , ,
:
i n t nl = 5;
i n t n2 = - n l ;
//
-5
- .
. - 1
. , 5%3 2 ( 5 / 3 = 1, 2), ^25%3 I
1 ( 2 5 / 3 = 8, 1).
% = ( / ) * + ( % ) .
( )
. - I
, .
. , - |
:
int = 5 * 3 + 2 ;
? 5 3,
2? 3 2, 5?
# , - I
, 17.
# , 24 6,
2 :
int
24/6/2;
, ,
. # ,
. I
, , .
, ,
.
, ( - ) I
.
.
:
int =
74
(7
3)
(4+
( 6 / 3 ) ) ;
//. # [
, ,
. #
6 / 3 , 2.
:
int = (7 % 3)
(4 + 2) ;
// 2 = 6 / 3
# , :
int n = 1 * 6 ;
/ / 6 = 4 + 2,
1 = 7 % 3
:
int = 6;
" " , , .
,
, ,
, :
int = 7 + 2
3;
//
7 +
( 2 * 3 )
# ++:
, .
, ( ).
,
:
= 5 * 3;
5 * 3 = 15 i n t .
int- i n t - 1 5 . ,
, , ,
, .. :
m= = 5 * 3;
.
15
15, 15
m 15 (
).
, (
):
int n;
int m;
= m = 2 ;
, ,
. , ,
( ), .
.
,
: .
[ 4.
75
#
, . ,
+=
1;
= +
1 ;
. I
. 4.2 .
4.2.
, , 1
:
n = n +
1;
//
# :
n +=
1;
//
, , , #
:
++;
// 1
, ..
1.
,
, : + + + + . , + + ,
, , + + , .
, .
, .
+ + , ++ int:
int ;
= 1;
int = ++;
76
II. #
n = 1;
int m = n + + ;
p m ? (:
1 2.) , 2, m 1.
+ + , + +
. 2.
- -
n = n - 1 . , .
?
1970- PDP-8 .
, #,
.
+ + =-1.
.
, ,
+ + = + 1 . ,
,
++ = + 1 .
, .
, .. ?
#
, . 4.3.
(logical comparisons),
true f a l s e , b o o l .
:
int m = 5;
int n = 6;
bool b = m > n;
b f a l s e , 5
, 6.
( ),
,
. . .
4.
77
4.3.
...
=.=
>
>= b
<
<=
!=
, f l o a t , dou
b l e , d e c i m a l c h a r . :
bool b;
b = 3 > 2;
b = 3 . 0 > 2.0;
1
> 'b' ;
b = 'a
b =
A'
1
b = ' A
<
'a';
<
'b' ;
//
//
//
//
//
//
//
//
//
true
true
f a l s e -
""
true
- ''
' '
true
-
false
b o o l .
, ==, s t r i n g (
, # ).
, . :
float
float
fl =
f2 =
bool
fl =
f2 =
bool
fl;
f2;
10;
fl /
=
9;
fl /
b2 =
3;
(3
f2)
==
fl;
3;
(3
f2)
==
fl;
,
=, ==. .
# ,
.
f 1. 2? ,
t r u e : 9/3 3, 3*3 9, 9 9. !
: 10/3 3.3333.... 3.3333...*3 9.9999....
9.9999... 10? ,
78
II. #
. Pentium #
, , t r u e .
f 1 f 2
:
Math.abs(fl-f2*3.0)<.0001;
//
. . .
t r u e . . 0 0 0 1
D o u b l e . E p s i l o n , .
d o u b l e .
, S y s t e m . M a t h ,
1
H e l p ^ I n d e x Math L o o k For.
b o o l ,
. 4.4.
! "". ,
! , , , .
. &
, , b t r u e ; | b ,
, , t r u e .
(
) t r u e , b
.. t r u e , f a l s e .
b o o l .
&,
, (bitwise).
i n t
. , 6&3 2 ( 0 1 1 0 2 & 0 0 1 1 2 0010 2 ),
6 | 3 7 ( 0 1 1 0 2 1 0 0 1 1 2 0111 2 ), 6 3 5 ( 0 1 1 0 2 0 0 1 1 2
0101 2 ). ,
.
,
. , . :
bool b =
(!)
4.
&
(2) ;
79
# 1 2,
, t r u e , ,
. , #
f a l s e , ,
t r u e .
&& ,
:
bool =
(1)
&&
(2);
# 1 ,
f a l s e , f a l s e 2
. 1 t r u e , # 2 , .
| | , :
bool b =
(1)
(2);
# 1, t r u e ,
b t r u e 2
. 1 f a l s e , # 2 , .
" " " ".
, .
:
int ;
= 5 * 5 + 7;
, 32.
, .
" ", :
. ,
:
typel
<>
type2
=>
type3
( "".) t y p e l t y p e 2
.
80
II. #
. ,
:
int
uint
long
float
decimal
double
* int
* uint
* long
* float
* decimal
* double
=>
<=>
=>
=>
<=>
int
uint
long
float
decimal
double
, 2 * 3 i n t * i n t *
int 6.
, , i n t
f l o a t . ,
? , , :
int n l = 1 0 ;
double d2 = 5 . 0 ;
double d R e s u l t = nl
d2 ;
-, C# i n t * d o u b l e . C#
.
. #
i n t * i n t d o u b l e * d o u b l e . # d2 i n t ,
(
). # nl d o u b l e
d o u b l e * d o u b l e .
(implicit promotion).
, # ,
, .
i n t d o u b l e ,
i n t d e c i m a l .
d e c i m a l .
, d o u b l e , , i n t ,
(demotion).
(up conversion),
(down conversion).
. #
.
, # ?
?
-
(cast), , ,
.
4.
81
, i n t * i n t :
int nl
double
double
= 10;
d2 = 5 . 0 ;
nResult =
nl
(int)d2;
# b o o l
b o o l .
.
,
, ,
.
:
i n t n l = 10;
i n t 2 = 5 . 0
nl;
,
, , .
: , # nl
d o u b l e . # d o u b l e ,
d o u b l e .
,
. #
, ,
d o u b l e i n t .
:
int nl
i n t n2
=
=
10;
(int) (5.0
nl) ;
( , .)
, .
nl d o u b l e , ,
d o u b l e i n t .
, 5 * 1
, #.
82
II. #
, - .
. ,
. :
b o o l ? 1
:
int = 1 ;
int = 2 ;
int nMax =
(a>b)
( ), . ,
.
1 2 ,
,
.
3
.
. i f ,
5, " ", , .
.
, 1 2 .
:
int = 1 ;
double b = 0 . 0 ;
i n t nMax = ( a > b ) ? : b;
,
nMax . b
, d o u b l e , .
?:
d o u b l e ,
i n t :
int = 1 ;
double b = 0 . 0 ;
i n t nMax;
/ /
nMax = ( i n t ) ( ( a > b )
/ / . . . :
nMax = ( a > b ) ? :
:
? : b) ;
(int)b;
#, ++
. . .
4.
83
> , ...
> w h i l e
> f o r
> s w i t c h
:
using S y s t e m ;
namespace H e l l o W o r l d
(
public
class
Program
{
/ /
s t a t i c void Main(string[] args)
{
/ /
C o n s o l e . W r i t e L i n e ( " : " ) ;
/ /
s t r i n g sName = C o n s o l e . R e a d L i n e ( ) ;
//
Console.WriteLine(", " + sName);
/ /
C o n s o l e . W r i t e L i n e ( " <Enter> "
"
...");
Console.Read();
,
#, . , .
,
-
( ?...),
.
. " " ,
!
, .
.
# i f :
if
()
II
}
//
//
if
b o o l (. 4, ""), ,
. ( true),
, .
.
if , :
// , - :
/ / 0 . . .
i f ( < 0)
{
// ... 0
= 0;
,
, , 0.
,
, ..
if ( < 0 ) = 0,-. ,
.
if
, .
, , (
). #:
//
decimal mlnterestPaid;
mlnterestPaid = mPrincipal * (mlnterest / 100);
/ /
decimal mTotal = m P r i n c i p a l + m l n t e r e s t P a i d ;
m P r i n c i p a l
m l n t e r e s t ( 1 0 0 ,
).
m l n t e r e s t P a i d ,
m T o t a l .
86
II. #
, ,
. ,
(, ,
...), C a l c u l a t e I n t e r e s t , -,
.
// C a l c u l a t e l n t e r e s t
//
// . ,
// .
using S y s t e m ;
namespace C a l c u l a t e l n t e r e s t
(
public
class
Program
{
public
s t a t i c void Main(string[]
args)
{
/ /
C o n s o l e . W r i t e ( " : " ) ;
string sPrincipal = Console.ReadLine();
decimal mPrincipal =
Convert.ToDecimal(sPrincipal);
// ,
if ( m P r i n c i p a l < 0)
{
C o n s o l e . W r i t e L i n e ( " "
" " ) ;
m P r i n c i p a l = 0;
}
/ /
C o n s o l e . W r i t e ( " : " ) ;
s t r i n g s l n t e r e s t = Console . ReadLine ;
decimal mlnterest =
Convert.ToDecimal(slnterest);
// ,
//
if ( m l n t e r e s t < 0)
{
C o n s o l e . W r i t e L i n e ( " "
" " ) ;
m l n t e r e s t = ,-
/ /
//
decimal mlnterestPaid;
mlnterestPaid = mPrincipal * (mlnterest / 100);
/ /
d e c i m a l mTotal = m P r i n c i p a l + m l n t e r e s t P a i d ;
/ /
C o n s o l e . W r i t e L i n e ( ) ; // skip a l i n e
Console.WriteLine(" = " + mPrincipal);
5.
87
Console.WriteLine(" = "+mInterest+"%");
Console.WriteLine();
C o n s o l e . W r i t e L i n e ( " = "
+ mlnterestPaid);
C o n s o l e . W r i t e L i n e ( " = " + m T o t a l ) ;
/ /
C o n s o l e . W r i t e L i n e ( " < E n t e r > "
" . . . " ) ;
Console.Read();
}
}
}
C a l c u l a t e l n t e r e s t !
. W r i t e -
L i n e ( ) , s t r i n g .
, . -
, . -
>
.
!
<Enter> s t r i n g R e a d L i n e ( ) . !
d e c i m a l , !
d e c i m a l , Con
v e r t . T o D e c i m a l ( ) . m P r i n c i p a l .
R e a d L i n e ( ) , W r i t e L i n e () T o D e c i m a l () - I
.
, . 7,
" ", .
- , ,
.
m P r i n c i p a l .
, .
. ,
,
W r i t e L i n e ( ) .
:
: 1 2 3 4
: 21
= 1234
= 21%
= 2 5 9 . 1 4
= 1493.14
< E n t e r >
...
88
II. #
:
: 1 2 3 4
-12.5
= 1234
= 0%
= 0
= 12 34
< E n t e r > . . .
if . #
, .
i f . Visual Studio
T o o l s O O p t i o n s , T e x t Editor, # ,
T a b s . S m a r t Indenting
, .
T a b S i z e .
else
. ,
m a x
:
// b
// m a x
int max;
// . . .
if ( > )
// . . .
max = ,-
max
- '
"
// . . .
if ( <= )
{
// . . . b m a x
max = b ;
}
'
if ,
. , .
# e l s e , ,
, i f .
e l s e :
// b
// m a x
int max;
5.
89
/ /
i f ( > )
...
{
// . . .
//
max = ,-
max;
}
else
{
// . . .
max = b ;
max
}
, ; -|
. max .
else
e l s e , |
e l s e ,
. ,
:
//
// max
i n t max;
// ,
max = ,/ / . . .
i f (b > )
{
// . . .
max = b ;
max b
}
, , ml
. , , , !
. . .
if
C a l c u l a t e l n t e r e s t ,
,
. . , ,
,
, .
, ,
? ,
.
, , ,
. if
.
90
II. #
i f , i f ,
(embedded) (nested).
CalculatelnterestWithEmbedded-
T e s t if ,
.
// C a l c u l a t e I n t e r e s t W i t h E m b e d d e d T e s t
//
//
. ,
//
//
.
using S y s t e m ;
namespace C a l c u l a t e I n t e r e s t W i t h E m b e d d e d T e s t
{
public
class
Program
{
public
s t a t i c void Main(string[]
args)
{
//
//
int nMaximumlnterest = 50;
/ /
//
C o n s o l e . W r i t e ( " : " ) ;
string sPrincipal = Console.ReadLine;
decimal mPrincipal = C o n v e r t . T o D e c i m a l ( s P r i n c i p a l ) ;
/ /
if ( m P r i n c i p a l < 0)
...
II...
...
C o n s o l e . W r i t e L i n e ( "
");
}
else
I {
// . . .
//
C o n s o l e . W r i t e ( " : " ) ;
string slnterest = Console.ReadLine;
decimal mlnterest = Convert.ToDecimal(slnterest);
/ /
// . . . if
(mlnterest <
m l n t e r e s t > nMaximumlnterest)
{
/ / . . .
C o n s o l e . W r i t e L i n e ( "
" " +
5.
"
91
" "
+ nMaximumlnterest) ;
m l n t e r e s t = 0;
}
else
{
// ,
//
//
decimal mlnterestPaid;
mlnterestPaid = mPrincipal *
//
d e c i m a l mTotal
//
(mlnterest / 1 0 0 ) ;
= mPrincipal
+ mlnterestPaid;
C o n s o l e . W r i t e L i n e () ,// s k i p a l i n e
Console.WriteLine(" = "
+ mPrincipal);
Console.WriteLine(" = "
+ m l n t e r e s t + "%") ;
Console.WriteLine();
C o n s o l e . W r i t e L i n e ( " = "
+ mlnterestPaid);
C o n s o l e . W r i t e L i n e ( "
= "
o t a l), +
mT
//
C o n s o l e . W r i t e L i n e ( " < E n t e r > "
" . . . ") ;
Console.Read();
. ,
. , e l s e .
. ,
.
if :
if
(mlnterest
<
||
mlnterest
> nMaximumlnterest)
, m l n t e r e s t 0 nMaximu
m l n t e r e s t . , n M a x i m u m l n t e r e s t
, .
.
92
II. #
.
. n M a x i m u m l n t e r e s t ,
50.
, .
. ,
n M a x i m u m l n t e r e s t .
n M a x i m u m l n t e r e s t , , 60,
, .
6, "
".
,
:
: 1 2 3 4
: - 1 2 . 5
< E n t e r > . . .
50.
,
:
: 1 2 3 4
:12.5
= 1234
= 1 2 . 5 %
= 1 5 4 . 2 5 0
= 13 8 8 . 2 50
< E n t e r > . . .
i f
b o o l .
, ,
i f .
.
C a l c u l a t e l n t e r e s t " if
.
, .
, ?
(, Microsoft Excel
, , ).
,
. (loop).
while
w h i l e
:
5.
93
while{)
{
II ,
//
}
w h i l e . ,
.
, .
- , ,
, .
,
.
. , w h i l e , ,
, . ,
, ,
, . ,
;
, .
w h i l e C a l c u l a t e l n t e r e s t T a b l e , C a l c u l a t e l n t e r e s t
. .
// CalculatelnterestTable
/ /
//
using System;
namespace C a l c u l a t e l n t e r e s t T a b l e
{
using
System;
public
class
{
public
Program
s t a t i c void Main(string[]
args)
//
//
i n t nMaximumlnterest = 50;
/ /
//
C o n s o l e . W r i t e ( " : " ) ;
string sPrincipal = Console.ReadLine();
decimal mPrincipal = Convert.ToDecimal(sPrincipal);
//
if ( m P r i n c i p a l < 0)
...
94
. #
II...
...
C o n s o l e . W r i t e L i n e ( " " ) ;
}
else
{
// . . .
//
C o n s o l e . W r i t e ( " : " ) ;
string slnterest = Console.ReadLine();
decimal m l n t e r e s t = C o n v e r t . T o D e c i m a l ( s l n t e r e s t ) ;
/ /
// . . .
if ( m l n t e r e s t <
I m l n t e r e s t > nMaximumlnterest)
/ / . . .
C o n s o l e . W r i t e L i n e ( " " +
" " +
" " +
nMaximumlnterest);
m l n t e r e s t = 0;
}
else
/ / ,
// ,
/ /
//
C o n s o l e . W r i t e ( " : " ) ;
string sDuration = Console.ReadLine();
int nDuration = Convert.ToInt32(sDuration);
/ /
Console.WriteLine();
/ /
Console.WriteLine(" = "
+ mPrincipal);
Console.WriteLine(" = "
+ m l n t e r e s t + "%") ;
Console.WriteLine("
= "
+ nDuration + " y e a r s " ) ;
Console.WriteLine();
/ /
i n t nYear = 1;
w h i l e ( n Y e a r <= n D u r a t i o n )
{
//
decimal mlnterestPaid;
mlnterestPaid = mPrincipal * (mlnterest / 100);
/ /
mPrincipal = mPrincipal + mlnterestPaid;
C#
^ 5.
95
/ /
mPrincipal = decimal.Round(mPrincipal,
2);
/ /
Console.WriteLine(nYear + "-" + mPrincipal)
//
n Y e a r = n Y e a r + 1;
/ /
C o n s o l e . W r i t e L i n e ( " < E n t e r > "
"
.
Console.Read();
") ;
: 1 2 3 4
: 1 0
CalculatelnterestTable:
:12.5
= 1234
= 1 2 . 5 %
= lOyears
1-1388.25
2-1561.78
3-1757.00
4-1976.62
5-2223.70
6-2501.66
7 - 2 8 1 4 . 37
8-3166.17
9-3561.94
10-4007.18
<Enter>
...
i
, . ,
1234 . 12.5% 9 3561.94 .
.
# , , ,
2223.70 2223.7. 0
,
9, " # " (# 2.0 .)
C a l c u l a t e l n t e r e s t T a b l e ]
.
, ,
n D u r a t i o n .
96
II. *
w h i l e , n Y e a r ,
1. " ", ..
. ,
nYear, , n D u r a tion, " "
" ".
" ".
d e c i m a l . Round ()
.
nYear = n Y e a r + 1; n Y e a r 1.
, , nYear,
. 10 ,
n Y e a r 11, .. 10,
w h i l e , .
,
.
- n Y e a r C a l c u l a t e l n t e r e s t T a b l e
w h i l e , .
, n Y e a r ,
. , ,
. w h i l e ,
.
w h i l e .
:
i n t nYear =
while
1;
{
//
...
...
}
( n Y e a r = n Y e a r + 1 ; . )
n Y e a r 1,
. (infinite loop).
(
).
, .
.
, ,
...
5.
97
,
, .
do...while
w h i l e d o . . . w h i l e . |
, :
i n t nYear = 1;
do
{
/ / . . . . . .
n Y e a r = n Y e a r + 1;
} w h i l e (nYear < n D u r a t i o n ) ;
w h i l e , d o . . . w h i l e
, n D u r a t i o n .
, w h i l e .
break continue
b r e a k c o n t i n u e
b r e a k
. c o n t i n u e
, .
c o n t i n u e ,
. , ,
.
,
, (
, , .
:
if
(mPrincipal
>
(maxPower
mOriginalPrincipal))
{
break;
}
b r e a k , if
, .. maxPower . Ons
b r e a k w h i l e (nYear<=nDurationj
,
.
-
C a l c u l a t e l n t e r e s t T a b l e W i t h B r e a k (
).
98
II.
cyjvijviy
loo
:25
: 1 0 0
= 10 0
= 25%
= 100 y e a r s
10
1-125.00
2-156.25
3-195.31
4-244.14
5-305.18
6-381.48
7-476.85
8-596 . 06
9-745 . 08
10-931.35
11-1164 . 19
< E n t e r > . . .
,
1000 , 100 !
C a l c u l a t e l n t e r e s t T a b l e ,
,
. ,
, .
w h i l e b r e a k
, C a l culatelnterestTableMoreForgiving:
//
CalculateInterestTableMoreForgiving
//
// .
//
//
using S y s t e m ;
namespace C a l c u l a t e I n t e r e s t T a b l e M o r e F o r g i v i n g
[{
using S y s t e m ;
public
class
Program
{
p u b l i c s t a t i c v o i d Main ( s t r i n g [ ]
args)
{
//
5.
99
//
i n t nMaximumlnterest = 50;
/ /
/ / ; ,
//
decimal mPrincipal;
while(true)
{
C o n s o l e . W r i t e ( " : " ) ;
string sPrincipal = Console.ReadLine();
mPrincipal = Convert.ToDecimal(sPrincipal);
/ / ,
if ( m P r i n c i p a l >= 0)
{
break;
}
//
C o n s o l e . W r i t e L i n e ( " " ) ;
Console.WriteLine(" ");
Console.WriteLine ( ) ;
}
//
decimal mlnterest;
while(true)
{
C o n s o l e . W r i t e ( " : " ) ;
string slnterest = Console.ReadLine();
mlnterest = Convert.ToDecimal(slnterest);
//
//
if
...
{
break;
}
/ / . . .
C o n s o l e . W r i t e L i n e ( "
" " +
" " +
nMaximumlnterest);
Console.WriteLine(" ");
Console.WriteLine();
" +
}
//
//
//
//
100
,
,
II. #
Console.Write(" : " ) ;
string sDuration = Console.ReadLine();
int nDuration = Convert.ToInt32(sDuration);
/ /
Console.WriteLine();
/ /
Console.WriteLine(" = " + mPrincipal);
Console.WriteLine(" = " +
m l n t e r e s t + "%") ;
Console.WriteLine("
= " + nDuration
" years");
Console.WriteLine() ;
/ /
i n t nYear = 1;
w h i l e ( n Y e a r <= n D u r a t i o n )
{
/ /
decimal mlnterestPaid;
mlnterestPaid = mPrincipal * (mlnterest / 100);
/ /
mPrincipal = mPrincipal + mlnterestPaid;
//
mPrincipal = decimal.Round(mPrincipal,
2);
/ /
Console.WriteLine(nYear + "-" + mPrincipal);
/ /
n Y e a r = n Y e a r + 1;
I }
\}
}
/ /
C o n s o l e . W r i t e L i n e ( " <Enter> " +
"
...");
Console.Read();
,
. i f ,
, w h i l e :
decimal m P r i n c i p a l ;
while(true)
{
C o n s o l e . W r i t e ( " : " ) ;
string sPrincipal = Console.ReadLine();
mPrincipal = C o n v e r t . T o D e c i m a l ( s P r i n c i p a l ) ;
5.
101
/ / ,
if ( m P r i n c i p a l >= 0)
{
break;
}
//
C o n s o l e . W r i t e L i n e ( " " ) ;
Console.WriteLine(" ");
Console.WriteLine();
}
. !
, .!
, -|
, .
, , , !
, . I
, |
, , , -1
.
mlnterest
<
||
mlnterest
> nMaximumlnterest
m l n t e r e s t >= 0 && m l n t e r e s t <= n M a x i m u m l n t e r e s t
, m l n t e r e s t > = 0 m l n t e r e s t < 0 ,
(| |) (&&). if
: " , -1
( , )".
, C a l c u l a t e l n t e r e s t T a - l
b l e M o r e F o r g i v i n g , -1
, ,
? :
w h i l e ( t r u e ) .
: m P r i n c i p a l -1
, - [
.
, t r u e
t r u e . , w h i l e ( t r u e ) ,
b r e a k .
w h i l e ( t r u e ) b r e a k ,
.
:
: - 1 0 0 0
102
II. #
: 1 0 0 0
:-10
50
: 1 0
: 5
= 10 0 0
= 10%
,
= 5 years
1-1100 . 0
2-1210.00
3-1331.00
4-1464.10
5-1610 . 51
< E n t e r > . . .
,
.
, ,
. ,
,
. !
, , .
:
int n D a y s = 1;
while(nDays < n D u r a t i o n )
{
...
n A v e r a g e w h i l e .
, :
i n t n A v e r a g e .
, ,
, . .
,
, .
, n A v e r a g e
.
( 5.
103
, nAver-l
a g e w h i l e .
, w h i l e
#. foil
:
f o r { 1;
2)
{
II
...
...
1
f o r !.
, , , !
f o r .
2,
.
f o r w h i l e :
1 ;
whi1()
{
I I . . . . . .
2;
, , f o r , !
:
/ / #
= 1;
/ /
f o r ( i n t n Y e a r = 1;
nYear < n D u r a t i o n ;
n Y e a r = n Y e a r + 1)
{
//
...
...
}
/ /
= 2;
, = 1 ; .
n Y e a r 1.
n Y e a r n D u r a t i o n . n Y e a r n D u r a t i o n ,
. }|
n Y e a r = n Y e a r + l ,
n Y e a r < n D u r a t i o n .
104
II. &
n Y e a r f o r ,
, .
# f o r , , w h i l e ?
, f o r
, w h i l e .
f o r ,
, : ,
. ,
, w h i l e
.
for .
, f o r
-, .
# .
f o r , , ,
, .
f o r (
4, "").
for
for(int n Y e a r = 1;
nYear < n D u r a t i o n ;
nYear++)
{
//
...
.. .
}
f o r
, .
f o r ,
, , t r u e .
, f or (; ;)
, w h i l e ( t r u e ) .
f o r (; ;)
, w h i l e ( t r u e ) ; , .
, , ,
,
. . .
5
,
false, ,
. . .
5.
105
:
for(...
...)
{
for(...
...)
{
//
...
...
. - , ,
.
, , (nested)
"", ..
:
, ,
.
b r e a k
. brea)
:
//
for(...
for
...)
{
/ / f o r
for(...
...)
{
//
if
{
. . .
( )
break;
//
...
}
}
# , . |
, .
,
, . r e t u r n
, .. ,
. r e t u r n
7, " ".
106
II. #
D i s p l a y X W i t h N e s t e d L o o p s ,
, ,
X.
II D i s p l a y X W i t h N e s t e d L o o p s
//
// X
using S y s t e m ;
namespace D i s p l a y X W i t h N e s t e d L o o p s
{
public
class
Program
{
public
s t a t i c void Main(string[]
args)
{
int nConsoleWidth = 4 0 ;
//
f o r ( i n t nRowNum = 0 ;
nRowNum < n C o n s o l e W i d t h ;
nRowNum + = 2)
{
//
f o r ( i n t nColumnNum = 0 ;
i
nColumnNum < n C o n s o l e W i d t h ; nColumnNum++)
{
/ /
1 1
char =
;
//
if (nColumnNum == nRowNum)
. . .
{
/ / . . .
= ' \\ ' ;
}
//
// . . .
i n t nMirrorColumn = nConsoleWidth i f (nColumnNum = = n M i r r o r C o l u m n )
nRowNum;
{
/ / . . .
= ' /1 ;
}
//
Console.Write();
}
Console.WriteLine();
}
//
5.
Si
107
s w i t c h .
n M a r i t a l S t a t u s .
c a s e .
, , d e f a u l t .
s w i t c h s t r i n g :
string s = "Davis";
switch(s)
{
case "Mallory":
/ /
break;
case "Wells":
/ /
break;
case
"Arturo":
/ /
break;
c a s e "Brown":
/ /
break;
default:
// ,
/ /
s w i t c h .
S
string.
s w i t c h ()
c a s e , s w i t c h .
c a s e ,
.
c a s e b r e a k ( -
, , r e t u r n ) . b r e a k
s w i t c h .
c a s e , :
switch(nMaritalStatus)
{
c a s e 0 .case 2:
// /
//
break;
case 1:
//
break;
110
II. #
case 3 :
// ( )
break;
case 4:
/ /
break;
default:
// , ,
/ / -
/ / , , -
/ /
break;
}
g o t o . :
;
c a s e s w i t c h ;
d e f a u l t , d e f a u l t s w i t c h .
c a s e
s w i t c h .
g o t o :
// . . .
if ( > )
I'
/ / g o t o
// e x i t L a b e l
goto e x i t L a b e l ;
//
exitLabel:
//
g o t o ,
.
, ,
. , "" .
g o t o " ".
# , .
. ,
, .
5.
111
, ,
. ,
-. ,
.
.
.
> #
>
>
>
i n t , d o u b l e b o o l ,
. ,
.
, ,
. , , ,
, ,
.. , s t r i n g ,
int s h o r t , d o u b l e .
S t u d e n t .
, # , ,
.
.
, . d o u b l e
,
, ,
d o u b l e . # .
,
, ,
. # .
,
. #
, .
, "
". , ,
, ,
..
, . ,
V e h i c l e ,
d T o p S p e e d , n W e i g h t b T r a i l e r .
#,
IV, "-
"; .
V e h i c l e :
public
class
Vehicle
{
public
public
public
public
s t r i n g sModel;
s t r i n g sManufacturer;
i n t nNumOfDoors;
i n t nNumOfWheels;
//
//
//
//
}
p u b l i c
c l a s s ,
( V e h i c l e ) .
#, . #
,
.
,
( ). , . V e h i c l e s t r i n g
s M o d e l , .
s t r i n g
sManuf a c t u r e r , -
.
, . , #. .
p u b l i c -!
. , p u b l i c
. ,
11, "".
.
, ,
, .
116
III. -
#, .
"". ,
, .
i n t i n t . V e h i c l e .
, , V e h i c l e :
Vehicle myCar;
myCar = new V e h i c l e () ;
myCar V e h i c l e ,
n S o m e t h i n g i n t (, ,
# ). new V e h i c l e ()
V e h i c l e myCar. n e w
("")
, myCar.
# m y C a r V e h i c l e . ,
m y C a r V e h i c l e .
(instance) "" " ".
, V e h i c l e ,
new.
myCar num i n t :
int num;
num = 1;
num i n t ,
i n t ,
num.
num my . 1 , , # ,
1. , V e h i c l e .
new V e h i c l e
, , #, .
V e h i c l e .
1 nNumberOf D o o r s ,
myCar:
myCar. nNumberOf D o o r s = 1;
# , . myCar
V e h i c l e . V e h i c l e .nNumberOf D o o r s
i n t ( V e h i c l e ) . 1
i n t ,
.
6. -
117
, strinJ
myCar:
m y C a r . s M a n u f a c t u r e r = "BMW";
myCar.sModel = " I s e t t a " ;
( I s e t t a , 1950-
.)
-
V e h i c l e D a t a O n l y :
V e h i c l e ;
myCar;
myCar;
myCar .
V e h i c l e D a t a O n l y :
//
VehicleDataOnly
/ / V e h i c l e , ,
// ,
using System;
namespace
VehicleDataOnly
{
public
class
Vehicle
{
public
public
public
public
s t r i n g sModel;
s t r i n g sManufacturer;
i n t nNumOfDoors,i n t nNumOf Wheels,-
/ /
//
//
//
}
public
class
Program
{
/ /
s t a t i c void
Main(string[] args)
{
/ /
C o n s o l e . W r i t e L i n e ( "
");
// V e h i c l e
Vehicle
myCar = new V e h i c l e ( ) , / /
Console.Write(" = " ) ;
string s = Console.ReadLine();
myCar.sModel = s;
118
III. -
/ /
Console.Write(" = " ) ;
myCar.sManufacturer
= Console.ReadLine;
/ / i n t
Console.Write(" = " ) ;
s = Console.ReadLine();
myCar.nNumOfDoors
= Convert.ToInt32(s),Console.Write(" = " ) ;
s = Console.ReadLine();
myCar.nNumOfWheels = C o n v e r t . T o I n t 3 2 ( s ) ;
//
C o n s o l e . W r i t e L i n e ( " \ : " ) ;
Console.WriteLine(myCar.sManufacturer
+ " " +
myCar.sModel);
C o n s o l e . W r i t e L i n e ( " c " + myCar.nNumOfDoors +
" , "
+
" " + m y C a r . n N u m O f W h e e l s
+
" ") ;
/ /
C o n s o l e . W r i t e L i n e ( " < E n t e r > " +
" . . . ") ;
Console.Read();
V e h i c l e .
, P r o g r a m
. -
.
myCar V e h i c l e ,
, .
.
.
:
= Metropolitan
= N a s h
= 2
= 4
:
Nash M e t r o p o l i t a n
2 , 4
< E n t e r >
...
, -
119
R e a d ( ) , R e a d L i n e ( ) , !
. , nil
, . ,
' \ ' W r i t e L i n e ( ) .
|
. ,
, :
V e h i c l e c a r l = new V e h i c l e ;
carl.sManufacturer = "Studebaker";
carl.sModel = "Avanti";
/ /
V e h i c l e c a r 2 = new V e h i c l e ( ) ;
c a r 2 . s M a n u f a c t u r e r = "Hudson";
car2.sModel = "Hornet";
carl
c a r 2 ]
c a r l .
,
,
, .
"" ,
. :
/ /
V e h i c l e yourCar;
//
y o u r C a r = new V e h i c l e ( ) ;
//
yourCar.sManufacturer = "Rambler";
// ,
V e h i c l e yourSpousalCar = yourCar;
y o u r C a r , ,
(null object).
, .
#
. -
.
V e h i c l e -1
y o u r C a r . y o u r S p o u s a l C a r l
120
III. -
y o u r C a r . . 6.1, , y o u r S p o u s a l C a r
, y o u r C a r .
. 6.1.
:
//
Vehicle y o u r C a r = new V e h i c l e () ;
yourCar. s M o d e l = " K a i s e r " ;
//
Vehicle y o u r S p o u s a l C a r = y o u r C a r ;
// ,
yourSpousalCar. sModel = "Henry J " ;
C o n s o l e . W r i t e L i n e ( " - " + y o u r C a r . s M o d e l ) ;
Henry J, K a i s e r . , y o u r S p o u s a l C a r
yourCar y o u r S p o u s a l C a r , y o u r C a r ,
.
, y o u r S p o u s a l C a r ,
"" (, ),
:
//
Vehicle y o u r C a r = new V e h i c l e () ;
yourCar. s M o d e l = " K a i s e r " ;
//
Vehicle y o u r S p o u s a l C a r = y o u r C a r ;
// . . .
yourCar = n u l l ;
// yourCar "
// "
// . . . y o u r S p o u s a l C a r
C o n s o l e . W r i t e L i n e ( " - " + y o u r S p o u s a l C a r . s M o d e l ) ;
" - K a i s e r " , y o u r C a r
.
y o u r C a r .
, ""
yourCar, y o u r S p o u s a l C a r .
,
(garbage collector) # ,
6. -
121
, . -1
12, "".
,
, , . !
, , !
.
V e h i c l e :
public class Vehicle
{
//
p u b l i c s t r i n g sModel;
public
s t r i n g s M a n u f a c t u r e r ; //
//
p u b l i c i n t nNumOfDoors;
public
i n t nNumOfWheels;
//
p u b l i c i n t nPower;
//
public double displacement;
//
,!
, , !
.
!
:
public
class
Motor
{
public
public
i n t nPower;
double displacement;
/ /
/ /
}
V e h i c l e :
public
{
class
public
public
public
public
public
Vehicle
s t r i n g sModel;
s t r i n g sManufacturer;
i n t nNumOfDoors;
i n t nNumOfWheels;
Motor motor;
//
//
//
//
, s o n s C a r :
/ /
M o t o r l a r g e r M o t o r = new M o t o r ( ) ;
l a r g e r M o t o r . n P o w e r = 23 0;
largerMotor.displacement = 4.0;
//
V e h i c l e s o n s C a r = new V e h i c l e ( ) ;
sonsCar.sModel = "Cherokee S p o r t " ;
sonsCar.sManfacturer = "Jeep";
sonsCar.nNumOfDoors = 2;
sonsCar.nNumOfWheels = 4;
//
sonsCar.motor = largerMotor;
122
III. -
V e h i c l e ,
:
Motor m = s o n s C a r . m o t o r ;
Console.WriteLine("
"
+ m.displacement);
:
Console.Writeline (" " +
sonsCar.motor.displacement);
d i s p l a c e m e n t M o t o r .
V e h i c l e A n d M o t o r ,
-.
- .
:
public c l a s s
Car
(
public
string
sLicensePlate;
//
I
,
.
:
Car c o u s i n s C a r = new Car () ;
cousinsCar. s L i c e n s e P l a t e = " X Y Z 1 2 3 " ;
Car yourCar = new Car () ;
yourCar. s L i c e n s e P l a t e = " A B C 7 8 9 " ;
, . ,
,
. s t a t i c ,
:
public class Car
{
public s t a t i c
int nNumberOfCars; / /
public s t r i n g s L i c e n s e P l a t e ;
/ /
}
,
, :
//
Car.nNumberOf Cars++ ;
( 6. -
123
n e w C a r . s L i c e n s e P l a t e
n e w C a r , ()
C a r . n N u m b e r O f C a r s .
n N u m b e r O f C a r s .
-
,
.
, ,
:
class
Program
{
//
public
public
i n t
const
s t a t i c
nDaysInYear
void
366;
Main(string []
args)
{
//
int[]
nMaxTemperatures
for(int
index
0;
index
new
<
int[nDaysInYear];
nDaysInYear;
index++)
{
//
n D a y s I n Y e a r
366.
" " ( 3 6 6 ) n D a y s I n Y e a r , *
.
# . ,
c o n s t , r e a d o n l y :
public
readonly
i n t
nDaysInYear
366;
c o n s t ,
, . X
,
,
readonly.
.
, ( n D a y s I n Y e a r )
D A Y S _ I N _ Y E A R . .
,
. .
124
III. -
, ,
. A r r a y ,
( i n t ,
double, V e h i c l e ..).
.
10 d o u b l e
( , 3,
" -"):
double
double
double
double
double
double
double
double
double
double
dO
dl
d2
d3
d4
d5
d6
d7
d8
d9
=
=
=
=
=
=
=
=
=
=
5;
2;
7;
3.5;
6.5;
8;
1;
9;
1;
3;
10:
double dSum = dO + dl + d2 + d3 + d4 +
d5 + d6 + d7 + d8 + d 9 ;
double dAverage = dSum / 1 0 ;
, 10.
, 10000 ...
, . #
,
. ,
:
double[]
dArray =
{5,
2,
7,
3.5,
6.5,
8,
1,
9,
1,
3} ;
A r r a y ,
. []
:
dArray[0]
dArray[1]
dO
dl
do, dl .
0, 1, 2, ... .
( 6.
-
125
# 0, 1. ,
1 . !
d A r r a y , . f o r , , F i x e d A r r a y A v e r a g e .
11
'FixedArrayAverage
//
//
namespace
FixedArrayAverage
using
System;
public
class
Program
{
public
static
double[]
void
Main(string[]
dArray =
{5, 2, 7, 3 . 5 ,
6.5,
8,
args)
1,
9,
1,
3};
//
// dSum
d o u b l e d S u m = 0 ,f o r ( i n t i = 0; i < 10; i + + \
dSum =
dSum +
dArray[i];
//
d o u b l e d A v e r a g e = dSum / 1 0 ;
Console.WriteLine(dAverage);
//
Console.WriteLine(" <Enter> " +
" .. . " ) ;
Console.Read();
d S u m 0.
d A r r a y
d S u m . d S u m . !
, .
F i x e d A r r a y A v e r a g e 10
. , , 10
. 10 , ? .
126
III. -
9 ? #
. 9 10 , #
, ? ,
, .
11 ( ) ?
#
, -
. , f o r , 10 11:
for ( i n t i = 0 ; i < 1 1 ; i + + )
:
IndexOutOfRangeException was u n h a n d l e d
Index was o u t s i d e t h e b o u n d s o f t h e a r r a y .
C# I n d e x O u t O f
R a n g e E x c e p t i o n ,
, . (
, , ,
#, .)
, F i x e d A r r a y A v e r a g e ,
:
10 ;
,
.
,
, ,
F i x e d A r r a y A v e r a g e ,
.
:
doublet] d A r r a y = n e w d o u b l e [ N ] ;
N .
V a r i a b l e A r r a y A v e r a g e
.
,
, .
;// V a r i a b l e A r r a y A v e r a g e
// ,
// .
[//
[// , ,
[// .
6, -
127
namespace
VariableArrayAverage
{
using
System;
public
class
Program
{
public
s t a t i c
void
Main(string[]
args)
{
// double,
//
Console.Write(" :
");
s t r i n g sNumElements = C o n s o l e . R e a d L i n e ;
i n t numElements = Convert.ToInt32(sNumElements);
Console.WriteLine();
//
double[]
//
dArray
for
(int
new
0;
<
double[numElements];
numElements;
i++)
{
//
C o n s o l e . W r i t e ( " d o u b l e " +
( i + 1) + " : ") ;
s t r i n g sVal = C o n s o l e . R e a d L i n e ( ) ;
double dValue = Convert.ToDouble(sVal);
//
dArray[i]
dValue;
//
'numElements'
/ / dSum
d o u b l e dSum = 0;
for
(int
0;
<
numElements,-
i++)
{
dSum
dSum
dArray[i];
}
//
d o u b l e d A v e r a g e = dSum
numElements;
/ /
Console.WriteLine();
Console.Write(dAverage
+ "
("
+ d A r r a y [ 0 ] ) ,for
(int
1;
<
numElements,-
Console.Write("
i++)
{
"
dArray[i]);
}
Console.WriteLine(")
128
"
numElements);
III. -
//
Console.WriteLine(" <Enter> " +
" ...");
Console.Read();
1 5:
double
double
double
double
double
1:
2:
3:
4:
5:
1
2
3
4
5
3
( 1 + 2 + 3 + 4 + 5 )
/5
< E n t e r > . . .
V a r i a b l e A r r a y A v e r a g e
,
. n u m E l e m e n t s i n t .
5.
d A r r a y
. ,
d o u b l e . n u m E l e m e n t s ,
.
,
, F i x e d A r r a y A v e r a g e
.
( ).
, . ,
, ,
.
V a r i a b l e A r r a y A v e r a g e , ,
. ,
, -
. , #
;
. 15, "
", .
Length
V a r i a b l e A r r a y A v e r a g e
f o r :
6.
129
//
doublet]
//
dArray
for
(int
new
0;
<
double[numElements];
numElements;
i++)
{
//
C o n s o l e . W r i t e ( " d o u b l e "
+
(i + 1) + " : ,),,;
s t r i n g sVal = C o n s o l e . R e a d L i n e ;
double dValue
=
Convert.ToDouble(sVal);
//
dArray[i]
dValue;
}
d A r r a y n u m E l e m e n t s . ,
, n u m E l e m e n t s .
, - (
, . , ,
L e n g t h , , d A r r a y . Lengtkl
, n u m E l e m e n t s .
, f o r :
//
for
(int
0;
<
dArray.Length;
i++)
, |
:
double[]
doublet]
dFixedLengthArray =
{5,2,7,3.5,6.5,8,1,9,1,3};
d V a r i a b l e L e n g t h A r r a y = new d o u b l e [ 1 0 ] ;
C# , |
:
doublet]
dFixedLengthArray =
new d o u b l e [ 1 0 ]
{5,
2,
7,
3.5,
6.5,
8,
1,
9,
1,
3 } ;
,
, .
( ) . , , -]
, , :
public
class
Student
{
public
public
string
double
sName;
dGPA;
//
}
130
III. -
.
n u m S t u d e n t :
Student []
students
new
Student[num];
n e w S t u d e n t [ n u m ]
S t u d e n t ,
S t u d e n t .
, s t u d e n t s [ i ] ,
# , n u l l .
, , s t u d e n t s [ i ]
n e w S t u d e n t .
:
for
(int
0;
<
students. Length;
new
i + +)
{
students[i]
Student();
}
:
students [ i ]
new
students [ i ] . sName
students [ i ] . dGPA
Student ();
=
"My N a m e " ;
dMyGPA;
A v e r a g e S t u d e n t G P A ,
.
// A v e r a g e S t u d e n t G P A
//
using S y s t e m ;
namespace
AverageStudentGPA
f
public
class
Student
{
public
public
string
sName;
d o u b l e dGPA;
//
}
public
{
class
public
Program
s t a t i c
void
Main(string[]
args)
{
//
Console.WriteLine("
");
string s = Console.ReadLine();
int nNumberOfStudents = Convert.ToInt32(s);
// S t u d e n t
Student[]
students
//
f o r ( i n t i = 0; i <
new
Student[nNumberOfStudents];
students.Length;
6. -
i++)
131
//
//
//
Co
.
,
#
n s o l e . W r i t e ( "
"
+ ( i + 1) + " : ") ;
s t r i n g sName = C o n s o l e . R e a d L i n e ;
Console.Write("
:
s t r i n g sAvg = C o n s o l e . R e a d L i n e ;
d o u b l e dGPA = C o n v e r t . T o D o u b l e ( s A v g ) ;
//
Student thisStuden
thisStudent.sName
thisStudent.dGPA
//
students[i]
t = new S t u d e n t ( ) ;
= sName;
= dGPA;
" ) ;
thisStudent;
}
//
d o u b l e dSum = 0 . 0 ;
for
(int
0;
<
students.Length;
i++)
{
dSum
+=
students[i].dGPA;
}
double
dAvg
dSum/students.Length;
//
Console.WriteLine();
C o n s o l e . W r i t e L i n e ( "
"
+
students.Length
+ " - " + dAvg);
//
,
S t u d e n t .
f o r ,
.
S t u d e n t .
, ]
, ]
.
132
III. -
<Enter>
1:
Randy
:
2:
Jeff
:
3:
Carrie
:
3.0
3.5
4.0
3.5
...
, ,
, s t u d e n t . -
, , , b a d S t u d e n t , g o o d S t u d e n t ..
( ) ,
s t u d e n t s , p h o n e N u m b e r s
p h o n e N u m b e r s I n M y P a l m P i l o t .
, #
, .
, :
public
class
Student
{
public
public
string
double
sName;
dGPA;
//
)
public
class
Program
(
public
s t a t i c
void
Main(string[]
args)
{
// ...
...
//
d o u b l e dSum = 0 . 0 ;
for
(int
0;
<
students.Length;
i++)
{
dSum
+=
students[i].dGPA;
d o u b l e dAvg = dSum / s t u d e n t s . L e n g t h ;
// . . . . . .
f o r .
s t u d e n t s . L e n g t h .
6.
133
# f o r e a c h ,
, .
:
//
d o u b l e dSum = 0 . 0 ;
foreach
(Student
stud
in
students)
{
dSum +=
stud.dGPA;
d o u b l e dAvg = dSum / s t u d e n t s . L e n g t h ;
S t u d e n t
s t u d . f o r e a c h
s t u d .
f o r e a c h , .
, f o r e a c h .
.
, ++ J a v a f o r e a c h
, # (, .NET)
.
f o r e a c h ,
. , (
, , 15, " ").
, f o r e a c h (..
), .
-'
. , , ,
, . ,
S t u d e n t ,
:
S t u d e n t temp = s t u d e n t s [i] ;
s t u d e n t s [i]
= students [j];
students[j]
= temp;
//
i-
i-
s t u d e n t s , , i-
j- . j-
, i-
. . 6.2.
, ,
.
15, " ".
734
III. -
.
.
,
.
// S o r t S t u d e n t s
//
//
using S y s t e m ;
namespace
SortStudents
(
class
Program
{
public
s t a t i c
void
Main(string[]
args)
{
//
Student[]
st
students[0]
students[1]
students[2]
students[3]
students[4]
u d e n t s = new S t u d e n t [ 5 ] ;
= Student.NewStudent("Homer",
0);
= Student.NewStudent("Lisa",
4.0);
= Student.NewStudent("Bart",
2.0);
= Student.NewStudent("Marge",
3.0);
= Student.NewStudent("Maggie",
3.5);
ft
135
/ /
:
Console.WriteLine("
:");
OutputStudentArray(students);
//
//
(
//
)
Console.WriteLine("\
\");
Student.Sort(students);
/ /
Console.WriteLine("
OutputStudentArray(students);
:");
//
}
// O u t p u t S t u d e n t A r r a y -
//
public
s t a t i c
void OutputStudentArray(Student []
students)
{
foreach(Student
in
students)
{
Console.WriteLine(s.GetString());
}
} }
//
Student
//
class
Student
{
public
string
sName;
public
double
dGrade
//
//
NewStudent
public
s t a t i c
0.0;
Student
NewStudent(string
double
sName,
dGrade)
{
S t u d e n t s t u d e n t = new S t u d e n t ( ) ;
s t u d e n t . s N a m e = sName;
student.dGrade = dGrade;
return
student;
}
//
//
GetString
public
string
Student
GetString()
136
III. -
strin
s +=
s +=
s +=
retur
g s = "" ;
dGrade;
" - " ;
sName;
n
s;
}
//
//
//
Sort -
public
s t a t i c
void
Sort(Student[]
students)
{
bool
//
bRepeatLoop;
do
{
// t r u e
//
bRepeatLoop =
false;
//
f o r ( i n t i n d e x = 0;
index
index++)
<
(students.Length
1);
{
//
//
if
...
(students[index].dGrade
<
students[index +
1].dGrade)
{
//
... ...
Student to = s t u d e n t s [ i n d e x ] ;
S t u d e n t from = s t u d e n t s [ i n d e x +
students[index]
= from;
s t u d e n t s [ i n d e x + 1]
= to;
1];
//
. . . t r u e ,
//
//
( ,
//
)
bRepeatLoop = t r u e ;
while
(bRepeatLoop);
}
}
, :
:
- Homer
4 - Lisa
2 - Bart
6.
137
3 3.5
Marge
- Maggie
4 - Lisa
3.5
- Maggie
3 - Marge
2 - Bart
0 - Homer
< E n t e r >
...
,
. N e w S t u d e n t ()
S t u d e n t , *
. OutputStudentArray().
S o r t ( ) .
, , .
, S o r t S t u d e n t s ]
S o r t ( ) . ,
,
. ,
-,
, , . . 6.3-6.6 ,
.
HOMER
LISA
LISA
BART
BART
MARGE
MARGE
MAGGIE
3,5
MAGGIE
3,5
HOMER
. 6.3.
-* Homer
. 6.4.
. 6.5.
, , ""
"" .
138
III. -
. 6.6.
.
,
,
. , ,
, .
6.
139
>
>
>
>
, . , ,
, ,
.
# , .
.
:
class
Example
public i n t n l n t ;
public s t a t i c i n t n S t a t i c I n t
public v o i d MemberFunction()
{
Console.WriteLine("
/ / H e
//
/ / H e
-" ) ;
^
public
static
void
ClassFunction()
//
{
Console.WriteLine("
");
}
n l n t -, 6,
" ". M e m b e r F u n c t i o n ()
. -, #,
. ,
, , , .
:
. ,
8, " ", , - #
.
n l n t
( ) n S t a t i c I n t :
Example
example
example.nlnt
Example.nStaticInt
new
1;
= 2;
Example();
//
//
//
//
//
(
) M e m b e r F u n c t i o n ( ) C l a s s F u n c t i o n ( ) :
Example e x a m p l e = new E x a m p l e ( ) ; //
example.MemberFunction();
// -
//
Example.ClassFunction();
//
//
//
example.ClassFunction();
//
//
E x a m p l e . M e m b e r F u n c t i o n ( ) ; // //
() -
(), ,
() - (), !
6, " ".
e x a m p l e . M e m b e r F u n c t i o n ( ) ,
. E x a m p l e . C l a s s F u n c t i o n ()
.
:
,
.
M a i n () .
.
, !
,
, , ,
, ,
.
, , ,
.
142
III. -
,
,
C a l c u l a t e l n t e r e s t T a b l e 5, "
", .
, Visual Studio 2005
Refactor,
.
, .
. ,
. ( , ,
, ,
, , /
).
"" C a l c u l a t e l n t e r e s t T a b l e :
public
static
void
Main ( s t r i n g []
args)
{
// .
// ,
// .
// .
// ,
// .
/ / , .
// .
/ / .
while(nYear <= n D u r a t i o n )
{
//
II
.
// .
*.
}
}
.
, , :
I / , ,
;
| / , ,
;
I J , .
7,
143
. ,
, ,
,
. .
Calcl
latelnterestTable
CalculatelnterestTa
bleWithFunctions.
//
CalculateInterestTableWithFunctions
//
//
//
//
,
,
.
using
System;
namespace
{
public
CalculateInterestTableWithFunctions
class
Program
{
public
s t a t i c
void
Main(string[]
args)
// 1 - ,
//
d
d
d
I
ecima
ecima
ecima
nputl
l
l
l
n
m P r i n c i p a l = 0;
m l n t e r e s t = 0,m D u r a t i o n = 0;
terestData(ref
mPrincipal,
ref
mlnterest,
ref
mDuration);
// 2 -
//
Console.WriteLine();
// skip a.line
Console.WriteLine("
mPrincipal);
"
C o n s o l e . W r i t e L i n e ( " = " +
mlnterest
Console.WriteLine("
"%") ;
=
"
m D u r a t i o n + . " ");
Console.WriteLine();
// 3 - , ,
//
OutputlnterestTable(mPrincipal,
mlnterest,
mDuration);
//
Console.WriteLine("
744
<Enter>
"
III. -
"
...");
Console.Read();
}
//
//
InputlnterestData
// 1,
//
public
s t a t i c
void
InputlnterestData(ref
ref
ref
mPrincipal,
mlnterest,
mDuration)
// la
mPrincipal
=
InputPositiveDecimal("");
//
16
mlnterest
//
decimal
decimal
decimal
InputPositiveDecimal("
mDuration
");
InputPositiveDecimal("");
//
InputPositiveDecimal
//
//
//
decimal
public
s t a t i c
decimal
InputPositiveDecimal(string
sPrompt)
{
//
//
while(true)
{
//
Console.Write(" " +
sPrompt
" : " ) ;
// d e c i m a l
string slnput = Console.ReadLine();
decimal mValue = C o n v e r t . T o D e c i m a l ( s l n p u t ) ;
//
if
(mValue
//
>=
return
0)
mValue;
}
//
//
Console.WriteLine(sPrompt
+
7.
145
" " ) ;
Console.WriteLine("
" ) ;
Console.WriteLine();
//
//
//
O u t p u t l n t e r e s t T a b l e - ,
// 3
public
s t a t i c
void
OutputlnterestTable(decimal
decimal
decimal
mPrincipal,
mlnterest,
mDuration)
{
for
(int
nYear
1;
nYear
<=
mDuration;
nYear++)
{
//
decimal
mlnterestPaid;
mlnterestPaid = mPrincipal *
(mlnterest
100);
//
//
//
"-"
2);
mPrincipal);
}
M a i n () ,
. , , ,
1, 1 1.
.
,
.
, .
,
( m P r i n c i p a l , m l n t e r e s t m D u r a t i o n ) , I n p u t l n
t e r e s t D a t a ( ) . ,
.
O u t p u t l n t e r e s t T a b l e ( ) .
, O u t p u t l n t e r e s t T a b l e ( ) .
,
146
III. -
C a l c u l a t e l n t e r e s t T a b l e 5, "
". ,
.
,
.
, O u t p u t l n t e r e s t T a b l e ( ) .
O u t p u t l n t e r e s t T a b l e ! ) ,
Refactoring V i s u a l Studio 2005.
.
1. CalculatelnterestTableMoreForgiving 5, " ",
nYear while:
0;
<=
int nYear =
while(nYear
{
//...
nDuration)
//
// w h i l e
}
2. R e f a c t o r ^ E x t r a c t Method.
3. Extract Method O u t p u t l n t e r e s t T a b l e ,
p r i v a t e s t a t i c O u t
p u t l n t e r e s t T a b l e ().
/ , M a i n () ,
:
mPrincipal
OutputlnterestTable(mPrincipal,
mlnterest, nDuration);
" " I n p u t l n
t e r e s t D a t a ( ) . ,
.
.
I n p u t l n t e r e s t D a t a ()
d e c i m a l . , ,
I n p u t P o s i
t i v e D e c i m a l ( ) , ,
, . ,
while I n p u t P o s i
t i v e D e c i m a l ( ) . , .
[ 7.
147
, ,
,
. Web-ca
www.refactoring.com.
I n p u t P o s i t i v e D e c i m a l () ]
. ,
. , !
.
, (
" "), :
:100
:-10
:10
1-110 . 0
2 - 1 2 1 . 00
3-133 .10
4-146 .41
5 - 1 6 1 . 05
6-177.16
7 - 1 9 4 . 88
8-214 .37
9-235.81
10-259.39
< E n t e r >
=
=
=
:10
10 0
10%
10
...
, , , |
, , -
.
?
1950- , !
. , ,
.
D i s p l a y R a t i o ( ) ,
.
, , ;
. , ,
W r i t e L i n e ( ) , MOiyr .
:
, ,
, . D i s p l a y R a t i o () -
148
III. -
, .
, ,
.
:
.
.
. : .
c a l c u l a t e S i n () . ,
, ,
.
c a l c u l a t e S i n ( ) , , .
,
. ,
.
, , , ,
. , R e d i s p l a y D o c u m e n t ()
R e p a r a g r a p h () . ,
, C a l c u l a t e W o r d W r a p ()
. C a l c u l a t e W o r d W r a p () L o o k UpWordBreak ( ) , , ,
. ,
(,
).
,
, ,
.
, , , ,
. ,
:
public
static
v o i d O u t p u t ()
Console.WriteLine("
");
. ,
, ? ,
? ? ,
.
7.
149
, , (
).
. ,
.
E x a m p l e :
public
class
Example
{
public
static
void
Output(string
funcString)
{
C o n s o l e . W r i t e L i n e (" O u t p u t ()
+ funcString);
11
}
}
:
Output("Hello") ;
Output
Hello
O u t p u t () " H e l l o " .
f u n c S t r i n g . O u t p u t () nep
f u n c S t r i n g ,
s t r i n g .
:
string myString =
Output(myString);
"Hello";
m y S t r i n g " H e l l o " .
O u t p u t ( m y S t r i n g ) , .
m y S t r i n g , .. " H e l l o " , . . 7.1.
, .
. 7.1. O u t p u t (myString)
myString
funcString
, , . , . , , .
750
III. -
. A v e r a g e A n d D i s p l a y ( ) :
// AverageAndDisplay
using System;
namespace
Example
{
public
class
Program
{
public
static
void
Main(string[]
args)
{
// -
A v e r a g e A n d D i s p l a y ( " 1 " , 3 . 5 , " 2 " , 4 . 0 ) ;
//
C o n s o l e . W r i t e L i n e ( " < E n t e r > " +
" . . . " ) ;
Console.Read();
}
// A v e r a g e A n d D i s p l a y -
//
public s t a t i c
void AverageAndDisplay(string si,
double dl,
s t r i n g s2,
d o u b l e d2)
{
double dAverage = (dl + d2) / 2 ;
Console.WriteLine(" "
+ si
+ ", " + dl
+ ", "
+ s2
+ ", " + d2
+ ", "
+ dAverage);
:
1 , 3 . 5 , 2 , 4 ,
< E n t e r > . . .
3 . 7 5
A v e r a g e A n d D i s p l a y ()
, .
, M a i n ( ) .
M a i n ( ) , , A v e r
a g e A n d D i s p l a y ( ) , d o u b l e .
A v e r a g e A n d D i s p l a y ()
double, dl d 2 , (
si s 2 ) , d A v e r a g e .
.
.
7.
151
, .
.
// A v e r a g e W i t h C o m p i l e r E r r o r
using
System;
namespace
Example
{
public
class
Program
{
public
static
void
Main(string[]
args)
{
// -
AverageAndDisplay(" 1",
"
2",
3.5,
4.0);
//
Console.WriteLine(" <Enter> " +
" . . . ") ;
Console.Read();
}
// A v e r a g e A n d D i s p l a y -
//
public
static
void AverageAndDisplay(string si,
double dl,
s t r i n g s2,
d o u b l e d2)
d o u b l e d A v e r a g e = ( d l + d 2) / 2;
Console.WriteLine(" "
+
+ ", " +
+ ", "
+
+ ", " +
+ ", "
+
si
dl
s2
d2
dAverage);
C#
. " 1 " s t r i n g
;
d o u b l e ,
s t r i n g .
, !
. , ,
.
,
A v e r a g e A n d D i s p l a y ( ) .
152
III. -
(overloading) .
, .
// A v e r a g e A n d D i s p l a y O v e r l o a d e d -
//
//
using
System;
namespace
AverageAndDisplayOverloaded
{
public
class
Program
{
public
static
void
Main(string[]
args)
// -
A v e r a g e A n d D i s p l a y ( " ", 3 . 5 ,
" ",- 4 . 0 ) ;
Console.WriteLine();
// -
AverageAndDisplay(3.5,
4.0);
//
Console.WriteLine("
<Enter>
"
" ...");
Console.Read();
// A v e r a g e A n d D i s p l a y -
//
//
public
void
static
AverageAndDisplay(string
string
{
double dAverage = (dl + d2) / 2 ;
Console.WriteLine("
"
si,
s2,
double
double
dl,
d2)
si
+ ", " + d l ) ;
Console.WriteLine("
"
s2
+ ", " + d2
+ ", "
}
public static void AverageAndDisplay(double dl,
+ dAverage);
double
d2)
{
double
dAverage
7.
(dl
d2)
2;
153
Console.WriteLine("
+ " "
+ "
"
"
+ dl
+ d2
+ dAverage);
A v e r a g e A n d D i s p l a y ( ) .
, . # col
, |
, .
, :
, 3.5
, 4,
3.5 4
<Enter>
3.75
3.75
...
, # !
. , ,
? , #
, . # !
A v e r a g e A n d D i s p l a y ( s t r i n g , d o u b l e , s t r i n g , d o u b l e )
A v e r a g e A n d D i s p l a y ( d o u b l e , d o u b l e ) .
, , .
( ) ,
.
,
,
,
.
,
,
. ,
.
,
, ,
.
.
D i s p l a y R o u n d e d D e c i m a l ( ) :
//
//
//
154
FunctionsWithDefaultArguments -
,
III. -
using
System;
namespace
F u n c t i o n s W i t h D e f a u l tA r g u m e n t s
(
public
class
Program
{
public
s t a t i c
void
Main(string[]
args)
// -
11
C o n s o l e . W r i t e L i n e ( " {0} ,
DisplayRoundedDecimal(12.345678M,
3));
//
Console.WriteLine(" <Enter> " +
" ...");
Console.Read();
}
//
DisplayRoundedDecimal
//
decimal
//
public
s t a t i c
string
DisplayRoundedDecimal(decimal
int
mValue,
nNumberOfSignificantDigits)
{
//
//
decimal mRoundedValue =
decimal.Round(mValue,
nNumberOfSignificantDigits);
//
string s = Convert.ToString(mRoundedValue);
return
s,-
}
public
s t a t i c
string
DisplayRoundedDecimal(decimal
mValue)
{
// D i s p l a y R o u n d e d D e c i m a l ( d e c i m a l ,
int)
//
string s = DisplayRoundedDecimal(mValue,
2);
return
s;
D i s p l a y R o u n d e d D e c i m a l ( )
d e c i m a l
s t r i n g
. d e c i m a l ,
, 2.
, D i s p l a y R o u n d e d D e c i m a l ()
' 7.
155
.
, D i s p l a y R o u n d e d D e c i m a l ( d e c i m a l
D i s p l a y R o u n d e d D e c i m a l
( d e c i m a l , i n t ) .
.
, |
.
,
,
,
, !
. !
, .
Visual Basic C/C++: #
. #
.
-
, i n t , d o u b l e , d e c i m a l ,
.
. (by valu
.
.
-, " ",
" ".
, - id
d o u b l e , ..
, , .
, :
// PassByValue -
//
using
System;
namespace
PassByValue
{
public
class
Program
156
III.
// Update -
// ,
; ,
//
public s t a t i c v o i d U p d a t e ( i n t i,
double d)
{
i = 10;
d = 2 0.0;
}
public
s t a t i c
void
Main(string []
args)
{
// ,
int i = 1;
double d = 2.0;
Console.WriteLine(" U p d a t e ( i n t , d o u b l e ) : " ) ;
C o n s o l e . W r i t e L i n e ( " i = " + i + ", d = " + d ) ;
//
U p d a t e ( i , d) ;
// 1 2 . 0
Console . W r i t e L i n e (" U p d a t e ( i n t , d o u b l e ) :") ,C o n s o l e . W r i t e L i n e ( " i = " + i + ", d = " + d) ;
//
Console.WriteLine(" <Enter> " +
" ...");
Console.Read();
:
U p d a t e ( i n t , d o u b l e ) :
i=1, d = 2
U p d a t e ( i n t , d o u b l e ) :
i = 1, d = 2
< E n t e r > . . .
U p d a t e () 1 2.0, i .
,
.
-
, ,
. P a s s B y R e f e r e n c e
.
#
r e f o u t .
P a s s B y V a l u e , .
7.
157
// PassByReference -
//
using
System;
namespace
PassByValue
{
public
class
Program
{
// Update -
// ,
; ,
// ref out
public s t a t i c void Update(ref
int i,
out double d)
{
i =
d =
10;
20.0;
}
public
s t a t i c
void
Main(string[]
args)
{
//
i n t i = 1;
double
d;
Console.WriteLine(" " +
"Update(ref
int,
out double):");
Console.WriteLine("i = " + i +
", d " ) ;
'
//
Update(ref i,
out d);
//
//
20.0
Console.WriteLine("
10,
"
"Update (ref
int,
out double):"),C o n s o l e . W r i t e L i n e ( " i = " + i + ", d = " + d ) ;
/ /
Console.WriteLine(" <Enter>
"
"
...");
Console.Read();
r e f #, i,
. , , , .
, o u t : " ,
,
".
, ,
.
158
III. -
P a s s B y R e f
:
U p d a t e ( r e f i n t , o u t d o u b l e ) :
i = 1, d
U p d a t e ( r e f i n t , o u t d o u b l e ) :
i = 10, d = 20
< E n t e r > . . .
, o u t ,
r e f , .. r e f
o u t . ,
, 2
i n t , .
, i d U p
d a t e d . M a i n ()
U p d a t e () . P a s s B y
Value, .
,
. ,
. U p d a t e ()
.
// PassByRef e r e n c e E r r o r -
/ /
//
using S y s t e m ;
namespace
PassByRef e r e n c e E r r o r
public
class
Program
{
// Update -
//
public s t a t i c v o i d D i s p l a y A n d U p d a t e ( r e f
int nVarl,
ref
i n t nVar2)
{
Console.WriteLine("
nVarl);
nVarl = 10;
nVarl
"
Console.WriteLine("
nVar2);
n V a r 2 = 2 0;
nVar2
"
}
public
static
void
Main(string[]
args)
7.
159
//
i n t = 1;
C o n s o l e . W r i t e L i n e ( " " +
"Update(ref n,
ref n ) : " ) ;
Console.WriteLine("n = " + n);
Console.WriteLine();
//
DisplayAndUpdate(ref
n,
ref
n);
// ,
// - ,
// ,
Console.WriteLine();
Console.WriteLine(" " +
"Update(ref n,
ref n ) : " ) ;
C o n s o l e . W r i t e L i n e ( " n = " + n) ;
//
Console.WriteLine(" <Enter> " +
" .. . " ) ;
Console.Read();
}
}
U p d a t e ( r e f i n t , r e f i n t )
,
. ,
, .
n V a r l , ,
1 10. nVar
:
, 1 20
,
10 20.
:
U p d a t e ( r e f n,
ref n ) :
n = 1
nVarl
nVar2
1
10
U p d a t e ( r e f n,
ref
n = 20
< E n t e r >
n):
...
, , U p d a t e ( ) ,
, , . ,
.
, ,
.
160
III. -
,
.
?
# ,
.
(
-). # ,
, :
int n V a r i a b l e ;
Console.WriteLine("
n V a r i a b l e = 1;
Console.WriteLine("
"
+,nVariable);
"
nVariable);
C# :
void
SomeFunction(ref
int
nVariable)
{
Console.WriteLine("
"
nVariable);
},
S o m e F u n c t i o n () , n V a r i a b l e
? . #
,
:
int n U n i n i t i a l i z e d V a r i a b l e ;
SomeFunction(ref n U n i n i t i a l i z e d V a r i a b l e ) ;
C# , S o m e F u n c t i o n ()
(.. ( g a r b a g e ) )
. o u t
,
, .
- :
int n U n i n i t i a l i z e d V a r i a b l e ;
SomeFunction(out n U n i n i t i a l i z e d V a r i a b l e ) ;
o u t -
:
int n l n i t i a l i z e d V a r i a b l e = 1;
SomeFunction(out n l n i t i a l i z e d V a r i a b l e ) ;
n l n i t i a l i z e d V a r i a b l e
S o m e F u n c t i o n ( ) ,
.
7.
161
,
, . , s i n ()
""
.
r e t u r n ;
.
return
,
public
class
Example
{
public
static
double
Average(double
dl,
double
d2)
{
double dAverage =
return
dAverage;
(dl
d2)
2;
}
public
static
void
TestO
{
d o u b l e vl = 1.0;
d o u b l e v2 = 3 . 0 ;
double dAverageValue = Average(vl, v2);
C o n s o l e . W r i t e L i n e ( " " + vl
+ " " + v2 + " "
+ dAverageValue);
//
C o n s o l e . W r i t e L i n e ( " " + vl
+ " " + v2 + " "
+ Average(vl, v2));
}
}
, p u b l i c s t a t i c double
A v e r a g e () d o u b l e , Av
e r a g e () d o u b l e .
A v e r a g e () dl d2 ,
. d A v e r a g e ,
. , d A v e r a g e ,
.
, " d A v e r a g e " .
. , dAver
a g e .
, d A v e r a g e .
A v e r a g e () T e s t () ,
; d o u b l e , A v e r a g e d ,
d A v e r a g e V a l u e .
162
III. - ]
, (, , A v e r a g e ( ) ) ,
,
# ,
? r e t u r n .
r e f o u t . U p d a t e ()
" " :
// U p d a t e -
//, ; ,
// r e f o u t
public static void Update (ref int i, out double d)
{
i = 10;
d = 20.0;
}
v o i d ,
. i r e f ,
d o u t , ,
Update ( ) , . ,
.
: "
r e t u r n , , .
?" , Av
erage () :
public c l a s s
Example
// : , ' o u t ' ,
//
public s t a t i c v o i d A v e r a g e ( d o u b l e 1 , d o u b l e d 2 ,
out double dResults)
{
dResults = (dl + d2) / 2;
}
public
static
void
Test()
{
double vl = 1.0;
d o u b l e v2 = 3 . 0 ;
double
dAverageValue;
Average(dAverageValue,
vl,
v2);
C o n s o l e . W r i t e L i n e ( " " + vl
+ " " + v2 + " "
+ dAverageValue);
}
}
7.
163
r e t u r n , o u t - , ;
.
!
, 14, "!
". |
.
^S^
, " o u t " ,
, :
public
class
Example
{
public
static
void AverageAndProduct(double dl, double d2,
out double dAverage,
out double dProduct)
{
}
dAverage
dProduct
=
=
( d l + d2)
dl * d 2 ;
/ 2 ;
,
. , ,
, ;
, . *
.
, -,
n u l l . (.. ,
n u l l ) , . ,
:
class
{
Example
int
nValue,-
}
// r e f l
Example
refl;
//
E x a m p l e r e f 2 = new E x a m p l e ;
r e f 2 . n V a l u e = 0;
r e f l , . "", ..
. r e f 2
, .
, :
string
string
164
si;
s2 =
"";
III. -
fj
, si ,
s2 (
). ,
:
// T e s t namespace
Test
(
using
System;
public
class
Program
{
public
s t a t i c
void
Main(string []
strings)
{
Console.WriteLine(" " +
" T e s t S t r i n g ( ) " ) ;
Console.WriteLine();
Example e x a m p l e O b j e c t = new E x a m p l e ( ) ;
Console.WriteLine("
:");
string s = null;
exampleObject.TestString(s);
Console.WriteLine();
//
()
Console.WriteLine(" : " ) ;
exampleObject.TestString("");
Console.WriteLine() ;
// ,
Console.WriteLine(" :");
exampleObject.TestString("test
string");
Console.WriteLine();
//
Console.WriteLine(" <Enter> " +
" . . . ") ;
C o n s o l e . R e a d () ,-
class
Example
{
public
void
TestString(string
sTest)
{
//
//
if
{
,
! )
(sTest == n u l l )
Console.WriteLine("sTest
return;
==
n u l l " ) ;
}
//
//
//
//
,
s T e s t ,
.
,
s T e s t
()
if
(String.Compare(sTest,
11
Console . WriteLine (
return;
7.
sTest
"")
-
==
0)
");
165
// ,
} Console.WriteLine("sTest :
'"
sTest +
T e s t S t r i n g O s T e s t = = n u l l ,
. ,
, T e s t S t r i n g () C o m p a r e () ( Com
p a r e () 0, . 9, "
# " , ).
:
T e s t S t r i n g O
:
sTest == n u l l
:
s T e s t -
:
sTest :
'test string'
< E n t e r > . . .
p u b l i c s t a t i c d o u b l e A v e r a g e A v e r a g e d
d o u b l e . ,
. A v e r a g e A n d D i s p l a y ,
,
.
, # v o i d :
public
void
AverageAndDisplay(double,
double)
v o i d , , ,
, .. , A v e r a g e A n d D i s p l a y ()
. ( #
, v o i d . )
, , void, .
, v o i d - ,
r e t u r n ,
. v o i d -
, r e t u r n
( )
.
D i s p l a y R a t i o ( ) :
public
class
Example
166
III. -
public
//
if
static
void
DisplayRatio(double
double
( d D e n o m i n a t o r ==
0.0)
dNumerator,
dDenominator)
0...
{
// . . .
// . . .
C o n s o l e . W r i t e L i n e ( "
//
return;
");
}
// ,
//
double dRatio = dNumerator / dDenominator;
Console.WriteLine(" " + dNumerator
+ " " + dDenominator
+ " " + d R a t i o ) ;
} // ,
}
//
D i s p l a y R a t i o () , d D e
n o m i n a t o r .
d D e n o m i n a t o r ,
,
.
0 .
d D e n o m i n a t o r ,
. W r i t e
L i n e () D i s p l a y R a t i o () ,
, .
.
M a i n ( ) .
M a i n ( ) :
public
static
void
Main ( s t r i n g []
args)
{
//
...
...
M a i n () P r o g r a m , Visual Stu
dio AppWizard.
s t r i n g . ?
.
.
copy m y F i l e : \ m y D i r , m y F i l e : \ m y D i r .
7.
167
D i s p l a y
A r g u m e n t s , s t r i n g ,
M a i n ( ) ,
.
//
DisplayArguments
using
System;
namespace
DisplayArguments
{
class
Test
public
// ,
// Program
s t a t i c
void
Main(string[]
Main(),
args)
{
//
C o n s o l e . W r i t e L i n e ( "
args.Length);
// :
i n t nCount = 0;
foreach
(string
arg
in
{0}
",
args)
{
Console.WriteLine("
nCount++,
{}
arg);
{l}",
}
/ /
Console.WriteLine(" <Enter> " +
" ...");
Console.Read();
r e t u r n 0;
//
//
//
//
,
,
;
, M a i n ( ) 4
, v o i d , .
s t a t i c .
a r g s .
, . |
a r g s , .
( ]
, ):
DisplayArguments
argl
arg2
3
0 /
1
- argl
2
- arg2
<Enter>
...
, (
, ).
168
III. - ,
, . "/ " . ,
a r g s ,
. ,
.
DOS
D i s p l a y A r g u m e n t s DOS,
.
1.
S t a r t s P r o g r a m s ^ A c c e s s o r i e s ^ C o m m a n d Prompt
( ^ ^ ).
: \ > ( ).
2. , D i s p l a y A r g u m e n t s ,
cd
\C#Programs\DisplayArguments\bin\Debug.
(
# P r o g r a m s . , .)
: \ C # P r o g r a m s \ D i s p l a y A r g u m e n t s \
bin\Debug>.
- Windows
. Windows
: \ C # P r o g r a m s Search
(...), . 7.2.
7.
169
D i s p l a y A r g u m e n t s .
Search ().
Search Result ( ), . 7.3.
D i s p l a y A r g u m e n t s . e x e o b j \ D e b u g ;
b i n \ D e b u g . ,
, .
. 7.3. ;
Visual Studio 2005 bin
D e b u g ; b i n \ r e l e a s e Visual Studio.
Windows ,
DOS . Windows Windows
. ,
My S t u f f
cd
\"
Stuff"
3. , :\C#Programs
DisplayArguments\bin\Debug>,
displayarguments
argl
arg2.
D i s p l a y A r g u m e n t s . ,
, . 7.4. ,
,
, , . .
D i s p l a y A r g u m e n t s . e x e ,
Command Prompt ( ).
Windows,
Windows.
170
III. -
. 7.4. DisplayArguments.
DOS
. 7.5, D i s p l a y A r g u m e n t s
.
a r g l . t x t a r g 2 . t x t
D i s p l a y A r g u m e n t s , . 7.6.
<Ctrl>
. 7.6.
7.
, ,
,
,
D i s p l a y A r g u m e n t s .
171
Puc. 7.7.
,
172
III. -
,
.
W r i t e L i n e ()
, WriteLine (),
, Console:
Console.WriteLine(" " ) ;
WriteLine () ,
.NET. Console ,
.
WriteLine (), ,
string. +
, , :
string s = "";
Console. WriteLine (" " + s +
" " + 3 + " " ) ;
"
'".
WriteLine ()
, :
Console .WriteLine (" { } {l}
"", 3 ) ;
",
.
"" {0}
. 3 ,
{l}. ,
, , .
,
, ,
WriteLine (). 9, " #".
7.
173
Visual Studio , . :
, , Visual Studio,
. .
1. Solution Explorer, !
V i e w : Solution Explorer.
Solution Explorer .
. .
, D i s p l a y A r g u m e n t s , P r o g r a m . c s
,
, ,
D i s p l a y A r g u m e n t s Visual Studio.
2. DisplayArguments Solution E
plorer Properties,
. 7.8.
, . 7.9,
, ,
.
Puc. 7.8.
Solution Explorer
3. DisplayArguments ]
Debug.
4. Command Line Arguments Start Options ,!
Visual Studio.
174
III. -
7.
176
III. -
>
>
> t h i s
>
, 7, " ",
.
, . , - , ,
, .
.
, ,
-,
.
,
:
// P a s s O b j e c t
using S y s t e m ;
namespace
public
PassObject
class
public
Student
string
sName;
}
public c l a s s P r o g r a m
(
public s t a t i c v o i d M a i n ( s t r i n g [ ]
{
Student
student
new
args)
Student();
//
//
Console.WriteLine(":");
student.sName =
"Madeleine";
OutputName(student);
//
Console.WriteLine("
:");
SetName(student,
"Willa");
OutputName(student);
//
// OutputName
public
static
-
void OutputName(Student
student)
{
//
Console.WriteLine("Student.sName
=
student.sName);
{o}",
}
// SetName -
public
s t a t i c void SetName(Student
student,
string
sName)
{
student.sName
sName;
s t u d e n t , , ~
Out
putName().
S e t N a m e ( ) .
# , , stud
d e n t , . M a i n ()
, ,
:
:
Student.sName = Madeleine
:
Student.sName = Willa.
< E n t e r >
,
r e f . ,
, ,
,
:
178
III. -
Student s t u d e n t = n e w S t u d e n t ( ) ;
student.Name = " M a d e l e i n e " ;
OutputName ( s t u d e n t ) ;
Console. W r i t e L i n e ( s t u d e n t . N a m e ) ;
I //
public
static
void
//
"Madeleine"
O u t p u t N a m e () :
OutputName (Student
student)
}
student
new
student.Name
Student();
//
//
//
He
s t u d e n t
OutputName()
"Pam";
,
. , V e h i c l e
, , ..
: ,
.. ,
. V e h i
cle, -.
-
,
:
// P a s s O b j e c t T o M e m b e r F u n c t i o n -
// -
UBing S y s t e m ;
namespace PassObjectMemberToFunction
{
public
class
Student
(
public
string
sName;
// OutputName -
public
static void OutputName(Student
student)
{
/ /
Console.WriteLine("Student.sName
=
student.sName);
{o}",
}
// SetName -
public s t a t i c v o i d S e t N a m e ( S t u d e n t
ft
student,
179
string
sName)
s t u d e n t . s N a m e = sName;
}
public
class
Program
{
public
static
void
Main(string[]
args)
{
Student
student
= new S t u d e n t ( ) ;
//
// Console .WriteLine(":");
student.sName = "Madeleine";
Student.OutputName(student);
//
//
// Student
//
Console.WriteLine("
:");
Student.SetName(student, "Willa");
Student.OutputName(student);
//
Console.WriteLine(" <Enter> " +
" ...");
Console.Read();
P a s s O b j e c t |
: O u t p u t N a m e () S e t N a m e () S t u d e n t .
- M a i n () |
S t u d e n t . Stu-|
d e n t , P r o g r a m , M a i n ( )
, . O u t p u t N a m e !
:
, Output
N a m e () , ,
, S t u d e n t , .
. P r o g r a m
, S t u d e n t , , :
. Stu
d e n t . .
M a i n ()
Madeleine <
S e t N a m e ( ) .
180
III. -
S t u d e n t -
. S e t N a m e () O u t p u t feme ( ) , . , # ,
.
, :
Student s t u d e n t
student.sName
= new S t u d e n t () ;
= "Madeleine";
//
//
Student
C# - :
student. S e t N a m e ( " M a d e l e i n e " ) ;
:
//InvokeMethod
using S y s t e m ;
namespace
InvokeMethod
{
class S t u d e n t
{
//
public
string
public
string
// SetName
public
void
sFirstName;
sLastName;
SetName(string
sFName,
string
sLName)
{
sFirstName
sLastName
=
=
sFName;
sLName;
}
// ToNameString -
//
public
s t r i n g ToNameString()
Stude
{
string
return s;
sFirstName
"
"
sLastName;
}
}
public
class
Program
{
public
s t a t i c
void
Main()
{
Student
student
new
Student();
student.SetName("Stephen",
8.
"Davis");
181
Console.WriteLine("
"
+ student.ToNameString());
//
Console.WriteLine(" <Enter> " +
" . . . ") ;
Console.Read();
}
}
}
:
Stephen
Davis
P a s s O b j e c t T o M e m b e r F u n c t i o n . B
,
s t u d e n t S t u d e n t ,
S e t N a m e ( ) , " S t e p h e n " "Davi
- s F i r s t N a m e s L a s t N a m e . ,
- T o N a m e S t r i n g ( ) , ,
, #,
. ]
-,
.
() ().
S e t N a m e ( ) ,
S t u d e n t . SetName!
, :
S t u d e n t c h r i s t a = new S t u d e n t ( ) ; //
Student sarah
= new S t u d e n t ( ) ; //
christa.SetName("Christa",
"Smith");
sarah.SetName("Sarah",
"Jones");
S e t N a m e () c h r i s t a ,
sarah.
# , .
c h r i s t a , s a r a h .
, , .
:
public
class
Person
{
public
void
Address()
{
Console.WriteLine("Hi");
182
III. -
public
(
class
Letter
string sAddress;
//
public
void
Address(string
sNewAddress)
}
sAddress = sNewAddress;
}
}
A d d r e s s () .
A d d r e s s () P e r s o n A d d r e s s ()
L e t t e r . - , A d d r e s s ( ) ,
A d d r e s s () ?
, . A d d r e s s ()
son. A d d r e s s ( ) L e t t e r . A d d r e s s ( ) .
, .
. ,
( , ,
). (
), .
, ,
. ,
,
, .
, A d d r e s s () ,
.
S t u d e n t . S e t N a m e ( ) :
class S t u d e n t
//
public s t r i n g s F i r s t N a m e ;
public s t r i n g s L a s t N a m e ;
// SetName -
public v o i d S e t N a m e ( s t r i n g sFName,
s t r i n g sLName)
sFirstName = sFName;
sLastName = sLName ;
}
}
public class Program
{
8.
public static void Main()
183
S t u d e n t s t u d e n t l = new S t u d e n t ( ) ;
studentl.SetName("Joseph",
"Smith");
S t u d e n t s t u d e n t 2 = new S t u d e n t ( ) ;
student2.SetName("John",
"Davis");
}
}
M a i n () S e t N a m e () ,
s t u d e n t l s t u d e n t 2 . S e t N a m e ()
S t u d e n t . , "
, ?
. -I
,
studentl.SetName("Joseph",
"Smith");
:
Student.SetName(studentl,
"Joseph",
"Smith");
// - (
// )
, S e t N a m e ()
, , .
,
. .
?
:
public
class
Student
{
p u b l i c s t r i n g sFirstName,public
s t r i n g sLastName;
public void SetName(string sFirstName,
s t r i n g sLastName)
SetFirstName(sFirstName);
SetLastName(sLastName);
public
void
SetFirstName(string
sFirstName
public
void
sLastName
sName;
SetLastName(string
=
sName)
sName)
sName;
}
S e t F i r s t N a m e () . , |
, .
, ,
.
184
III. - ]
this
,
, , . #
t h i s ,
,
.
t h i s #,
, .
, :
public
class
Student
(
public
public
public
string sFirstName;
string sLastName;
void SetName(string
sFirstName,
string
sLastName)
// " "
// t h i s
this.SetFirstName (sFirstName) ;
this.SetLastName (sLastName) ;
public
void
SetFirstName(string
this. sFirstName
public
void
sName;
SetLastName(string
this. sLastName
sName)
sName)
sName;
t h i s . t h i s
, t h i s
. M a i n () , t h i s s t u d e n t l
S e t N a m e ( ) , , :
studentl. S e t N a m e ( " J o h n " ,
"Smith" ) ;
this
t h i s ,
, . ,
. , t h i s
:
class
Person
(
public s t r i n g s N a m e ;
public i n t n I D ;
public v o i d I n i t ( s t r i n g
8.
sName,
int
nID)
185
this.sName
this.nID
sName;
// ,
// -
= nID;
}
}
I n i t () s N a m e n I D ,
-. ,
, . !
, s N a m e , -.
.
t h i s , ,
s N a m e .
I n i t ()
sName
t h i s . s N a m e .
t h i s
. Ref
enc ingThi s Exp1i i tly:
// R e f e r e n c i n g T h i s E x p l i c i t l y
// t h i s
using
System;
namespace
ReferencingThisExp1iitly
{
public
class
Program
{
public
s t a t i c
void
Main(string[]
strings)
{
//
S t u d e n t s t u d e n t = new
student.Init("Stephen
Student();
Davis",
1234);
' //
Console.WriteLine(" Stephen
" Biology 101");
student.Enroll("Biology
101");
//
Console.WriteLine("
student.DisplayCourse();
"
:");
//
Console.WriteLine(" <Enter>
"
Davis
"
...");
Console.Read();
}
}
// S t u d e n t public
class
186
Student
III. -
//
public
public
string
int
sName;
nID;
// ,
Courselnstance
// I n i t
public
courselnstance;
void
I n i t ( s t r i n g
sName,
int
nID)
{
this.sName
this.nID
=
=
sName;
nID;
courselnstance
null;
}
// E n r o l l -
public
void
Enroll (string
sCourselD)
{
c o u r s e l n s t a n c e = new C o u r s e l n s t a n c e ( ) ;
courselnstance.Init(this,
sCourselD);
//
public
void
Display-Course ()
{
Console . W r i t e L i n e (sName)
c o u r s e l n s t a n c e . D i s p l a y ()
;
;
// C o u r s e l n s t a n c e -
//
public
class
Courselnstance
{
public
public
Student
string
student;
sCourselD;
// I n i t -
public
void
Init(Student
student,
string
sCourselD)
(
this.student
this.sCourselD
=
=
student;
sCourselD;
// D i s p l a y -
public
void
D i s p l a y ()
{
Console.WriteLine(sCourselD);
km 8.
187
S t u d e n t
(, ...).
s t u d e n t , I n i t ( )
. c o u r s e l n s t a n c e n u l l ,
.
E n r o l l ()
s e l n s t a n c e . C o u r s e l n s t a n c e . I n i t ( )
S t u d e n t . )
? , S t u d e n
, t h i s .
this
, # .
, ,
:
// M i x i n g F u n c t i o n s A n d M e t h o d s -
//
using
System;
namespace
MixingFunctionsAndMethods
{
public
class
Student
{
public
public
string
string
sFirstName;
sLastName;
// I n i t S t u d e n t - s t u d e n t
public void InitStudent(string sFirstName,
string
sLastName)
this.sFirstName = sFirstName;
this.sLastName = sLastName;
// O u t p u t B a n n e r -
public
s t a t i c void OutputBanner()
Console.WriteLine("
:");
// Console.WriteLine(?
public
void
OutputBannerAndName()
// Student,
//
OutputBanner();
188
?);
III. -
//
OutputName(this);
}
// OutputName -
public
s t a t i c void OutputName(Student
student)
{
//
Console.WriteLine("
{}",
student.ToNameString());
}
II T o N a m e S t r i n g -
public
s t r i n g ToNameString()
{
//
//
//
ret
t h i s :
return this.sFirstName + " " + this.sLastName;
urn sFirstName + " " + sLastName;
}
public
class
Program
{
public
s t a t i c
void
Main(string[]
args)
{
S t u d e n t s t u d e n t = new S t u d e n t ( ) ;
student.InitStudent("Madeleine",
//
Student.OutputBanner();
Student.OutputName(student);
Console.WriteLine();
//
student.OutputBannerAndName();
"Cather");
//
Console.WriteLine(" <Enter> " +
" ...");
Console.Read();
, M a i n ( ) ,
. S t u d e n t
.
.
M a i n ()
. O u t p u t B a n n e r ()
O u t p u t N a m e () . O u t p u t B a n n e r ()
8.
189
. O u t p u t N a m e ()
S t u d e n t , .
M a i n ( )
s t u d e n t . O u t p u t B a n n e r A n d N a m e ( ) .
s t u d e n t . O u t p u t B a n n e r A n d N a m e ( )
O u t p u t B a n n e r ( ) . , ,
, , ,
, .. S t u d e n t . O u t p u t N a m e ( ) .
, S t u d
S t u d e n t . stu
d e n t . O u t p u t B a n n e r A n d N a m e ( ) t h i s .
T o N a m e S t r i n g () Output
N a m e ( ) . O u t p u t N a m e ( ) s t a t i c , ,
t h i s . S t u d e n t ,
.
. .
.
,
.
Visual Studio ,
(auto-complete).
, Visual Studio ,
.
Visual Studio .
, Visual Studio,
M i x i n g F u n c t i o n s A n d M e t h o d s :
//
Student.OutputBanner();
Student.OutputName(student);
Console.WriteLine();
//
student.OutputBannerAndName();
, ,
Help^Index . ,
, , , # .NET
,
.
.
, .
190
III. - .
M i x i n g F u n c t i o n s A n d M e t h o d s
, C o n s o l e . , Visual Studio
C o n s o l e . W, Visual Studio
, W ( W r i t e ( ) ) .
<1>, W r i t e L i n e ( ) ,
(. 8.1). , , , 19
, .
8.
191
M i x i n g F u n c t i o n s A n d M e t h o d s
, C o n s o l e ., Visual Studio
C o n s o l e . W, Visual Studio
, W ( W r i t e ( ) ) .
<1>, W r i t e L i n e ( ) ,
(. 8.1). , , , 19
, .
'
8.
191
W r i t e L i n e ().
(
). W r i t e L i n e () 19
. ,
.
.
W r i t e L i n e ,
" s o m e s t r i n g { 0 } " ( ),
, Visual Studio
, . 8.3.
Visual Studio (
, .NET).
. " s o m e s t r i n g { } "
: C o n s o l e . W r i t e L i n e .
s t u d e n t . , Vis
Studio S t u d e n t .
, Visual Studio
, . 8.4. ,
, .
,
- .
: -
, .
192
III. -
.
Student, , Visual Studio
. O u t p u t N , Visual Studio
OutputName ( ) , . 8.5.
Visual Studio ,
, ,
, .
Visual Studio
. , Visual Studio ,
8.
193
O u t p u t N a m e ( ) ,
. .
# / /. Visual Stud
, ///.
Visual
Studio
, .
, Java,
. #
,
.
. 8 . 1 .
X M L 7 H T M L :
< c o m m a n d > </command
XML.
, Help^lndex
"XML documentation features [#]".
M i x i n g
FunctionsAndMethods:
/ / M i x i n g F u n c t i o n s A n d M e t h o d s W i t h X M L T a g s - -
//
//
using
System;
namespace
MixingFunctionsAndMethods
{
///
<summary>
/ / /
///
</summary>
public class Student
{
///
<summary>
/ / /
194
III. -
string
sLastName;
// I n i t S t u d e n t
/// <summary>
/// .
/// < / s u m m a r y >
/// < p a r a m n a m e = " s F i r s t N a m e " > < / >
/// < p a r a m n a m e = " s L a s t N a m e " > < / >
public v o i d
I n i t S t u d e n t ( s t r i n g
sFirstName,
string
sLastName)
{'
this.sFirstName = sFirstName;
this.sLastName = sLastName;
// O u t p u t B a n n e r
/// <summary>
///
///
</summary>
public
s t a t i c
void
OutputBanner()
{
Console.WriteLine("
:");
// Console.WriteLine(?
?);
}
// O u t p u t B a n n e r A n d N a m e
/// < s u m m a r y >
///
/// < / s u m m a r y >
public
void
OutputBannerAndName()
{
// S t u d e n t ,
//
OutputBanner();
//
OutputName(this,
5);
// O u t p u t N a m e
/// < s u m m a r y >
///
/// < / s u m m a r y >
/// < p a r a m n a m e = " s t u d e n t " > ,
///
</>
/// < p a r a m n a m e = " n I n d e n t " > K ^ H 4 e c T B O
///
</>
/// < r e t u r n s > B b m e f l e H H a f l
cTpoKa</returns>
public s t a t i c
string OutputName(Student
student,
int
nlndent)
//
s t r i n g s = new S t r i n g ( '
',
nlndent);
s += S t r i n g . F o r m a t ( " {}",
student.ToNameString());
Console.WriteLine(s);
return
s;
}
//
ToNameString
///
<summary>
///
///
</summary>
///
<returns>CTpoKy
public
string
</returns>
ToNameString()
{
//
//
//
ret
t h i s :
return this.sFirstName + "
" + this.sLastName;
u r n sFirstName + " " + sLastName;
}
///
///
<summary>
,
///
Student
</summary>
public
class
Program
{
///
///
<summary>
///
</summary>
///
<param
name= "args
///
public
.
11
>
</>
s t a t i c
void
Main(string []
args)
{
S t u d e n t s t u d e n t = new S t u d e n t ( ) ;
student.InitStudent("Madeleine",
"Cather");
//
Student.OutputBanner();
string s = Student.OutputName(student,
Console.WriteLine();
//
student.OutputBannerAndName();
/ /
Console.WriteLine(" <Enter>
"
5);
"
...");
Console.Read();
}
}
}
.
,
196
III. -
. S t u
dent . O u t p u t N a m e ( ) M a i n ( ) .
1. Visual Studio . O u t p u t Name
<summary>
</summsiry>, . 8.6. .
2. , Visual Studio
, < p a r a m x / p a r a m > .
, ,
.
3. Visual Studio , n l n d e n t .
XML-
Visual Studio XML-.
, , .
, XML-, ,
.
XML- .
1. Project^ Properties.
2. Build Output
XML Documentation File.
.
3. Properties.
8.
197
Solution Explorer.
4. B u i l d : Rebuild Solution
, , .
5. b i n \ D e b u g M i x i n g F u n c t i o n s A n d M e t
o d s W i t h X M L T a g s ( ,
XML-).
. 2
,
XML-.
198
III. -
#
...
>
}
>
s t r i n g
- i n t c h a r .
, , :
int i = 1;
string s =
"abc";
/ /
/ /
i n t
string
, ,
:
string si = new S t r i n g ( ) ;
string s2 = " a b e d " ;
int n L e n g t h O f S t r i n g = s 2 . L e n g t h ;
s t r i n g - ? S t r i n g
, #
. , s t r i n g
S t r i n g , :
String si = " a b e d " ; //
//
string s2 = s i ;
//
//
S t r i n g
S t r i n g
si S t r i n g (
S ), s2 s t r i n g
( s ). , s t r i n g
S t r i n g ( ) .
. i n t I n t 3 2 , d o u b l e
D o u b l e ..; , s t r i n g S t r i n g
.
,
#. , ""
, :
s t r i n g sName = " R a n d y " ;
C o n s o l e . W r i t e L i n e ( "
"
sName);
//
S t r i n g . S t r i n g
.
" S t r i n g
c l a s s " .
!
:
s t r i n g , . -1
, # , s t r i n g .
, s t r i n g , ,
s t r i n g .
, " -
, "
- R a n d y " .
, , - .
:
//
//
//
usi
M o d i f y S t r i n g - S t r i n g
(s.ToUpperO
s;
)
ng
System;
namespace
class
ModifyString
Program
{
public
static
void
Main(string[]
args)
{
// Student
S t u d e n t si = new S t u d e n t ( ) ;
sl.sName = "Jenny";
//
S t u d e n t s2 = new S t u d e n t ( ) ;
s2.sName = s i . s N a m e ;
//
//
200
"" si
,
ToUpperO
III. -
// ,
s2.sName = s i . s N a m e . T o U p p e r ( ) ;
Console.WriteLine("si - { o } ,
s1.sName,
s2.sName);
s2 - {l}",
//
Console.WriteLine(" <Enter> " +
" . . . " ) ;
Console.Read();
}
II S t u d e n t - ,
class S t u d e n t
{
public String sName;
}
S t u d e n t si s2 , s N a m e
. T o U p p e r () s i . s N a m e ,
. , , si s2
, , T o U p p e r ()
sName, , .
:
Sl - Jenny,
s2 - J E N N Y
< E n t e r > . . .
( ).
.
"
" , 1
int. , ,
, .
, .
,
Compare ( ) , , "-".
, C o m p a r e () 1.
, C o m p a r e () -1.
, C o m p a r e () 0.
C o m p a r e ( ) ,
:
compare (string s l ,
s t r i n g s2)
{
ft #
// ,
//
//
f o r e a c h ( )
if ( sl >
s2)
return 1
if ( sl <
s2)
r e t u r n -1
// , sl
// s2, s2
if sl
return 1
// , s2
//, s l , s l
if s2
r e t u r n -1
// ,
//
return
}
, " a b e d " " a b b d " , a " a b e d e " " a b e d " . ,
, ,
"-" .
C o m p a r e () 0, .
, .
B u i l d A S e n t e n c e
.
. ,
E X I T , e x i t , Q U I T q u i t .
// BuildASentence -
// ,
//
// .
//
using System;
namespace BuildASentence
{
public
class
Program
{
public
args)
{
Console.WriteLine("
" , "
202
III.
"
" E X I T or Q U I T " ) ;
//
// ,
//
string s S e n t e n c e = "" ;
for( ; ; )
{
//
Console.WriteLine(" " ) ;
string s L i n e = C o n s o l e . R e a d L i n e ( ) ;
//
if ( I s T e r m i n a t e S t r i n g ( s L i n e ) )
{
break;
}
//
sSentence = S t r i n g . C o n c a t ( s S e n t e n c e , s L i n e ) ;
//
Console.WriteLine("\ : { } " , s S e n t e n c e ) ;
}
Console.WriteLine("\:\n{OJ", s S e n t e n c e ) ;
//
Console.WriteLine(" <Enter> " +
" . ..'"');
Console.Read();
}
// I s T e r m i n a t e S t r i n g - t r u e ,
/ /
public s t a t i c b o o l I s T e r m i n a t e S t r i n g ( s t r i n g s o u r c e )
{
string[] s T e r m s = { " E X I T " , " e x i t " , " Q U I T " , " q u i t " };
//
//
foreach
(string
sTerm
in
sTerms)
// true
if ( S t r i n g . C o m p a r e ( s o u r c e , s T e r m )
== 0)
return
true;
return f a l s e ;
s S e n t e n c e , "" .
w h i l e (true)
for (; ;)
, b r e a k
( ) r e t u r n ( ).
, .
5, " ".
9. #
203
,
R e a d L i n e ( ) .
,
I s T e r m i n a t e S t r i n g ( ) . t r u e ,
, f a l s e .
,
t r u e f a l s e , Is, H a s , C a n
. ,
, #
, .
,
S t r i n g . C o n c a t ( ) .
, , .
I s T e r m i n a t e S t r i n g () s T e r m s .
.
, ,
.
" E X I T " , " e x i t " ,
C o m p a r e () (
, " e x i t " " E x i t " ,
).
I s T e r m i n a t e S t r i n g ()
. m
p a r e () ,
t r u e ;
,
fa 1 .
B u i l d A S e n t e n c e .
, EXIT or QUIT
#
:
-
:
EXIT
204
III.
# -
< E n t e r > . . .
.
C o m p a r e ( ) , I s T e r m i n a t e S t r i n g ( ) ,
" E X I T " " e x i t " .
C o m p a r e ( ) , .
, ( t r u e )
( false). 7, " ".
I s T e r m i n a t e S t r i n g ()
t r u e ,
.
// I s T e r m i n a t e S t r i n g - t r u e ,
//
//
public s t a t i c b o o l I s T e r m i n a t e S t r i n g ( s t r i n g s o u r c e )
// , exit
/ / q u i t ,
return ( S t r i n g . C o m p a r e ( " e x i t " , s o u r c e , t r u e ) = = 0 )
(String.Compare("quit", source, true) == 0 ) ;
I s T e r m i n a t e S t r i n g ()
. ,
,
.
, I s T e r m i n a t e S t r i n g ()
if.
,
if.
switch
, s w i t c h
. s w i t c h
,
s t r i n g . I s
TerminateString () s w i t c h .
//IsTerminateString - t r u e ,
//
//
public static b o o l I s T e r m i n a t e S t r i n g ( s t r i n g s o u r c e )
{
switch (source)
{
case "EXIT" :
case "exit" :
9. #
205
case "QUIT":
case "quit":
return true;
}
return
false;
}
,
. f o r ()
, C o m p a r e ( ) , !
, ""
.
,
.
.
,
, f o r e a c h [],
StringToCharAccess
.
// S t r i n g T o C h a r A c c e s s -
//
using System;
namespace
StringToCharAccess
{
public
class
Program
{
public
args)
{
//
Console.WriteLine(" " +
" . " ) ;
string sRandom = C o n s o l e . R e a d L i n e ;
Console.WriteLine();
//
Console.WriteLine(" : " + s R a n d o m ) ;
Console.WriteLine();
//
Console.Write(" foreach: " ) ;
f o r e a c h ( c h a r in s R a n d o m )
{
Console.Write();
}
C o n s o l e . W r i t e L i n e ( ) ; .//
//
206
III.
Console.WriteLine();
/ /
Console.Write(" for: " ) ;
f o r ( i n t i = 0; i < s R a n d o m . L e n g t h ; i + + )
{
Console.Write{sRandom[i]);
Console.WriteLine();
//
//
Console.WriteLine(" <Enter> " +
" . . . " ) ;
Console.Read();
,
.
WriteLine ( s t r i n g ) . f o r e a c h ,
. , f o r
[ ]. :
.
: d j n d f v c k e x f q y f z c n h j r f
foreach:
for:
<Enter>
cnhjrf
cnhjrf
...
(
(white space) ,
, , , ).
T r i m ():
//
sRandom = s R a n d o m . T r i m () ;
S t r i n g . T r i m () . ,
,
.
R e a d L i n e () s t r i n g .
, ,
. # C o n v e r t
, ,
. ,
int.
ft #
207
string s = Console.ReadLine();
int n = C o n v e r t . T o I n t 3 2 ( s ) ;
/ / ,
//
: T o D o u b l e (), ToFloat
ToBoolean().
T o I n t 3 2 ( )
32-
(, 32 int),
int; !
l o n g Int 6 4 ().
"" ,
, , |
, .
t r u e , crpoi
.
, .
- TI
, ,
, .
, :
//
//
IsAllDigits -
public
static
bool
true,
IsAllDigits(string
sRaw)
{
// .
// ,
//
string s = s R a w . T r i m ( ) ; //
if ( s . L e n g t h == 0)
{
return
false;
}
//
f o r ( i n t i n d e x = 0; i n d e x < s . L e n g t h ; i n d e x + + )
{
// , ,
// ,
if ( C h a r . I s D i g i t ( s [ i n d e x ] ) == false)
{
return false;
}
}
// :
return true;
}
I s A l l D i g i t s |
. , !
. ,
. -
208
III.
, f a l s e , ,
. t r u e , ,
, , .
:
// IsAllDigits - ,
[// I s A l l D i g i t s
using S y s t e m ;
namespace
IsAllDigits
{
class P r o g r a m
{
public
args)
{
//
Console.WriteLine(" " ) ;
string s = C o n s o l e . R e a d L i n e ( ) ;
// ,
if
(!IsAllDigits(s))
{
Console.WriteLine("
!");
}
else
{
//
int n = I n t 3 2 . P a r s e ( s ) ;
// , 2
Console.WriteLine("2 * { } = { l } " , n, 2 * n ) ;
}
//
Console.WriteLine(" <Enter> " +
" . . . " ) ;
Console.Read();
}
// IsAllDigits - t r u e ,
//
//
public
static bool
IsAllDigits(string
sRaw)
{
//
II
, ,
I s A l l D i g i t s .
false, . ,
9. #
209
I n t 3 2 . P a r s e ( ) ,
C o n v e r t . T o I n t 3 2 ( ) . ,
(
).
:
1
!
<Enter>
...
C o n v e r t
,
.
,
,
1 1.
,
. S t r i n g . S p l i t ( ) ,
, , |
.
S p l i t ()
-. ,
S p l i t ( ) , , "1,2,
" 1", " 2" " 3".
Split]
.
// ParseSequenceWithSplit
// ,
//
namespace ParseSequenceWithSplit
{
using System;
class
Program
{
p u b l i c static v o i d M a i n ( s t r i n g []
args)
{
//
//
Console.WriteLine("
"
+
" , :");
//
string input = C o n s o l e . R e a d L i n e ;
Console.WriteLine();
210
III.
//
// -
//
1
1
c h a r [] c D i v i d e r s = { ' , ' ,
};
string[] segments = i n p u t . S p l i t ( c D i v i d e r s ) ;
//
int n S u m = 0;
f o r e a c h ( s t r i n g s in s e g m e n t s )
//
if
(
( s . L e n g t h > 0)
{
// ,
if ( I s A l l D i g i t s ( s ) )
{
// 32-
int n u m = I n t 3 2 . P a r s e ( s ) ;
Console.WriteLine(" = { } " , n u m ) ;
//
nSum += num;
}
//
Console.WriteLine("
{ } " ,
nSum);
//
Console.WriteLine(" <Enter> " +
" . . . ") ;
Console.Read();
// I s A l l D i g i t s - t r u e ,
//
public
static bool
IsAllDigits(string
sRaw)
{
// .
// - ,
//
string s = s R a w . T r i m f ) ; //
if ( s . L e n g t h == 0)
{
return f a l s e ;
}
//
for (int i n d e x = 0; i n d e x < s . L e n g t h ; i n d e x + + )
{
// , ,
// ,
if ( C h a r . I s D i g i t ( s [ i n d e x ] ) == f a l s e )
return
false;
tea 9. #
211
}
// :
return true;
}
P a r s e S e q u e n c e W i t h S p l i t ]
. S p l i t () c D i v i d e r s , :
-, .
"",
S p l i t ( ) , f o r e a c h .
, I s A l l D i g i t s ()
, .
nSum.
( ).
:
,
1, 2 , , 3, 4
= 1
= 2
= 3
= 4
= 10
<Enter>
...
, , (
) . ""
, 10. , ,
, . not
"" .
.
. : , .
,
;
.
S t r i n g
. , Trim
Pad(), PadRight(), PadLeft(), Substring() Concat().
Trim() Pad()
T r i m () . !
. )
.
212
III.
,
Pad, ,
. ,
,
.
A l i g n O u t p u t .
// AlignOutput -
//
namespace A l i g n O u t p u t
(
using S y s t e m ;
class P r o g r a m
{
public s t a t i c v o i d M a i n ( s t r i n g []
args)
{
string[]
names
{"Christa
",
"
Sarah",
"Jonathan",
"Sam",
" Schmekowitz " } ;
// ,
Console.WriteLine(" " ) ;
foreach(string
in n a m e s )
{
Console.WriteLine(" '{}' ", s);
}
C o n s o l e . W r i t e L i n e () ;
II
//
string [] s A l i g n e d N a m e s = T r i m A n d P a d ( n a m e s ) ;
//
C o n s o l e . W r i t e L i n e ( "
" " ) ;
foreach(string s in s A l i g n e d N a m e s )
"
}
II
Console.WriteLine(" <Enter> " +
" . . . " ) . ;
Console.Read();
}
9. #
213
// TrimAndPad -
// ,
// ,
//
p u b l i c s t a t i c string[] T r i m A n d P a d ( s t r i n g [] strings)
{
// ,
//
string[] stringsToAlign = new String[strings.Length];
//
//
f o r ( i n t i = 0;
i < stringsToAlign.Length;
i++)
{
stringsToAlign[i]
= strings[i].Trim();
}
//
i n t n M a x L e n g t h = 0;
foreach(string
in
stringsToAlign)
{
if
(s.Length
> nMaxLength)
{
nMaxLength
s.Length;
}
}
//
f o r ( i n t i = 0;
i < stringsToAlign.Length;
i++)
{
stringsToAlign[i] =
stringsToAlign[i].PadRight(nMaxLength + 1 ) ;
}
//
return stringsToAlign;
A l i g n O u t p u t ,
( ,
). M a i n ()
, . TrimAndPad
:
'Christa
'
'
S a r a h '
'Jonathan'
'Sam'
1
' Schmekowitz
214
III.
e
'Christa
'
'Sarah
'
ta 'Jonathan
'
'Sam
'
'Schmekowitz '
< E n t e r > . . .
T r i m A n d P a d ()
strings. , ,
, .
TrimAndPad () , T r i m ()
, .
, .
, P a d R i g h t ( ) ,
, .
P a d R i g h t (10) ,
10 .
PadRight (10) 4 .
T r i m A n d P a d () .
lain () , . .
.
( R e p l a c e ():
string s = " D a n g e r N o S m o k i n g " ;
.Replace (s, ' ' , ' ! ' )
"Danger!NoSmoking".
( )
( ) ,
.
,
.
, , R e m o v e S p e c i a l C h a r s ( ) ,
.
R e m o v e W i t h S p a c e
R e m o v e S p e c i a l C h a r s ()
(, ).
// RemoveWhiteSpace
// RemoveSpecialChars () ,
//
// .
/ /
//
namespace R e m o v e W h i t e S p a c e
' 9. #
215
*
using
System;
public
class
Program
{
public
args)
{
//
c h a r t ] c W h i t e S p a c e = {' ', ' \ n ' , ' \ t ' } ;
// ,
//
s t r i n g s = " t h i s is a \ n s t r i n g " ;
Console.WriteLine(":" + s ) ;
//
Console.WriteLine(" : " +
RemoveSpecialChars(s, cWhiteSpace))
//
Console.WriteLine(" <Enter> " +
" . . . ") ;
Console.Read();
}
// R e m o v e S p e c i a l C h a r s -
//
public static string RemoveSpecialChars(string slnput,
c h a r t ] cTargets)
{
// sOutput
string sOutput = slnput;
//
for(;;)
{
// ;
//
int n O f f s e t = s O u t p u t . I n d e x O f A n y ( c T a r g e t s ) ;
if ( n O f f s e t
-1)
{
break;
}
//
//
string sBefore = sOutput.Substring(0, n O f f s e t ) ;
string sAfter
= sOutput.Substring(nOffset + 1 ) ;
// ,
//
sOutput = String.Concat(sBefore, s A f t e r ) ;
216
III.
return
sOutput;
R e m o v e S p e c i a l
Chars ( ) . , ,
, c T a r g e t s .
, , " a b , c d , " ,
' ,
R e m o v e S p e c i a l C h a r s () ,
, . 1dexOf Any () ( -1 ,
).
I n d e x O f A n y () 2 (
1
0,
0,
..
"ab".
Substring (3) 3
, .. " c d , e " (+1 ).
Concat () , " a b e d , ".
. 4, " a b e d e " .
, -1.
,
, R e m o v e S p e c i a l C h a r s ()
:
|: this is
Wring
:thisisastring
<Enter> . . .
SplitQ
R e m o v e W h i t e S p a c e
C o n c a t ( ) I n d e x O f ( ) ;
.
,
S p l i t .
Remove-
W h i t e S p a c e W i t h S p l i t . R e m o v e S p e c i a l C h a r s ()
.
// RemoveSpecialChars -
//
r a b l i c static s t r i n g R e m o v e S p e c i a l C h a r s ( s t r i n g s l n p u t ,
char[] cTargets)
toa ft #
217
//
//
string[] sSubStrings = s l n p u t . S p l i t ( c T a r g e t s ) ;
// sOutput
string sOutput = "";
//
foreach(string substring in
sSubStrings)
{
sOutput
String.Concat(sOutput,
substring);
}
return
sOutput;
}
S p l i t ()
-,
, ,
.
f o r e a c h .
.
S t r i n g Format ]
, . Forma
, ,
. , , :
string myString =
S t r i n g . F o r m a t ( " { 0 } {l} { 2 } " ,
2,
3,
2*3);
F o r m a t () ( ).
, - , ,
. {0} ( 2), {1
(3) .
myStr
:
2
, F o r m a t ()
.
, . 9.1.
218
III.
. 9.1
,
#.
,
O u t p u t F o r m a t C o n t r o l s ,
, ,
.
//OutputFormatControls - ,
// .
// ,
//
namespace O u t p u t F o r m a t C o n t r o l s
{
using S y s t e m ;
public c l a s s P r o g r a m
{
public s t a t i c v o i d M a i n ( s t r i n g []
args)
{
// ,
// ,
//
for(;;)
{
/ / , ]
//
C o n s o l e . W r i t e L i n e ( " ")
string sNumber = C o n s o l e . R e a d L i n e ;
if ( s N u m b e r . L e n g t h == 0)
{
break;
}
double dNumber = Double.Parse(sNumber);
// ,
//
Console.WriteLine(" " +
", " + ,
"");
chart] separator = {' ' } ;
string sFormatString = Console.ReadLine();
string[] sFormats = sFormatString.Split(separator) ;
//
f o r e a c h ( s t r i n g s in s F o r m a t s )
{
if
(s.Length
!=
0)
{
// !
//
s t r i n g s F o r m a t C o m m a n d = " {0 : " + s + " } " ;
//
//
Console.Write(
" { } ", sFormatCommand);
try
{
Console.WriteLine(sFormatCommand,
dNumber);
}
catch(Exception)
{
Console.WriteLine("<
>");
220
Console.WriteLine();
//
Console.WriteLine(" <Enter> " +
" . . . " ) ;
Console.Read();
III.
O u t p u t F o r m a t C o n t r o l s
d N u m b e r ,
. ,
. ,
( !).
,
. { 0 } s F o r m a t
Command. ,
N4,
{0: N4}.
:
Console.WriteLine ( s F o r m a t C o m m a n d , d N u m b e r ) ;
N4
tasole. W r i t e L i n e (" { :N4 }" , d N u m b e r ) ;
( ; ):
.
1234 5 . 6 7 8 9
,
F1 N0 0000000 .00000
{0:} $ 1 2 , 3 4 5 . 6 8
{0:} 1 . 2 3 4 5 6 8 + 0 0 4
{ 0 : F 1 } 1 2 3 4 5 . 7
{0:N0} 1 2 , 3 4 6
{ 0 : 0 0 0 0 0 0 0 . 0 0 0 0 0 } 0 0 1 2 3 4 5 . 6 7 8 9 0
.12345
,
.%
{ 0 : 0 0 . 0 % } 1 2 . 3 %
<Enter>
...
12345.6789, N0
( N) ( 0),
, 346 ( , ).
, 0.12345,
0 0 . 0 %
[ 9. #
221
IV
- ,
. ,
- + +
. Java,
, .
-! ?
? ?
#,
- .
-
;
, , ,
.
10
> -
>
> -
-
.
,
, , ,
. , , ,
.
, ,
.
( , ).
, .
.
,
; , .
,
.
.
.
.
. ,
. -)
, ,
.
.
, -
, ,
.
.
. ,
, "" , , .
, !
. , , , , -
" , , " ..
,
" ".
. ""
. ,
, , ,
. . ""
, . , .
!
. ,
.
"-"
- ,
, : , ,
. ,
.
( ) . , ,
, ( ).
, ..
.
#.
226
IV.
.
-, : " ?"
: " , ...". : "
?" - : ", , ...". (
, , ,
.)
,
, . ,
, .
-
.
, , ,
.
. .
, , .
, , " " " " . ,
, , - , .
. , .
, , , , .. ,
.
,
. , ", ...", ,
(, ). .
,
.
.
, -
? . ,
. - ?
,
- .
,
. ,
. ,
. , ?
!
, "]
". .
: " .
. 3000 .
. ,
". !
.
. ,
.
,
, of
.
. -
. ,
. ,
.
. !
. -
. ,
.
. "
, -
,
(
). , ,
, - ( , - HI
).
(,
).
. i
,
. , ,
.
. (
. , ,
, .
. ...
,
228
IV.
, ""
. ,
, ...
,
.
,
. ,
, ,
.
.
.
,
.
. , -
, .
,
. ,
, .
,
, , .
, , ,
. -
,
, .
, .
, ,
.
- .
, # - ? ,
. # - -
10.
229
, .
(, Word M a i n O ) .
, #
- .
# !
.
. # .
# ,
.
11, "".
. # , i
.
. , , Vi
h i c l e . 12, "".
. ,
. , Rocket
V e h i c l e , S t a r t
, , V e h i c l e . ,
,
...|
13,
"", 1
" ".
230
IV.
11
>
>
>
.
, , - ,
( )
.
,
,
. # .
p u b l i c .
BankAccount, - b a l a n c e
. p u b l i c ,
, .
, ,
,
. ,
, .
. ,
, , ,
b a l a n c e
. , , , ,
. , ,
,
...
B a n k A c c o u n t
p u b l i c , - nAccountNumb e r d B a l a n c e p r i v a t e .
, .
// B a n k A c c o u n t -
/ / d o u b l e (
// private,
// )
// :
// , ,
// Main() private-
// BankAccount.
using System;
namespace
BankAccount
{
public
class
Program
{
public
args)
{
Console.WriteLine(" " +
" . " ) ;
//
Console.WriteLine(" " +
" " ) ;
BankAccount ba = new B a n k A c c o u n t ( ) ;
ba.InitBankAccount();
// Deposit()
// ; Deposit()
// -
ba.Deposit(10);
// -
//
C o n s o l e . W r i t e L i n e ( " " +
" " ) ;
ba.dBalance += 10;
//
Console.WriteLine(" <Enter> " +
" . . . ") ;
Console.Read();
// BankAccount - ,
//
public class BankAccount
232
IV.
private s t a t i c i n t n N e x t A c c o u n t N u m b e r =
private i n t n A c c o u n t N u m b e r ;
10 0 0 ;
// d o u b l e
private d o u b l e d B a l a n c e ;
// Init -
//
//
public v o i d
InitBankAccount()
{
nAccountNumber =
dBalance = 0.0;
++nNextAccountNumber;
// G e t B a l a n c e -
public d o u b l e G e t B a l a n c e ( )
{
return d B a l a n c e ;
//
public int G e t A c c o u n t N u m b e r ( )
{
return n A c c o u n t N u m b e r ;
}
public v o i d S e t A c c o u n t N u m b e r ( i n t
nAccountNumber)
{
this.nAccountNumber
= nAccountNumber;
// Deposit -
public v o i d D e p o s i t ( d o u b l e d A m o u n t )
{
if
(dAmount
>
0.0)
{
dBalance
+= d A m o u n t ;
// Withdraw - ,
// ;
//
public d o u b l e W i t h d r a w ( d o u b l e
dWithdrawal)
{
if
(dBalance <= d W i t h d r a w a l )
{
dWithdrawal
= dBalance;
dBalance -= d W i t h d r a w a l ;
return d W i t h d r a w a l ;
11.
233
1000;
// d o u b l e
private d o u b l e d B a l a n c e ;
// Init -
//
//
public v o i d I n i t B a n k A c c o u n t ( )
nAccountNumber =
dBalance = 0.0;
++nNextAccountNumber;
}
// G e t B a l a n c e -
public d o u b l e G e t B a l a n c e ( )
return d B a l a n c e ;
//
public
int G e t A c c o u n t N u m b e r ( )
return n A c c o u n t N u m b e r ;
public v o i d S e t A c c o u n t N u m b e r ( i n t
nAccountNumber)
this.nAccountNumber = nAccountNumber;
// D e p o s i t -
public v o i d D e p o s i t ( d o u b l e d A m o u n t )
{
if
(dAmount
>
0.0)
{
dBalance
+= d A m o u n t ;
// W i t h d r a w - ,
// ;
//
public d o u b l e W i t h d r a w ( d o u b l e d W i t h d r a w a l )
if
( d B a l a n c e <= d W i t h d r a w a l )
dWithdrawal
= dBalance;
dBalance -= d W i t h d r a w a l ;
return d W i t h d r a w a l ;
233
// G e t S t r i n g -
//-
public string GetString()
{
string
return
s;
String.Format("#{0} = {l:C}",
GetAccountNumber(),
GetBalance());
d B a l a n c e -= d W i t h d r a w a l ,
d B a l a n c e = d B a l a n c e - d W i t h d r a w a l . C#
.
p u b l i c ,
.
B a n k A c c o u n t I n i t B a n k A c c o u n t () -1
, D e p o s i t () With-I
d r a w ( ) . D e p o s i t ( ) W i t h d r a w ( )
-1
" -1
" " , ". !
, - d B a l a n c e (
" , !
"), . -1
.!
, -1
. , -1
, .
, , -1
, !
D o u b l e B a n k A c c o u n t . B a n k A c c o u n t . d B a l a n c e :
'DoubleBankAccount.BankAccount.dBalance'
due to its protection level.
is
inaccessible
,
" private", . b a . d B a l a n c e += 10; I
d B a l a n c e ]
p r i v a t e M a i n ( ) ,
B a n k A c c o u n t . b a . D e p o s i t (10)
B a n k A c c o u n t . D e p o s i t () p u b l i c ,
M a i n ().
p r i v a t e ,
,
p r i v a t e .
. ,
.
234
IV.
, .
,
.
# .
, p u b l i c , .
, p r i v a t e , .
, p r o t e c t e d ,
.
, i n t e r n a l ,
.
# ,
. - . D L L .
.
, i n t e r n a l p r o t e c t e d ,
.
p r i v a t e . . ,
, p r o
jected .
I p u b l i c
.
- p u b l i c , ,
.
Deposit () W i t h d r a w () ? ,
.
, .
B a n k A c c o u n t ,
, . W i t h
draw () ,
, W i t h d r a w n ,
. ,
.
-
. , B a n k A c c o u n t ,
, . ,
.
' 11.
235
- ""
. , B a n k A c c o u n t
. -,
W i t h d r a w ( ) .
, ,
, ,
!
?
, ,
, .
. p r i v a t e ,
p r o t e c t e d , i n t e r n a l , internal
p r o t e c t e d p u b l i c .
B a n k A c c o u n t ,
. , G e t S t r i n g ( ) ,
C o n s o l e . W r i t e L i n e ( ) . ,
B a n k A c c o u n t ,
. , " ",
, .
, G e t B a l a n c e () ]
G e t A c c o u n t N u m b e r () SetAc
c o u n t N u m b e r ( ) . - , !
d B a l a n c e p r i v a t e , GetBal
() ? .
G e t B a l a n c e d B a l a n c e
.
. ,
,
, , .
G e t B a l a n c e ()
. G e t B a l a n c e ()
, , -
, .
. ,
, , .
, G e t B a l a n c e () !
B a n k A c c o u n t , !,!
- , -1
( !
).
236
IV.
D o u b l e B a n k A c c o u n t
B a n k A c c o u n t .
// DoubleBankAccount
//
//
double
private,
using S y s t e m ;
namespace
DoubleBankAccount
{
public
class
Program
{
public
args)
{
//
Console.WriteLine("
"
"
" ) ;
BankAccount ba = new B a n k A c c o u n t ( ) ;
ba.InitBankAccount();
//
double dDeposit = 1 2 3 . 4 5 4 ;
Console.WriteLine(" {0:C}",
ba.Deposit(dDeposit);
//
dDeposit);
Console.WriteLine("
{ } " ,
ba.GetString());
//
Console.WriteLine(" <Enter>
"
"
.. . " ) ;
Console.Read();
}
}
//
BankAccount
//
public c l a s s B a n k A c c o u n t
{
private static int n N e x t A c c o u n t N u m b e r =
private int n A c c o u n t N u m b e r ;
11.
1000;
237
//
private double dBalance;
double
// I n i t -
//
//
public
void
InitBankAccount()
{
nAccountNumber =
d B a l a n c e = 0.0;
++nNextAccountNumber;
}
// GetBalance
public double
-
GetBalance()
{
return dBalance;,
}
// AccountNumber
p u b l i c int G e t A c c o u n t N u m b e r ( )
{
return
nAccountNumber;
}
public
void
SetAccountNumber(int
nAccountNumber)
{
this.nAccountNumber = nAccountNumber;
}
// Deposit -
public void Deposit(double dAmount)
{
if
(dAmount
>
0.0)
{
dBalance
+= dAmount;
}
}
// W i t h d r a w - ,
// ;
//
public double Withdraw(double dWithdrawal)
{
if
(dBalance
<= d W i t h d r a w a l )
{
dWithdrawal
= dBalance;
}
d B a l a n c e -= d W i t h d r a w a l ;
return dWithdrawal;
}
// G e t S t r i n g -
//
public string GetString()
238
IV.
{
string s
String.Format("#{0} = {l:C}",
GetAccountNumber(),
GetBalance());
return s;
}
}
M a i n ()
123.454 ..
. M a i n ()
.
:
$123 .45
= #1001 = $ 1 2 3 .45
$0.00
= #1001 = $123.46
< E n t e r > . . .
.
,
... , .
, , , 123.454 123.45.
,
.
d e c i m a l D e c i m a l . R o u n d ( ) ,
D e c i m a l B a n k A c c o u n t .
// DecimalBankAccount -
// decimal
//
using S y s t e m ;
namespace
DecimalBankAccount
{
public
class
Program
{
public
args)
{
//
Console.WriteLine(" " +
"
");
BankAccount ba = n e w B a n k A c c o u n t ( ) ;
ba.InitBankAccount();
//
double dDeposit = 123.454;
Console.WriteLine(" {0:C}",
dDeposit);
ba.Deposit(dDeposit);
//
[ 11.
239
Console.WriteLine(" = { o } "
ba.GetString());
//
double dAddition = 0.002;
Console.WriteLine(" {0:C}",
dAddition);
ba.Deposit(dAddition);
//
Console.WriteLine(" =
ba . G e t S t r i n g ()'');
{ } " ,
//
Console.WriteLine(" <Enter> " +
" . . . " ) ;
Console.Read();
}
// BankAccount - ,
//
public class BankAccount
{
p r i v a t e s t a t i c int n N e x t A c c o u n t N u m b e r = 1 0 0 0 ;
p r i v a t e int n A c c o u n t N u m b e r ;
//
private decimal mBalance;
//
//
//
decimal
Init -
public
void
InitBankAccount()
{
nAccountNumber =
m B a l a n c e = 0;
++nNextAccountNumber;
}
// G e t B a l a n c e
public double
return
-
GetBalance()
(double)mBalance;
// AccountNumber
p u b l i c int G e t A c c o u n t N u m b e r ( )
{
return
nAccountNumber;
}
public
void
SetAccountNumber(int
this.nAccountNumber
nAccountNumber)
nAccountNumber;
}
240
IV. -
// D e p o s i t -
public v o i d D e p o s i t ( d o u b l e d A m o u n t )
{
if
(dAmount
>
0.0)
//
//
decimal mTemp = (decimal)dAmount;
mTemp = Decimal.Round(mTemp, 2 ) ;
mBalance
+= m T e m p ;
}
// Withdraw - ,
// ;
//
public d e c i m a l W i t h d r a w ( d e c i m a l d W i t h d r a w a l )
{
if
( m B a l a n c e <= d W i t h d r a w a l )
{
dWithdrawal
= mBalance;
}
mBalance -= d W i t h d r a w a l ;
return d W i t h d r a w a l ;
// G e t S t r i n g -
//
public s t r i n g G e t S t r i n g ( )
d e c i m a l ,
, d o u
ble. D e p o s i t () D e c i m a l . R o u n d ()
. ,
:
$123.45
= #1001
=
$123 .45
$0.00
= # 1 0 0 1 = $ 1 2 3 . 4 5
<Enter> . . .
, Bank
c o u n t d e c i m a l , , , .
. . , BankAcoi
,
( ,
: B a l a n c e () d o u b l e ) .
: , B a n k A c c o u n t
.
,
, M a i n ( ) , |
, |
.
,
.
, *
.
G e t X O S e t X ( ) , BankAccou
(access functions).
,
. 1 n A c c o u n t N u m b e r ,
:
SetAccountNumber(GetAccountNumber() + 1 ) ;
# ,
.
A c c o u n t N u m b e r :
p u b l i c int A c c o u n t N u m b e r
//
getjreturn nAccountNumber;}
set{nAccountNumber = value;}
//
//
// v a l u e -
g e t , a s e t .
B a l a n c e
, g e t :
public
double
Balance
{
get
{
242
IV.
return
(double)mBalance;
:
BankAccount ba = n e w B a n k A c c o u n t () ;
// A c c o u n t N u m b e r
ba. AccountNumber = 1 0 0 1 ;
//
Console. W r i t e L i n e ("#{ 0} = { l : C } " , ba . A c c o u n t N u m b e r ,
ba.Balance) ;
A c c o u n t N u m b e r B a l a n c e -
, .
(, m B a l a n c e p r i v a t e ) .
, B a l a n c e
.
.
.
, : B a l a n c e ,
Balance().
. #
,
, .
, #. #
,
- .
- ,
:
public class B a n k A c c o u n t
private s t a t i c int n N e x t A c c o u n t N u m b e r =
public s t a t i c i n t N e x t A c c o u n t N u m b e r
1000;
get {return n N e x t A c c o u n t N u m b e r ; }
N e x t A c c o u n t N u m b e r ,
.
// N e x t A c c o u n t N u m b e r
int nValue = B a n k A c c o u n t . N e x t A c c o u n t N u m b e r ;
get
, . :
11.
243
p u b l i c static int A c c o u n t N u m b e r
{
// ,
//
get{return ++nNextAccountNumber;}
}
, .
, oco
, - .
.
, ,
. ,
,
, ,
.
. , ,
, ,
. - ]
"", .
#
,
MyObject
mo
new M y O b j e c t O ;
,
.
. D o g , Scooter-I
Dog.
#
. ,
:
public
args)
{
int n ;
d o u b l e d;
double dCalculatedValue
= n
d;
}
244
IV.
# , n, d
.
:
Use of u n a s s i g n e d l o c a l v a r i a b l e 'n'
Use of u n a s s i g n e d l o c a l v a r i a b l e 'd'
C# ,
- 0 , f a l s e
n u l l .
:
using System;
namespace T e s t
(
public c l a s s P r o g r a m
{
public s t a t i c v o i d M a i n ( s t r i n g []
args)
{
//
MyObject l o c a l O b j e c t = n e w M y O b j e c t O ;
Console.WriteLine("localObject.n = { o } " , localObject.n);
if ( l o c a l O b j e c t . n e x t O b j e c t == n u l l )
{
Console.WriteLine("localObject.nextObject
null");
}
//
Console.WriteLine(" <Enter> " +
" .. . " ) ;
Console.Read();
public class M y O b j e c t
{
internal int n;
internal M y O b j e c t n e x t O b j e c t ;
M y O b j e c t ,
int n e x t O b j e c t ,
. M a i n () M y O b j e c t
.
localObject . =
localObject . n e x t O b j e c t = n u l l
<Enter> . . .
#
. , - l o c a l O b j e c t . l o c a l O b j e c t . n e x t O b j ect
- , "".
[, , .
"" .
11.
245
# , :
. (,
) . Bank
c o u n t , ,
public
class
BankAccount
{
int n A c c o u n t N u m b e r ;
double dBalance;
I I . . .
}
,
.
B a n k A c c o u n t I n i t B a n k A c c o u n t ( ) ,
.
, .
I n i t B a n k A c c o u n t ( ) ,
,
InitBankA
c o u n t ( ) , .
,
# . # ,
, ,
B a n k A c c o u n t :
public void Main(string[]
args)
{
BankAccount ba = new B a n k A c c o u n t ( ) ;
}
public
class
BankAccount
{
// 1000
//
static int n N e x t A c c o u n t N u m b e r = 1 0 0 0 ;
//
int n A c c o u n t N u m b e r ;
double dBalance;
/ / B a n k A c c o u n t -
public BankAccount() // ,
// ,
//
{
nAccountNumber =
d B a l a n c e = 0.0;
++nNextAccountNumber;
}
I I . . .
}
246
IV.
B a n k A c c o u n t ,
InitBankAccount (). :
, ;
, v o i d ;
M a i n ()
.
D e m o n s t r a t e D e f a u l t C o n s t r u e t o r .
// DemonstrateDef a u l t C o n s t r u c t o r -
// ;
//
using System;
namespace
DemonstrateDef aultConstructor
{
Console.WriteLine(" M y O b j e c t " ) ;
Console.WriteLine("( - " +
" " +
" )11) ;
Console.WriteLine(" " +
" - : ") ;
dynamicObj = n e w M y O t h e r O b j e c t ;
Console.WriteLine(" M y O b j e c t " ) ;
// MyOtherObject -
// , -
public class M y O t h e r O b j e c t
public M y O t h e r O b j e c t ()
{
.
Console.WriteLine("
11,
MyOtherObject");
247
public
class
Program
{
public
args)
{
Console.WriteLine(" M a i n ( ) " ) ;
Console.WriteLine("
"MyObject M a i n ( ) : " ) ;
MyObject localObject = new M y O b j e c t O ;
"
//
Console.WriteLine(" <Enter> " +
" . . . " ) ;
Console.Read();
}
}
:
Main()
MyObject M a i n O :
MyOtherObject
MyObject
( -
)
-:
MyOtherObject
MyObject
<Enter> ...
.
1. , M a i n ()
M y O b j e c t .
2. M a i n () l o c a l O b j e c t M y O b j e c t .
3. M y O b j e c t s t a t i c O b j MyOtherObject.
- MyObject
# s t a t i c O b j
, M y O b j e c t .
4. M y O b j e c t .
, , pal
M y O b j e c t .
5.
M y O b j e c t
MyOtherObject
n e w ,
M y O t h e r O b j e c t .
6. M y O b j e c t , ,
M a i n ().
7. .
248
IV.
,
.
1.
DefaultConstructor.
2. ,
Console.WriteLine
MyOtherObject.
, . . 11.1
,
.
Puc. 11.1.
3.
<F11>).
D e b u g ^ S t e p Into
, ,
M a i n ()
.
4. <F11>
localObject ( ).
M y O b j e c t .
, . 11.2. , -
toa 77.
249
l o c a l O b j e c t n u l l .
Locals.
250
IV.
11.
251
, !
, - . #
- , .
, B a n k A c c o u n t :
public
class
BankAccount
{
// 1000
//
s t a t i c i n t nNextAccountNumber = 1000;
/ /
i n t nAccountNumber = ++nNextAccountNumber;
double dBalance = 0.0;
I I . . . .
.
.
}
. n A c c o u n t N u m b e r , dBalance
,
.
. ,
0 . 0 d B a l a n c e .
d B a l a n c e . ,
, B a n k A c c o u n t .
.
, - n N e x t A c c o u n t N u m b e r
B a n k A c c o u n t (
), ..
, , .
, ,
. .
. ()
, ,
.
D e m o n s t r a t e D e f a u l t C o n s t r u c
t o r n e w M y O t h e r O b j e c t () M y O b j e c t
,
, W r i t e L i n e ( ) .
public
class
MyObject
/ /
s t a t i c M y O t h e r O b j e c t s t a t i c O b j = new M y O t h e r O b j e c t ( ) ;
/ /
M y O t h e r O b j e c t d y n a m i c O b j = new M y O t h e r O b j e c t ;
public
MyObject()
252
IV.
D e m o n s t r a t e D e f a u l t C o n s t r u c t o r .
M a i n ( )
M y O b j e c t M a i n ( ) :
M y O t h e r O b j e c t
M y O t h e r O b j e c t
MyObject
( )
M y O b j e c t
<Enter> . . .
D e m o n s t r a t e C o n s t r u c t o r W i t h l n i t i a l i z e r .
, .
, (
7, " ").
, B a n k A c c o u n t , ,
.
BankAccountWithMultipleConstructors
Using System;
H o m e s p B a n k A c c o u n t W i t h M u l t i p l e C o n s t r u c t o r s
Using S y s t e m ;
public class Program
{
public
static
void
Main ( s t r i n g []
args)
{
//
//
BankAccount b a l = n e w B a n k A c c o u n t ( ) ;
Console.WriteLine(bal.GetString());
253
B a n k A c c o u n t 2 = new B a n k A c c o u n t ( 1 0 0 ) ;
C o n s o l e . W r i t e L i n e ( b a 2 . G e t S t r i n g (.)).;
B a n k A c c o u n t b a 3 = new B a n k A c c o u n t ( 1 2 3 4 ,
Console.WriteLine(.GetString());
200);
/ /
Console.WriteLine(" <Enter> " +
" . . . " ) ;
Console.Read();
// BankAccount -
p u b l i c c l a s s BankAccount
//
1000;
//
static
int
//
//
1000;
public
nNextAccountNumber
/ /
i n t nAccountNumber;
double dBalance;
BankAccount()
//
{
nAccountNumber = ++nNextAccountNumber;
dBalance = 0.0;
}'
public
BankAccount(double
dlnitialBalance)
{
//
nAccountNumber = ++nNextAccountNumber;
//
//
//
if
,
(
)
( d l n i t i a l B a l a n c e < 0)
{
dlnitialBalance
0;
}
dBalance
dlnitialBalance;
}
public
BankAccount(int nlnitialAccountNumber,
double
dlnitialBalance)
{
//
if
( n l n i t i a l A c c o u n t N u m b e r <= 0)
{
254
IV.
nlnitialAccountNumber =
nAccountNumber
++nNextAccountNumber;
nlnitialAccountNumber;
//
// )
if ( d l n i t i a l B a l a n c e < 0)
{
dlnitialBalance
0;
}
dBalance
dlnitialBalance;
}
public
string
G e t S t r i n g ()
{
return
String.Format("#{0} = {l:N}",
nAccountNumber,
dBalance);
,
, #
.
.
B a n k A c c o u n t W i t ^ h M u l t i pleConstructors :
;
( );
.
Main ()
ipex .
:
#1001 = 0 . 0 0
#1002 = 1 0 0 . 00
# 1 2 3 4= 2 0 0 0 0
<Enter> . . .
, .
,
. , M a i n ( ) , b a l , , B a n k A c c o u n t ( ) , ( ,
# ). , -
11.
255
// BankAccount -
public c l a s s
BankAccount
(
/ / 1 0 0 0 ;
//
static i n t nNextAccountNumber = 1000;
/ /
int n A c c o u n t N u m b e r ;
double d B a l a n c e ;
//
// ,
public
BankAccount()
//
{
Init(++nNextAccountNumber,
public
BankAccount(double
0.0);
dlnitialBalance)
{
Init(++nNextAccountNumber,
dlnitialBalance);
//
//
public B a n k A c c o u n t ( i n t
nlnitialAccountNumber,
double
dlnitialBalance)
{
//
//
//
//
In
,
nlnitialAccountNumber
()
()
1000
it(nlnitialAccountNumber,
dlnitialBalance);
private
void
I n i t ( i n t
double
nlnitialAccountNumber,
dlnitialBalance)
{
nAccountNumber
//
if
nlnitialAccountNumber;
(dlnitialBalance <
0)
{
dlnitialBalance
0;
}
dBalance
dlnitialBalance;
}
public
string
return
GetStringO
String.Format("#{0}
=
{l:N}",
nAccountNumber,
11.
dBalance);
257
I n i t ()
, .
" " !
, ,
!
, .
t h i s
:
// B a n k A c c o u n t C o n t r u c t o r s A n d T h i s
//
using
System;
namespace
BankAccountContructorsAndFunction
{
using
System;
public
class
Program
{
public
s t a t i c
void
Main(string[]
args)
//
//
BankAccount bal
= new B a n k A c c o u n t ( ) ;
Console.WriteLine(bal.GetString());
BankAccount ba2
= new B a n k A c c o u n t ( 1 0 0 ) ;
Console.WriteLine(ba2.GetString());
BankAccount ba3
= new B a n k A c c o u n t ( 1 2 3 4 ,
Console.WriteLine(ba3.GetString());
//
Console.WriteLine(" <Enter>
"
200);
"
+
..").;
Console.Read();
}
// BankAccount
-
public
class
BankAccount
{
// 1000;
//
s t a t i c i n t nNextAccountNumber = 1000;
//
int
nAccountNumber;
double
dBalance;
//
//
//
258
IV. -
public
BankAccount()
this(0,
0)
{}
public
BankAccount(double
dlnitialBalance)
:this(0,
dlnitialBalance)
{}
//
//
public B a n k A c c o u n t ( i n t
nlnitialAccountNumber,
double
dlnitialBalance)
{
//
//
//
if
,
( n l n i t i a l A c c o u n t N u m b e r <= 0)
{
nlnitialAccountNumber
++nNextAccountNumber;
}
nAccountNumber
nlnitialAccountNumber;
//
if
(dlnitialBalance
<
0)
{
dlnitialBalance
0;
}
dBalance
public
string
dlnitialBalance;
GetString()
{
return
String.Format("#{0}
=
{l:N}",
nAccountNumber,
dBalance);
B a n k A c c o u n t ,
.
,
. I n i t () .
B a n k A c c o u n t ( ) :
BankAccount
bal
new
B a n k A c c o u n t () ;
B a n k A c c o u n t ( )
//
BankAc
count ( i n t , d o u b l e ) , 0 0.0:
public B a n k A c c o u n t ( )
this(0,
0)
{}
,
.
11.
259
,
:
public
BankAccount(double
d)
this(0,
d)
{}
- .
, # .
, .
, B a n k A c c o u n t , i n t e r n a l
( ) ,
, , B a n k A c c o u n t .
// BankAccount
-
public
class
BankAccount
// 1000
//
s t a t i c i n t nNextAccountNumber = 1000;
//
int
nAccountNumber;
double
dBalance;
'
internal BankAccount() //
{
nAccountNumber
dBalance
++nNextAccountNumber;
0;
}
public
string
GetStringO
{
return
260
String.Format("#{0}
=
{l:N}",
nAccountNumber,
dBalance);
IV. -
12
,
" " " "
( ) ,
( ) .
. ( ,
- ) . H u m a n ( ) ,
, , .. H u m a n
, M a m m a l
(),
, A n i m a l ( ) .
.
. , : " ? "
: " , " . , ,
. , ,
, , ,
" " .
-
, , , ,
- ,
, , , -
.
InheritanceExam-
p l e S u b c l a s s B a s e C l a s s .
// I n h e r i t a n c e E x a m p l e
using S y s t e m ;
namespace
InheritanceExample
public
class
BaseClass
{
public
int
public
void
nDataMember;
SomeMethodO
{
Console.WriteLine("SomeMethod()");
}
}
public
class
Subclass
BaseClass
{
public
void
SomeOtherMethod()
{
Console.WriteLine("SomeOtherMethod()");
}
public
class
Program
{
public
s t a t i c
void
Main(string[]
args)
{
//
Console . WriteLine ("
B a s e C l a s s be = new B a s e C l a s s ;
be.nDataMember = 1 ;
be.SomeMethod();
//
Console.WriteLine("
S u b c l a s s sc = new S u b c l a s s ;
s c . n D a t a M e m b e r = 2;
sc.SomeMethod();
sc.SomeOtherMethod();
:"
:");
//
B a s e C l a s s
- S
M e t h o d ( ) . M a i n ()
b e B a s e C l a s s
Subclass
B a s e C l a s s
BaseClass
Subclass
B a s e C l a s s , ,
. M a i n ()
262
, S u b c l a s s -'
IV. -
nDataMember - S o m e M e t h o d ( ) , B a s e C l a s s ,
S o m e O t h e r M e t h o d (.), .
, ( L , ) :
SomeMethod ()
SomeMethod ()
B o m e O t h e r M e t h o d ()
<Enter>
...
, ,
. , , , .. .
(, ) .
( , ) . , , .
, .
' # , S t u d e n t P e r s o n . , P e r s o n
S t u d e n t .
Stu-
dent P e r s o n (
) . + +
- .
, S t u d e n t P e r s o n , . P e r s o n
S t u d e n t
, , ) .
, P e r s o n , S t u d e n t .
, S t u d e n t , a P e r s o n .
. , G r a d u a t e Student S t u d e n t , P e r s o n .
, : G r a d u a t e S t u d e n t S t u d e n t
Student P e r s o n , G r a d u a t e S t u d e n t P e r s o n .
. ,
.
P e r s o n S t u d e n t .
(reuse).
,
.
hm 12.
263
.
, ? ,
, ?
,
, .
.
.
,
. - ,
,
.
. , . ,
, .
,
.
, .
,
, .
.
. # .
-
, .
//
SimpleSavingsAccount
-
SavingsAccount
// BankAccount;
//
( 13)
using
System;
namespace
SimpleSavingsAccount
{
// BankAccount - ,
//
p u b l i c c l a s s BankAccount // t h e base c l a s s
{
// 1000
//
p u b l i c s t a t i c i n t nNextAccountNumber = 1000;
//
public
int
nAccountNumber;
public
decimal
mBalance;
// I n i t -
//
(
264
IV. -
// )
public v o i d
InitBankAccount()
{
I n i t B a n k A c c o u n t (0) ;
}
public
void
InitBankAccount(decimal
mlnitialBalance)
{
nAccountNumber = ++nNextAccountNumber;
mBalance = m l n i t i a l B a l a n c e ;
}
// B a l a n c e
public d e c i m a l B a l a n c e
get { return mBalance;}
}
// D e p o s i t -
public v o i d D e p o s i t ( d e c i m a l mAmount)
if
(mAmount
>
0)
{
mBalance
+=
mAmount;
// Withdraw - ,
// ;
public d e c i m a l
Withdraw(decimal
mWithdrawal)
if
(Balance
<=
mWithdrawal)
{
mWithdrawal
//
//
Balance
Balance;
}
mBalance -= m W i t h d r a w a l ;
return mWithdrawal;
}
// T o S t r i n g public
string
return
ToBankAccountString()
String.Format("{0}
{l:C}",
nAccountNumber,
// S a v i n g s A c c o u n t
//
public
class
SavingsAccount
Balance);
BankAccount
//
{
public d e c i m a l m l n t e r e s t R a t e ;
// I n i t S a v i n g s A c c o u n t -
// 0 100
public
void
InitSavingsAccount(decimal
mlnterestRate)
{
InitSavingsAccount(0,
mlnterestRate);
public
void
InitSavingsAccount(decimal
decimal
m 12.
mlnitial,
mlnterestRate)
265
InitBankAccount(mlnitial);
this.mlnterestRate = mlnterestRate
100;
}
// A c c u m u l a t e l n t e r e s t -
//
public
void
Accumulatelnterest()
{
i
mBalance = Balance +
(decimal) (Balance*mInterestRate);
}
// T o S t r i n g -
public
string
ToSavingsAccountString()
r e t u r n
String.Format("{0}
({l}%)"(
ToBankAccountStringO ,
mlnterestRate *
100);
}
}
public
class
Program
{
public
s t a t i c
void
Main(string[]
args)
{
//
BankAccount ba = new B a n k A c c o u n t ( ) ;
ba.InitBankAccount(10 0);
ba.Deposit(100);
Console.WriteLine("
{}",
ba.ToBankAccountString());
//
SavingsAccount
sa
=
new S a v i n g s A c c o u n t ( ) ;
sa.InitSavingsAccount(100,
12.5M);
sa.Accumulatelnterest();
Console.WriteLine("
{}",
sa.ToSavingsAccountString());
//
Console.WriteLine(" <Enter> " +
" . . . ") ;
Console.Read();
B a n k A c c o u n t ,
. I n i t B a n k A c c o u n t l
,
, ,
1 1 , " " . !
.
B a l a n c e , !
. D e p o s i t , W i t h d r a w
(
T o B a n k A c c o u n t S t r i n g ()
266
IV. - *
S a v i n g s A c c o u n t
BankAccount.
.
M a i n ( )
. B a n k -
ccount, , S a v i n g s A c c o u n t , . :
1001
$2 0 0 . 0 0
1002
$112.50
(12.500%),
< E n t e r > . . .
I n i t S a v i n g s A c c o u n t ()
I n i t B a n k A c c o u n t ( ) , -
. I n i t S a v i n g s A c c o u n t ( )
B a n k A c c o u n t
. .
S a v i n g s A c c o u n t B a n k A c c o u n t , .
.
( I S _ A ) S a v i n g s A c c o u n t
BankAccount
P r o g r a m
S i m p l e S a v i n g s A c c o u n t ,
public class Program
{
// :
// D i r e c t D e p o s i t public s t a t i c v o i d
D i r e c t D e p o s i t (BankAccount ba,
decimal mPay)
{
ba.Deposit(mPay);
}
public
static
void
Main ( s t r i n g []
args)
{
II
BankAccount ba = new B a n k A c c o u n t ( ) ;
ba. I n i t B a n k A c c o u n t ( 1 0 0 ) ;
DirectDeposit(ba,100);
Console.WriteLine("
{0}",
ba.ToBankAccountString());
// -
SavingsAccount
sa
=
new S a v i n g s A c c o u n t ( ) ;
sa.InitSavingsAccount(100,
12.5M);
DirectDeposit(sa,100);
sa. A c c u m u l a t e l n t e r e s t ( ) ;
\m 12.
267
Console.WriteLine("
{0}",
sa.ToSavingsAccountString());
//
Console.WriteLine(" <Enter> " +
" ...");
Console.Read();
}
. , J
D i r e c t D e p o s i t (),i
B a n k A c c o u n t .
.
( !), M a i n ()
D i r e c t D e p o s i t ()
S a v i n g s A c c o u n t
, ,
BankAccount
, ,
. S a v i n g s A c c o u n t BankAt
c o u n t , S a v i n g s A c c o u n t B a n k A c c o u n t
B a n k A c c o u n t .
BankAccount
S a v i n g s A c c o u n t B a n k A c c o u n t
, (
).
// S a v i n g s A c c o u n t -
p u b l i c c l a s s SavingsAccount_ //
// :
{
//
SavingsAccount.
decimal
mlnterestRate;
// I n i t S a v i n g s A c c o u n t -
// 0 100
public
void
InitSavingsAccount(BankAccount
bankAccount,
decimal
mlnterestRate)
this.bankAccount
= bankAccount;
this.mlnterestRate = mlnterestRate
// A c c u m u l a t e l n t e r e s t -
//
public
void
Accumulatelnterest()
100;
bankAccount.mBalance
= bankAccount.Balance
+
(bankAccount.Balance * mlnterestRate);
// D e p o s i t -
public
void Deposit(decimal
//
mAmount)
BankAccount
bankAccount.Deposit(mAmount);
268
IV. -
//Withdraw
/ / ;
'public
double
return
Withdraw (decimal
mWithdrawal)
SavingsAccount_
bankAccount
B a n k A c c o u n t ) . b a n k A c c o u n t
, SavingsAccount_. Savingaccount_ ,
B a n k A c c o u n t (.. -
| S a v i n g s A c c o u n t _ , , ,
| B a n k A c c o u n t ) .
, S a v i n g s A c c o u n t _ B a n k A c c o u n t .
.
:
/ /
BankAccount b a
/ /
new
B a n k A c c o u n t ()
SavingsAccount:
SavingsAccount_ sa
new
S a v i n g s A c c o u n t _ () ;
s a . I n i t S a v i n g s A c c o u n t ( b a , 5) ;
/ /
100
sa.Deposit ( 1 0 0 ) ;
/ /
s a . A c c u m u l a t e l n t e r e s t () ;
, S a v i n g s A c c o u n t _
BankAccount,
BankAccount, . , :
//DirectDeposit
SavingsAccount_
sa
DirectDeposit (sa,
new
S a v i n g s A c c o u n t _ () ;
100);
// . . . . .
}
D i r e c t D e p o s i t ( )
SavingsAccount_
BankAccount. ,
.
12.
269
,
.
.
, " " .
, ,
" " , .
" " ,
.
Z a p o r o z h e t s ,
,
.
, M o t o r ,
M o t o r ,
. , M o t o r G o ( ) .
, . "
, , " "
, " " . ,
, ,
, , ..
, , .
. , ,
, .
-
.
# , .
.
. S o m e F u n c t i o n ( ) S a v i n g s A c c o u n t ,
B a n k A c c o u n t .
:
BankAccount
,SavingsAccount
sa
270
new
SavingsAccount();
IV. -
ba = s a ;
ba =
(BankAccount)sa;
sa = b a ;
sa =
(SavingsAccount)ba;
//
:
//
//
//
//
//
:
//
//
//
S a v i n g s A c c o u n t B a n k A c
count. # .
.
B a n k A c c o u n t S a v i n g s A c c o u n t .
. , " "
, " " . ,
B a n k A c c o u n t S a v i n g s A c c o u n t ,
. ,
,
.
B a n k A c c o u n t
SavingsAc
count . :
public
s t a t i c
void
ProcessAmount(BankAccount
bankAccount)
{
//
bankAccount.Deposit(10000.00);
// S a v i n g s A c c o u n t ,
SavingsAccount
SavingsAccount
=
(SavingsAccount)bankAccount;
SavingsAccount.Accumulatelnterest();
{
public
s t a t i c
void
TestCastO
{
SavingsAccount
sa
=
new S a v i n g s A c c o u n t ( ) ;
ProcessAmount(sa);
BankAccount ba = new B a n k A c c o u n t ( ) ;
ProcessAmount(ba);
}
P r o c e s s A c c o u n t ( )
A c c u m u l a t e l n t e r e s t ( ) .
,
ba
S a v i n g s A c c o u n t
, b a B a n k A c c o u n t . ,
.
P r o c e s s A c c o u n t ( )
S a v i n g s A c c o u n t
T e s t ( ) .
P r o c e s s A c c o u n t ( ) .
B a n k A c c o u n t S a v i n g s A c c o u n t , b a
S a v i n g s A c c o u n t .
12.
271
P r o c e s s A c c o u n t ()
S a v i n g s A c c o u n t . b a
m u l a t e l n t e r e s t ().
(run-time error
,
. ,
, .
.
is as
P r o c e s s A c c o u n t () , ,
S a v i n g s A c c o u n t , ,
. # is as.
is
i s . t r u e ,
. .
i s ,
.
public
s t a t i c
void
ProcessAmount(BankAccount
bankAccount)
{
//
bankAccount.Deposit(10000.00);
//
if
(bankAccount
SavingsAccount...
is
SavingsAccount)
{
//
...
//
SavingsAccount
SavingsAccount
(SavingsAccount)bankAccount;
SavingsAccount.Accumulatelnterest();
}
//
//
//
BankAccount
-
}
public
s t a t i c
void
TestCastO
{
SavingsAccount
sa
new
SavingsAccount();
ProcessAmount(sa);
BankAccount
ba
new
BankAccount();
ProcessAmount(ba);
}
i s , ,
b a n k A c c o u n t S a v i n g s A c c o u n t .
272
IV. -
P r o c e s s A m o u n t ( )
is t r u e ,
, B a n k A c c o u n t , i s
f a l s e , .
.
,
i s
. , .
o b j e c t
:
public
public
class
class
MyBaseClass
{}
MySubClass
:
MyBaseClass
{}
:
public
class
Test
{
public
s t a t i c
void
GenericFunction(MyBaseClass
//
MySubClass
if(msc
//
//
!=
...
.
mc)
msc
mc
as
MyBaseClass;
null)
. .
.
G e n e r i c F u n c t i o n ( )
M y B a s e C l a s s a s .
a s ? , #
o b j e c t . , ,
, o b j e c t . ,
o b j e c t :
class
MyClassl
class
MyClass2
object
{}
{}
o b j e c t :
public
class
Test
{
public
s t a t i c
void
GenericFunction(object
MyClassl
mcl
12.
as
o)
MyClassl;
273
if(mcl
!=
null)
//
//
mcl,
G e n e r i c F u n c t i o n ()
, a s M y C l a s s l
object.
as
a s , i s .
b o o l ,
n u l l ,
, .
, a s n u l l :
SavingsAccount
SavingsAccount
bankAccount
if(SavingsAccount
!=
as
=
SavingsAccount;
null)
{
//
SavingsAccount
}
/ /
//
? , a s
, i s
.
, as - ,
, i n t , l o n g , d o u b l e
i s .
I n h e r i t a n c e E x a m p l e ,
I n i t . . . ()
B a n k A c c o u n t Saving!
s A c c o u n t . , , , .
,
.
,
.
274
IV. -
// I n h e r i t i n g A C o n s t r u c t o r -
/ /
ping
System;
n a m e s I n h e r i t i n g A C o n s t r u c t o r
(
public class Program
{
public
s t a t i c
void
Main(string[]
args)
{
Console.WriteLine("
B a s e C l a s s be = new B a s e C l a s s ( ) ;
Console.WriteLine("\
Subclass
sc = new S u b c l a s s ( ) ;
BaseClass");
Subclass");
//
Console.WriteLine(" <Enter> " +
" ...");
Console.Read();
public
{
class
public
BaseClass
BaseClassO
{
Console.WriteLine(" BaseClass");
{
public
{
class
public
Subclass
BaseClass
S u b c l a s s ()
{
Console.WriteLine("
Subclass");
B a s e C l a s s S u b c l a s s ,
. B a s e C l a s s
B a s e C l a s s . S u b c l a s s
B a s e C l a s s , B a s e C l a s s .
.
BaseClass
BaseClass
Subclass
BaseClass
Subclass
<Enter>
. . .
. ,
, , .
: -
[ 12.
275
, . :
, ,
. B a s e C l a s s
, S u b c l a s s .
...
,
, , ,
. ,
:
using
System;
namespace
Example
public
class
Program
{
public
s t a t i c
void
Main(string[]
args)
Console.WriteLine("
Subclass()");
Subclass
scl
= new S u b c l a s s ( ) ,
Console.WriteLine("\
Subclass(int)");
Subclass sc2
= new S u b c l a s s ( 0 ) ;
//
Console.WriteLine(" <Enter> " +
" ...");
Console.Read();
p u pb ul ibcl i c c l aBs as s e C
B al as se sC( l) a s s
{
Console.WriteLine("
public
BaseClass(int
BaseClass
)");
i)
Console.WriteLine("
BaseClass
( i n t ) " ) ;
}
public
class
public
Subclass
BaseClass
Subclass()
{
Console.WriteLine("
public
Subclass(int
Subclass
)")
Subclass
( i n t ) " ) ;
i)
{
Console.WriteLine("
276
IV. -
S u b c l a s s ()
B a s e C l a s s
( )
S u b c l a s s
( )
Subclass (int)
B a s e C l a s s
( )
S u b c l a s s
(int)
< E n t e r > . . .
.
, # S u b c l a s s ,
B a s e C l a s s . ,
. , #
S u b c l a s s ( i n t ) .
, , B a s e C l a s s ,
, .
b a s e .
,
t h i s (. 11, " " ) .
, , :
// I n v o k e B a s e C o n s t r u c t o r - ,
/ /
// b a s e
using
System;
namespace
InvokeBaseConstructor
{
public
class
BaseClass
p{ u b l i c
BaseClassO
Console.WriteLine("
public
BaseClass(int
class
Subclass
)");
i)
Console.WriteLine("
public
BaseClass
BaseClass({0})",
i ) ;
BaseClass
{
public
Subclass()
{
Console.WriteLine("
12.
Subclass
)");
277
public
Subclass(int
il,
int
i2)
base(il)
{
Console.WriteLine(" Subclass({0},
i2)
{ l } ) " , il,
public
class
Program
{
public
s t a t i c
void
Main(string[]
args)
{
Console.WriteLine("
Subclass()");
Subclass
scl
= new S u b c l a s s ( ) ;
Console.WriteLine("\
Subclass(1,
S u b c l a s s sc2 = new S u b c l a s s ( 1 ,
2);
2 ) " ) ;
//
Console.WriteLine(" <Enter> " +
" ...");
Console.Read();
}
}
}
:
Subclass()
BaseClass
( )
Subclass
( )
S u b c l a s s ( 1 ,
2)
BaseClass(1)
Subclass(1,
2)
< E n t e r > . . .
, !
S u b c l a s s
S u b c l a s s , B a s e C l a s s .
n e w
S u b c l a s s ( 1 , 2 ) . # -
S u b c l a s s ( i n t , i n t ) , base
B a s e C l a s s ( i n t ) . ,
S u b c l a s s ,
.
BankAccount
ConstructorSavingsAccount,
-,
S i m p l e B a n k A c c o u n t .
S a v i n g s A c c o u n t -1
B a n k A c c o u n t . M a i n ()
278
IV. -
// C o n s t r u c t o r S a v i n g s A c c o u n t - S a v i n g s A c c o u n t
/ / , B a n k A c c o u n t ; ,
//
using S y s t e m ;
namespace
ConstructorSavingsAccount
{
// BankAccount -
// ( )
public
class
BankAccount
{
// 1000
//
public s t a t i c i n t nNextAccountNumber = 1000;
//
public i n t nAccountNumber;
public decimal mBalance;
//
public BankAccount():
this(0)
{
public BankAccount(decimal mlnitialBalance)
{
nAccountNumber = ++nNextAccountNumber;
mBalance = m l n i t i a l B a l a n c e ;
//
}
// S a v i n g s A c c o u n t -
//
public c l a s s SavingsAccount
:
BankAccount
{
public decimal mlnterestRate;
// .
// 0 100
public
SavingsAccount(decimal
mlnterestRate)
: t h i s ( m l n t e r e s t R a t e , 0)
public
:
SavingsAccount(decimal
decimal
mlnterestRate,
mlnitial)
base(mlnitial)
{
this.mlnterestRate
II
mlnterestRate
100;
. . .
}
public
class
Program
{
// D i r e c t D e p o s i t public
s t a t i c void
D i r e c t D e p o s i t (BankAccount ba,
decimal
mPay)
{
b a .
Deposit(mPay);
12.
279
public
s t a t i c
void
Main(string[]
args)
{
//
/ /
BankAccount ba = new B a n k A c c o u n t ( 1 0 0 ) ;
DirectDeposit(ba,
100);
Console.WriteLine("
{}",
ba.ToBankAccountString());
// To
SavingsAccount
sa = new S a v i n g s A c c o u n t ( 1 2 . 5 M ) ;
DirectDeposit(sa,
100);
sa.Accumulatelnterest();
Console.WriteLine("
{}",
sa.ToSavingsAccountString() )
/ /
Console.WriteLine(" <Enter> " +
"
...");
Console.Read();
B a n k A c c o u n t : ,
, ,
. ,
t h i s .
SavingsAccount
. S a v i n g s A c c o u n t ,
, S a v i n g s A c c o u n t ,
,
0. , o6mei
B a s e C l a s s
. 12.1).
12.1.
,
I n i t . . . ( ) , .
.
280
IV. - -
# , .
, , (~).
, - B a s e C l a s s ( )
B a s e C l a s s ( ) .
# , .
, ,
. , (
13, " " ) .
, . ,
.
#
# , -
, + + , #
.
n e w , ,
new S u b c l a s s ( ) . ,
.
, .
, " " ,
. ,
.
, #
.
, . "
" ,
.
, .
,
.
# , ~ B a s e C l a s s ( ) , ,
, ,
,
. ,
, ,
, .
,
.
/ 12.
281
#
, , + + , #
. #
, D i s p o s e ( ) ,
, .
282
IV. -
13
> ?
> ?
>
>
>
""
,
SavingsAccount,
- B a n k A c c o u n t .
.
12, " " ,
.
, -
, , .
,
. ,
, -
... , . . .
,
, ,
,
, .
.
7 , " " ,
/
.
(
), :
public
class
MyClass
{
public
//
s t a t i c
public
//
s t a t i c
s t a t i c
void
s t a t i c
AFunction(int)
void
public
AFunctionO
public
//
void
AFunction(double
,
void
Main(string[]
d)
args)
AFunction();
AFunction(1);
AFunction(2.0);
}
C# . |
M a i n () .
,
,
.
,
, , ,
. :
public
class
MyClass
{
public
s t a t i c
public
void
void
AFunctionO;
AMethod();
}
public
class
UrClass
{
public
public
s t a t i c
void AFunctionO;
void AMethod();
}
public
284
class
Program
IV. -
public
s t a t i c
void
Main(string[]
args)
{
UrClass.AFunction();
//
MyClass
//
-
mcObject
MyClass.AMethod()
new
MyClass();
mcObject.AMethod();
, # ,
A F u n c t i o n ( ) A M e t h o d ( )
.
, ,
. .
.
, ,
.
, 1.50.
- , ,
S a v i n g s A c c o u n t B a n k A c c o u n t .
, ,
.50, ,
public
class
BankAccount
{
private
decimal
private
bool
//
//
public
mBalance;
isSavingsAccount;
BankAccount(decimal
bool
mlnitialBalance,
isSavingsAccount)
mBalance = m l n i t i a l B a l a n c e ;
this.isSavingsAccount
=
isSavingsAccount;
)
public
decimal
Withdraw(decimal
mAmount)
{
//
if
(isSavingsAccount)
{
//
...
mBalance
-=
1.5 0
1.50,-
}
//
if
(mAmountToWithdraw > mBalance)
{
mAmountToWithdraw
13.
mBalance;
285
mBalance -= mAmountToWithdraw;
return
mAmountToWithdraw;
class
MyClass
{
public
void
SomeFunction()
{
//
BankAccount
ba
new
BankAccount(0,
t r u e ) ;
.}
, ,
B a n k A c c o u n t .
W i t h d r a w ( ) 1.50.
- W i t h d r a w ( )
B a n k A c c o u n t S a t
i n g s A c o o u n t ,
.
// HidingWithdrawal -
//
using
System;
namespace
HidingWithdrawal
{
//
BankAccount
public
class
BankAccount
{
protected
public
public
mlnitialBalance)
mlnitialBalance;
return
decimal
decimal
if
decimal
{
public
mBalance;
BankAccount(decimal
mBalance
get
decimal
Balance
mBalance;
Withdraw(decimal
mAmountToWithdraw
(mAmountToWithdraw
>
mAmount)
mAmount;
Balance)
{
mAmountToWithdraw
Balance;
}
mBalance -= mAmountToWithdraw;
return
mAmountToWithdraw;
286
IV. -
// S a v i n g s A c c o u n t
//
public
class
SavingsAccount
BankAccount
{
public
//
//
decimal
mlnterestRate;
SavingsAccount -
0 10 0
public
SavingsAccount(decimal
decimal
mlnitialBalance,
mlnterestRate)
base(mlnitialBalance)
{
this.mlnterestRate
mlnterestRate
// A c c u m u l a t e l n t e r e s t -
public
void Accumulatelnterest()
100;
{
mBalance
Balance
(Balance
mlnterestRate);
// W i t h d r a w - ,
/ / ;
public
decimal
Withdraw(decimal
mWithdrawal)
{
//
base.Withdraw(1.5M);
1.50
//
return
base.Withdraw(mWithdrawal);
public
class
Program
{
public
s t a t i c
void
MakeAWithdrawal(BankAccount
ba,
decimal
mAmount)
ba.Withdraw(mAmount);
}
public
s t a t i c
void
Main(string []
args)
{
BankAccount
ba;
SavingsAccount
sa;
//
100,
//
ba = new B a n k A c c o u n t ( 2 0 0 ) ;
ba.Withdraw(100M);
//
s a = ne~w S a v i n g s A c c o u n t ( 2 0 0 ,
12);
sa.Withdraw(lOOM);
( 13.
287
/ /
Console.WriteLine("
BankAccount
{0:}",
ba.Balance);
Console.WriteLine("
SavingsAccount
{0:C}",
sa.Balance);
//
Console.WriteLine(" <Enter>
"
"
...");
Console.Read();
M a i n ()
B a n k A c c o u n t
2 0 0 100. Saving'
s A c c o u n t . M a i n ( )
B a n k A c c o u n t . W i t h d r a w ( ) (
). M a i n ()
s A c c o u n t . W i t h d r a w ()
, Saving
, 1.50.
, S a v i n g s A c c o u n t . W i t h d r a w ()
B a n k A c c o u n t . W i t h d r a w ( ) ,
.
-.
B a n k A c c o u n t . W i t h d r a w ()
,
. ,
.
. ,
B a n k A c c o u n t S a v i n g s A c c o u n t .
, .
.
, . ,
,
CheckingAccount,
C D A c c o u n t , TBil-
l A c c o u n t .
. -
B a n k A c c o u n t . W i t h d r a w ()
, .
. .
. , ,
V e h i c l e . T a k e O f f ( ) , .
- V e h i c l e , A i r p l a n e . ,
T a k e O f f ( ) , V e h i c l e . ,
, .
, # .
288
IV. -
#
H i d i n g W i t h d r a w a l .
, :
1
.. . S a v i n g s A c c o u n t . W i t h d r a w ( d e c i m a l ) '
hides
i n h e r i t e d member
'...BankAccount.Withdraw(decimal)'.
Use
t h e new
keyword
if
h i d i n g was
intended.
C# , , ,
. ?
. ,
,
, .
- , ,
.
#
, .
, .
,
.
n e w ,
, # ,
( ) .
//
new
public
W i t h d r a w ()
decimal
Withdraw(decimal
mWithdrawal)
{
// . . . . . .
}
n e w
.
S a v i n g s A c c o u n t . W i t h d r a w ()
H i d i n g W i t h d r a w a l , . B a n k A c
count . W i t h d r a w ( )
b a s e .
b a s e :
new p u b l i c
decimal
Withdraw (decimal
mWithdrawal)
{
decimal
mAmountWithdrawn
13.
Withdraw(mWithdrawal);
289
mAmountWithdrawn
return
+=
Withdraw(1.5);
mAmountWithdrawn;
}
, :
void
fn()
{
f n () ;
//
}
f ()
f ()
, W i t h d r a w ( ) , , ;
, .
#,
SavingsAccount .Withdraw()
B a n k A c c o u n t . W i t h d r a w ( ) . !
t h i s
:
//
Withdraw
//
public
decimal
new
Withdraw(decimal
this
mWithdrawal)
{
//
//
BankAccount
//
this
BankAccount
ba
decimal
mAmountWithdrawn
mAmountWithdrawn
return
(BankAccount)this;
Withdraw()
+=
BankAccount
ba.Withdraw(mWithdrawal);
ba.Withdraw(1.5);
mAmountWithdrawn;
}
:
ba . W i t h d r a w ()
B a n k A c c o u n t .
, S a v i n g s A c c o u n t )
B a n k A c c o u n t .
.
# , W i t h d r a w ()
,
. # b a s e .
b a s e , !
.
# b a s e
, t h i s , ,
.
//
Withdraw
//
new
public
decimal
Withdraw(decimal
mWithdrawal)
{
//
1.50
base.Withdraw(1.5M);
290
IV. -
//
return base . Withdraw (mWithdrawal) ;
}
b a s e . W i t h d r a w ( )
B a n k A c c o u n t . W i t h d r a w ( ) ;
, , . , .
. ,
.
: ,
B a n k A c c o u n t . W i t h d r a w ()
S a v i n g s A c c o u n t . W i t h d r a w ()
: ?
, ,
H i d i n g W i t h d r a w a l
, ).
// H i d i n g W i t h d r a w a l P o l y m o r p h i c a l l y
// Withdraw ()
//
public c l a s s
Program
(
public
s t a t i c
void
MakeAWithdrawal(BankAccount
ba,
decimal
mAmount)
{
ba.Withdraw(mAmount);
}
public
s t a t i c
void
Main(string[]
args)
{
BankAccount
ba;
SavingsAccount
sa;
ba = new B a n k A c c o u n t ( 2 0 0 M ) ;
MakeAWithdrawal(ba,
100M);
sa = new S a v i n g s A c c o u n t ( 2 0 0 M ,
12);
MakeAWithdrawal(sa,
100M);
//
Console.WriteLine("
BankAccount
{0:C}",
ba.Balance);
Console.WriteLine("
SavingsAccount
{0:C}",
sa.Balance);
//
Console.WriteLine(" <Enter> " +
" ...");
Console.Read();
(
, ) :
13.
291
BankAccount
SavingsAccount
<Enter>
$100.00
$100.00
...
, M a i n ( ) ,
M a k e A W i t h d r a w a l
count?
SavingsAccount,
().
M a k e A W i t h d r a w a l ()
SavingsAccount
BankAc
BankAc
S a v i n g s A c c o u n t , .
W i t h d r a w ()
b a . W i t h d r a w ()
ba
?
B a n k A c c o u n t ,
B a n k A c c o u n t . W i t h d r a w ( ) .
, b a B a n k A c c o u n t ,
SavingsAccount,
SavingsAc
c o u n t . W i t h d r a w ( ) . .
# .
. B a n k A c c o u n t , .
. , ,
..
. , ,
S a v i n g s A c c o u n t , B a n k A c c o u n t .
,
(late
binding).
(early b i n d i n g ) ,
, .
- .
. ,
.
.
-
. , , ,
-.
, ,
,
(object-based
lan
guages). Ada.
292
IV. -
.
, . , ,
. . . , , S t u d e n t .
, -
( ,
).
, ,
, , , ,
( , ) . ,
.
, C a l c T u i t i o n ( ) ,
. :
void S o m e F u n c t i o n ( S t u d e n t
s)
{
// . .
. -
s. C a l c T u i t i o n ( ) ;
II... . . .
}
# ,
S o m e F u n c t i o n ( ) , , s
S t u d e n t G r a d u a t e S t u d e n t .
Student. C a l c T u i t i o n ( )
, s S t u -
|dent, G r a d u a t e S t u d e n t . C a l c T u i t i o n ( ) G r a d u a t e S t u d e n t .
, .
. , C a l c T u i t i o n ( )
. . .
,
C a l c T u i t i o n ()
. , , ,
. . .
# ,
.
i s
?
# i s (
12,
"").
ba
is
SavingsAccount
t r u e f a l s e
. B a n k A c c o u n t ,
? i s
W i t h d r a w ( )
public
public
class
S a v i n g s A c c o u n t .
Program
s t a t i c
void
MakeAWithdrawal(BankAccount
decimal
13.
ba,
mAmount)
293
if
ba
is
SavingsAccount
{
SavingsAccount
sa
(SavingsAccount)ba,
sa.Withdraw(mAmount);
}
else
{
ba.Withdraw(mAmount);
}
, M a i n () S a v i n g s A c c o u n t ,
M a k e A W i t h d r a w a l ()
ba
SavingsAccount.Withdraw().
:
((SavingsAccount)ba).Withdraw(mAmount);
, ,
.
,
.
i s ,
is M a k e A W i t h d r a w a l ()
, ( )
M a k e A W i t h d r a w a l ()
. , ,
, , C h e c k i n g A c c o u n t ,
,
.
M a k e A W i t h d r a w a l ()
, ,
.
, M a k e A W i t h d r a w a l ( ) , .. # ;
, ,
.
# W i t h d r a w ()
.
v i r t u a l ,
o v e r r i d e .
, W i t h d r a w ()
, ,
.
//
//
Polymorphiclnheritance
294
IV. -
using
System;
namespace
Polymorphiclnheritance
{
// B a n k A c c o u n t -
public
class
BankAccount
{
protected
public
public
mlnitialBalance)
mlnitialBalance;
decimal
{
public
mBalance;
BankAccount(decimal
mBalance
get
decimal
Balance
return
mBalance;
virtual
decimal
Withdraw(decimal
mAmount)
Console.WriteLine("BankAccount.Withdraw()
mAmount);
decimal
if
mAmountToWithdraw
(mAmountToWithdraw
>
${o}...",
mAmount;
Balance)
{
mAmountToWithdraw
Balance;
}
mBalance -= mAmountToWithdraw;
return
mAmountToWithdraw;
// S a v i n g s A c c o u n t
//
public
class
SavingsAccount
BankAccount
{
public
decimal
mlnterestRate;
// SavingsAccount -
// 0 100
public
SavingsAccount(decimal
mlnitialBalance,
decimal
mlnterestRate)
:
base(mlnitialBalance)
{
this.mlnterestRate
mlnterestRate
// A c c u m u l a t e l n t e r e s t -
public
void Accumulatelnterest()
100;
{
mBalance
//
Withdraw .-
Balance
13.
(Balance
mlnterestRate),-
295
//
//
override
public
decimal
Withdraw(decimal
mWithdrawal)
{
Console.WriteLine("SavingsAccount.Withdraw()...");
C o n s o l e . W r i t e L i n e ( " Withdraw
"
. . . ' " ) ;
//
1.50
base.Withdraw(1.5);
//
return
"
base.Withdraw(mWithdrawal);
}
}
public
class
Program
{
public
s t a t i c
void
MakeAWithdrawal(BankAccount
ba,
decimal
mAmount)
{
ba.Withdraw(mAmount);
}
public
s t a t i c
BankAccount
void
args)
ba;
SavingsAccount
//
Main(string[]
sa;
Console.WriteLine("MakeAWithdrawal(ba,
. . . ) " ) ;
ba = n e w B a n k A c c o u n t ( 2 0 0 M ) ;;
MakeAWithdrawal(ba,
100M);
Console.WriteLine(" BankAccount
{0:C}",
ba.Balance);
Console.WriteLine("MakeAWithdrawal(sa,
. . . ) " ) ;
sa = new S a v i n g s A c c o u n t ( 2 0 0 M ,
12);
MakeAWithdrawal(sa,
100M);
Console.WriteLine("
SavingsAccount
{0:C}",
sa.Balance);
/ /
Console.WriteLine(" <Enter>
"
"
...");
Console.Read();
}
}
}
:
MakeAWithdrawal(ba,
... )
BankAccount.Withdraw()
$100...
BankAccount
$100.00
MakeAWithdrawal(sa,
...)
SavingsAccount.Withdraw()...
296
IV. -
W i t h d r a w . . .
BankAccount.Withdraw()
$ 1 . 5 . . .
BankAccount.Withdraw()
$100...
SavingsAccount
$98.50
< E n t e r > . . .
W i t h d r a w ( )
B a n k A c c o u n t v i r t u a l ,
o v e r r i d e . M a k e A W i t h d r a w a l ( )
,
ba. W i t h d r a w ( ) b a .
, ,
Visual Studio 2005.
, <F11> .
,
.
.
, v i r t u a l .
. .
- . , ,
. , B i r d
- B i r d ,
B i r d . ? - ,
, , .
(
), , .
, , .
, . ,
,
HighSchool
University,
. 13.1.
(Unified M o d e l i n g L a n g u a g e , U M L ) , ,
.
, .
. 1 3 . 1 , . ,
E n r o l l ( )
( ) .
S t u d e n t
n u m S t u d e n t s ,
.
: ,
13.
297
.
, ,
.
13.1.
UML-
HighSchool
University
G e t G r a n t ()
nAvgSAT.
. 13.1 ,
. , U n i v e r s i t y
H i g h S c h o o l , . 13.2.
13.2.
HighSchool
versity,
Uni
H i g h S c h o o l , U n i v e r s i t y
. , U n i v e r s i t y H i g h S c h o o l nAvgSAT G e t G r a n t ( ) .
.
: " ? ,
" . , , ,
.
, . , ,
,
.
, , -
298
IV. -
H i g h S c h o o l N a m e F a v o r i t e ( ) ,
.
. ,
N a m e F a v o r i t e ( )
, U n i v e r s i t y
.
, ,
. - , ,
.
, " " , .
UML Lite
(Unified M o d e l i n g L a n g u a g e , U M L )
,
. U M L ,
.
U M L .
,
. .
- , .
, .
( + ) ,
(-) . U M L ,
#.
;
.
{ a b s t r a c t } .
U M L ,
. U M L
Lite.
.
.
(*) . ,
1. , . 13.1 ,
--.
( ) .
. ,
.
'
. , . 13.2,
, U n i v e r s i t y H i g h S c h o o l
. , , .
, .
13.
299
, , ,
, , , .
? ,
.
? , .
, , .
. 13.3 . S c h o o l
, stu
d e n t . , S c h o o l E n r o l l ( ) , !
U n i v e r s i t y , H i g h S c h o o l -.
13.3.
University
HighSchool
School
U n i v e r s i t y H i g h S c h o o l .
H i g h S c h o o l NameFavorite!),
a U n i v e r s i t y G e t G r a n t . , E n r o l l ( ) ,
. ,
,
.
S c h o o l .
1
. ,
. , .
.
, , U n i v e r s i t y ,
H i g h S c h o o l .
(factoring).
-1
, !
.
300
IV. -
M o u s e J o y s t i c k ,
, M o u s e D i s p l a y .
. ,
, , ,
. 13.4.
Puc.
13.4.
,
Learning
U n i v e r s i t y
LowerLevel.
School:
HigherLearning
Higher-
C o l l e g e U n i v e r s i t y .
, .
, , . ,
. 13.4 ,
, , . 13.5.
Puc.
13.5.
13.
301
B a n k A c c o u n t .
,
.
, S a v e i n g s A c c o u n t . W i t h d r a w ()
C h e c k i n g A c c o u n t . W i t h d r a w ( ) . BankAc
count
.Withdraw()?
. ?
? .
? ?
, . .
.
, .
, " " . ( )
, . ,
. ,
W i t h d r a w ()
( ,
).
B a n k A c c o u n t .
.
.
, ? :
, a b s t r a c t .
? :
.
.
//
Abstractlnheritance
//
//
namespace
BankAccount
Withdraw()
Abstractlnheritance
{
using
System;
//
AbstractBaseClass
//
abstract
public
class
Output()
AbstractBaseClass
{
//
//
//
abs
Output - ,
,
,
tract
public
void Output(string
sOutputString);
}
//
302
SubClassl
IV. -
//
AbstractBaseClass
public c l a s s
SubClassl
AbstractBaseClass
{
override
public
void
Output(string
sSource)
{
string
sSource.ToUpper();
Console.WriteLine("
SubClassl.Output()
{}",
s) ;
}
}
//
SubClass2
//
AbstractBaseClass
public
class
SubClass2
AbstractBaseClass
{
override
public
void
Output(string
sSource)
{
string
sSource.ToLower();
n rs oo gl re a. W
c l a s sC o P
mriteLine("
s) ;
SubClass2.Output()
{0}",
public
s t a t i c
void
Test(AbstractBaseClass
ba)
{
ba.Output("Test");
}
public
s t a t i c
void
Main(string []
strings)
{
//
AbstractBaseClass,
// .
// ,
#
//
// A b s t r a c t B a s e C l a s s ba
= new A b s t r a c t B a s e C l a s s ( ) ;
/ / S u b c l a s s l
Console.WriteLine("
SubClassl");
S u b C l a s s l s c l = new S u b C l a s s l ( ) ;
Test(scl) ;
// S u b c l a s s 2
Console.WriteLine("\
SubClass2");
SubClass2
sc2 = new S u b C l a s s 2 ( ) ;
Test(sc2) ;
//
Console.WriteLine(" <Enter> " +
" ...");
Console.Read();
A b s t r a c t B a s e C l a s s
Output ().
abstract,
Output ( ) , .. .
13.
303
A b s t r a c t B a s e C l a s s : S u b C l a s s l SubClass2
, O u t p u t () ""
.
.
,
() .
O u t p u t ( )
, .
A b s t r a c t B a s e C l a s s .
SubClassl
SubClassl.Output()
SubClass2.Output()
<Enter>
TEST
SubClass2
t e s t
...
,
v i r t u a l a b s t r a c t .
: A b s t r a c t B a s e C l a s s , Test()
A b s t r a c t B a s e C l a s s
. S u b C l a s s l
SubClass2
, A b s t r a c t B a s e C l a s s ,
. ,
.
v i r t u a l . ,
I n h e r i t a n c e T e s t .
//
InheritanceTest
//
v i r t u a l
namespace
InheritanceTest
{
using.
System;
public
class
Program
{
public
s t a t i c
void
Main(string[]
strings)
{
Console.WriteLine("\
BankAccount")
BankAccount ba = new B a n k A c c o u n t ( ) ;
304
IV. -
Testl(ba)
Console.WriteLine("\
SavingsAccount");
SavingsAccount
sa = new S a v i n g s A c c o u n t ( ) ;
Testl(sa) ;
Test2(sa) ;
Console.WriteLine("\
SpecialSaleAccount");
SpecialSaleAccount
s s a = new S p e c i a l S a l e A c c o u n t ( ) ;
Testl(ssa);
Test2(ssa) ;
Test3(ssa) ;
Console.WriteLine("\
S a l e S p e c i a l C u s t o m e r s s c = new
Testl(ssc);
Test2 (ssc)
Test3(ssc) ;
Test4(ssc) ;
//
SaleSpecialCustomer");
SaleSpecialCustomer();
public
s t a t i c
void
Testl(BankAccount
Console.WriteLine("\tB
account.Withdraw(100)
public
s t a t i c
void
account)
Test(BankAccount)");
Test2(SavingsAccount
account)
Test (SavingsAccount),") ;
account.Withdraw(100);
public
s t a t i c
void
Test3(SpecialSaleAccount
Console.WriteLine("\tB
account)
Test(SpecialSaleAccount)");
account.Withdraw(100);
public
s t a t i c
void
Test4(SaleSpecialCustomer
Console.WriteLine("\tB
account)
Test(SaleSpecialCustomer)");
account.Withdraw(10 0);
// B a n k A c c o u n t
//
public
class
BankAccount
taa 13.
305
//
//
//
W i t h d r a w a l - ,
.
v i r t u a l
public
void
Withdraw(decimal
dWithdraw)
{
C o n s o l e . W r i t e L i n e ( " \ \ "+
11
" B a n k A c c o u n t . W i t h d r a w ()
) ;
}
}
//
//
SavingsAccount
public
class
SavingsAccount
BankAccount
{
override
public
void
Withdraw(decimal
mWithdrawal)
{
Console .WriteLine ("\t\tBbi3biBaeT " +
"SavingsAccount.Withdraw()");
}
}
//
//
SpecialSaleAccount
public
class
SpecialSaleAccount
SavingsAccount
{
new
virtual
public
void
Withdraw(decimal
mWithdrawal)
{
Console .WriteLine ("\t\TBBI3BMAET " +
"SpecialSaleAccount.Withdraw()");
}
}
//
SaleSpecialCustomer
//
public
class
SaleSpecialCustomer
SpecialSaleAccount
{
override
public
void
Withdraw(decimal
mWithdrawal)
. , ,
S p e c i a l S a l e A c c o u n t . W i t h d r a w ()
v i r t u a l ,
. B a n k A c c o u n t S p e c i a l S a l e A c c o u n t S a l e S p e c i a l C u s t o m e r Savi n g s A c c o u n t . S p e c i a l S a l e A c c o u n t
W i t h d r a w ( ) .
. Main()
T e s t ( ) , ,]
306
IV. -
T e s t
W i t h d r a w ()
.
:
BankAccount
Test(BankAccount)
BankAccount.Withdraw()
1
SavingsAccount
Test(BankAccount)
SavingsAccount.Withdraw()
Test(SavingsAccount)
SavingsAccount.Withdraw()
SpecialSaleAccount
Test(BankAccount)
SavingsAccount.Withdraw()
Test(SavingsAccount)
SavingsAccount.Withdraw()
Test(SpecialSaleAccount)
SpecialSaleAccount.Withdraw()
SaleSpecialCustomer
Test(BankAccount)
SavingsAccount.Withdraw()
Test(SavingsAccount)
SavingsAccount.Withdraw()
Test(SpecialSaleAccount)
SaleSpecialCustomer.Withdraw()
Test(SaleSpecialCustomer)
SaleSpecialCustomer.Withdraw()
<Enter> ...
, .
BankAccount S a v i n g s A c c o u n t , .
T e s t ( S a v i n g s A c c o u n t )
Customer
SpecialSaleAccount,
SavingsAccount.
tomer T e s t ( S p e c i a l S a l e A c c o u n t )
SaleSpecial
SaleSpecialCus
, .. .
# ?
?
# " " ,
.
, ,
, .
, .
,
.
.
13.
307
,
. nocgj
s e a l e d
:
using
System;
public
class
BankAccount
{
//
//
//
W i t h d r a w a l - ,
.
v i r t u a l
public
void
Withdraw(decimal
mWithdraw)
{
Console.WriteLine("
BankAccount.Withdraw()");
}
}
public
sealed
class
SavingsAccount
BankAccount
{
override
public
void
Withdraw(decimal
mWithdrawal)
{
C o n s o l e . W r i t e L i n e ( "
S a v i n g s A c c o u n t . W i t h d r a w ()
11
) ;
}
public
class
SpecialSaleAccount
SavingsAccount
{
override
public
void
Withdraw(decimal
mWithdrawal)
{
Console.WriteLine(" " +
11
" S p e c i a l S a l e A c c o u n t . W i t h d r a w ()
) ;
:
'SpecialSaleAccount'
'SavingsAccount'
cannot
inherit
from
sealed
class
s e a l e d
. , ,
, , , ,
.
, , -
, .
, , , ,
.
308
IV. -
, B a n k A c c o u n t . #
. ,
- ( i n t
f l o a t ) , ,
.
, 13, " " ,
. !
. #
(generic)
.
,
, .
14
_ _
,
.
. .
. # ,
_ _ .
, ,
.
" " T a k e A N o t e . , # :
::stract class ThingsThatRecord
{
abstract
public
void
TakeANote ( s t r i n g
sNote) ;
}
public class Pen : ThingsThatRecord
{
override public void TakeANote (string sNote)
{
II . . . . . .
}
}
public class PDA : ThingsThatRecord
override
II
)
public
.
void
TakeANote ( s t r i n g
sNote)
.
"
public
class
Laptop
ThingsThatRecord
{
override
public
void
TakeANote(string
sNote)
{
II... - . . .
}
}
abstract
" " , . ,
12, " " .
,
T a k e A N o t e ( ) . R e
c o r d T a s k ( ) T a k e A N o t e ( ) ,
, :
void
RecordTask(ThingsThatRecord
things)
{
//
//
ThingsThatRecord
things.TakeANote(" "),// .
.
. .
.
.
}
.
. ,
. ,
, ,
.
. L a p t o p
C o m p u t e r . PDA
C o m p u t e r , . py
M e c h a n i c a l W r i t e D e v i c e ( y c
) D e v i c e T h a t S t a i n s Y o u r S h i r t ( ,
) . #
# .
. ,
,
-.
_ _ R e c o r d a b l e
.
, . " ,
", :
312
V.
interface IRecordable
{
void
TakeANote(string
sNote)
i n t e r f a c e ,
c l a s s . .
- .
T a k e A N o t e ( )
. p u b l i c v i r t u a l
a b s t r a c t . ,
, .
, ,
. , ,
o v e r r i d e .
I, ,
, , (
) . , #
, .
PDA
IRecordable.
public
class
PDA
IRecordable
{
public
void
TakeANote ( s t r i n g
sNote)
{
II
, T h i n g s T h a t R e c o r d I R e c o r d a b l e .
.
.
, . ,
, .
, ,
,
public class Pen : IRecordable
{
public
void
TakeANote ( s t r i n g
sNote)
{
II
. . .
14.
313
public
class
PDA
.-
ElectronicDevice,
IRecordable
{
public
void
TakeANote(string
sNote)
{
//
}
}
public
class
Laptop
Computer,
IRecordable
{
public
//
{
void
TakeANote(string
sNote)
, )
I R e c o r d a b l e , ,
T a k e A N o t e ( ) . ,
, R e c o r d S h o p p i n g L i s t ( ]
public
class
Program
{
s t a t i c
public
void
RecordShoppingList(IRecordable
recordingObj
ect)
//
string sList
= GenerateShoppingList();
//
recordingObject.TakeANote(sList)
;
public
s t a t i c
void
Main(string []
args)
{
PDA p d a = n e w P D A ( ) ;
RecordShoppingList(pda);
}
, R e c o r d S h o p p i n g L i s t ( )
, T a k e A N o t e ()
, , .
R e c o r d S h o p p i n g L i s t () ,
r e c o r d i n g O b j e c t . ,
PDA E l e c t r o n i c D e v i c e , ,
.
, Record
S h o p p i n g L i s t ()
, ,
. ,
,
,
.
.
314
V. 1
SortInterface
.
. .
. . . :) , .
I D i s p l a y a b l e , G e t S t r i n g ( ) (, , , I D i s p l a y a b l e ) . G e t String ( )
s t r i n g ,
( W r i t e L i n e ( ) :
//IDisplayable ,
interface
IDisplayable
GetStringO
{
//
string G e t S t r i n g O ;
}
S t u d e n t I D i s p l a y a b l e :
class S t u d e n t
private
st
private d o
/ /
public
str
IDisplayable
ring
sName;
uble dGrade = 0.0;
i n g Name
{
get
return
sName;
}
public
double
Grade
{
get
return
dGrade;
}
// G e t S t r i n g -
//
public
//
string
GetString()
implements
the
interface
string
string
sPadName = N a m e . P a d R i g h t ( 9 ) ;
11
s = S t r i n g . F o r m a t ( {0} :
{l:N0}",
sPadName, G r a d e ) ;
return
s;
[ 14.
315
P a d R i g h t ( ) ,
9 (
) . (
9 , " # " ) . { l : N 0 } :
( )
3 . .
( ):
// D i s p l a y A r r a y ,
)
//
IDisplayable
public
s t a t i c
void DisplayArray (IDisplayable []
displayables):
int
length =
displayables.Length;
f o r ( i n t i n d e x = 0;
index < length;
index++)
IDisplayable
displayable =
displayables[index];
Console.WriteLine("{0}",
displayable.GetString()
}
D i s p l a y A r r a y ( )
, G e t S t r i n g ( ) .
:
Homer
Marge
Bart
Lisa
Maggie
:
:
:
:
:
0
85
50
100
30
#.
, # I C o m p a r a b l e :
interface IComparable
{
//
// ,
//
int
CompareTo(object
);
- 1 ,
' ' ;
, 0
1,
}
I C o m p a r a b l e
, S t r i n g .
, 0. , 1,
, " " .
,
S t u d e n t - , .
C o m p a r e T o ()
, oral
316
objects);
V. *
, I C o m p a r a ble. ,
, S t u d e n t . A r r a y S t u d e n t :
//Student -
//
class S t u d e n t :
IComparable
private
double
dGrade;
/ /
public double Grade
{
get { return dGrade; }
}
// CompareTo - ;
// " "
public i n t
CompareTo ( o b j e c t
rightObject)
{
Student l e f t S t u d e n t = t h i s ;
Student r i g h t S t u d e n t =
(Student) rightObject ;
// 16 -1 0
//
if ( r i g h t S t u d e n t . G r a d e < l e f t S t u d e n t . G r a d e )
{
return
- 1 ;
}
if
(rightStudent.Grade
>
leftStudent.Grade)
return
1 ;
}
return
0;
S t u d e n t :
id MyFunction ( S t u d e n t [] s t u d e n t s )
/ /
Array.Sort ( s t u d e n t s ) ;
IComparable
; A r r a y .
: S o r t I n t e r f a c e ,
.
[Sortlnterf -
using S y s t e m ;
14.
Sortlnterf
317
namespace
Sortlnterf
{
//
IDisplayable
//
interface
IDisplayable
{
// G e t S t r i n g -
//
string
G e t S t r i n g O ;
}
class
Program
{
public
s t a t i c
void
Main(string []
args)
{
// ...
Console.WriteLine("
");
//
Student[]
students = Student.CreateStudentList();
//
//
IComparable[]
IComparable
comparableObjects =
(IComparable[])students;
Array.Sort(comparableObjects);
//
I D i s p l a y a b l e
I D i s p l a y a b l e []
displayableObjects
=
(IDisplayable[])students;
DisplayArray(displayableObj
ects);
//
// ,
// S t u d e n t
Console.WriteLine("\ ");
Bird[]
birds = Bird.CreateBirdList();
Bird
//
// . . .
Array.Sort(birds);
DisplayArray(birds);
/ /
Console.WriteLine(" <Enter> " +
" .. . " ) ;
Console.Read();
}
// D i s p l a y A r r a y
//
IDisplayable
public
s t a t i c
void
318
V.
D i s p l a y A r r a y [ I D i s p l a y a b l e [] d i s p l a y a b l e s )
int
length =
displayables.Length;
f o r ( i n t i n d e x = 0;
index < length;
index++)
{
IDisplayable
displayable
Console.WriteLine("{0}",
=
displayables[index];
displayable.GetString());
//
S t u d e n t s -
// S t u d e n t -
//
class
Student
IComparable,
IDisplayable
(
private
private
string
double
sName;
dGrade
0.0;
// -
public S t u d e n t ( s t r i n g sName,
double dGrade)
this.sName = sName;
this.dGrade = dGrade;
// C r e a t e S t u d e n t L i s t -
//
static s t r i n g []
sNames =
{"Homer",
static
doublet]
public
static
"Marge",
dGrades
"Bart",
S t u d e n t []
{0,
85,
"Lisa",
50,
"Maggie"};
100,
30};
C r e a t e S t u d e n t L i s t ()
{
Student[]
s A r r a y = new S t u d e n t [ s N a m e s . L e n g t h ] ;
for ( i n t i = 0; i < s N a m e s . L e n g t h ; i + + )
{
sArrayti]
new
Student(sNames[i],
dGrades[i]);
}
return
sArray;
/ /
public s t r i n g N a m e
get
public
get
return
double
return
sName;
Grade
dGrade;
// I C o m p a r a b l e :
// CompareTo -
14.
319
//
//
public
int
Student)
CompareTo(object
rightObject)
{
// S t u d e n t
( )
//
( )
,
// S t u d e n t
Student leftStudent = t h i s ;
if
(!(rightObject
is
Student))
{
Console.WriteLine("
r e t u r n 0;
Student")
}
Student
rightStudent
0
//
//
- 1 ,
if
(rightStudent.Grade
(Student)rightObject;
<
leftStudent.Grade)
>
leftStudent.Grade)
{
return
- 1 ;
}
if
(rightStudent.Grade
return
return
1;
0,
}
// IDisplayable:
// G e t S t r i n g -
//
public
string
GetString
{
string
sPadName
string
return
Name. P a d R i g n t ( ) ;
String.Format("{o}:
{l:N0}",
sPadName,
Grade)
s;
//
Birds
j u s t an a r r a y of b i r d names
:
IComparable,
IDisplayable
{
private
//
string
public
sName;
-
Bird(string
Bird
sName)
{
this.sName
sName;
320
//
CreateBirdList
//
V.
static
{
s t r i n g []
sBirdNames
"Oriole",
"Hawk",
"Bluejay",
public
s t a t i c
"Finch",
Bird[]
"Robin",
"Cardinal",
"Sparrow"};
CreateBirdList ()
{
Bird[]
'
birds
for(int
=
0;
new
Bird[sBirdNames.Length];
birds.Length;
<
i++)
{
birds[i]
new
Bird(sBirdNames[i]);
}
return
birds;
/ /
p u b l i c s t r i n g Name
get
return
sName;
// I C o m p a r a b l e :
// CompareTo - ;
// S t r i n g
public
int
CompareTo(object
rightObject)
{
// B i r d
(
//
( )
Bird l e f t B i r d =
t h i s ;
Bird r i g h t B i r d =
(Bird)rightObject;
return
String.Compare(leftBird.Name,
rightBird.Name);
// I D i s p l a y a b l e :
// G e t S t r i n g -
public
string GetString()
{
return
Name,-
S t u d e n t ( ) I C o m p a r a
ble I D i s p l a y a b l e , . C o m p a r e T o
, . G e t
String () .
S t u d e n t N a m e G r a d e ,
C r e a t e S t u d e n t L i s t ( ) . < .
B i r d
IComparable IDis
playable. C o m p a r e T o ( ) ,
S t r i n g . ,
. G e t N a m e ( )
[( 14.
321
M a i n (). C r e a t e S t u d e n t L i s t ()
,
students.
, ,
.
c o m p a r a b l e O b j e c t s .
" " ) .
, S t u d e n t , compa
r a b l e O b j e c t s , ICom
p a r a b l e , .
,
.
comparableObj ects
A r r a y . Sort ( ) ,
.
S t u d e n t
DisplayArray (),
, G e t S t r i n g ( ) .
A r r a y . L e n g t h .
G e t S t r i n g ( ) ,
W r i t e L i n e ( ) .
. , ,
. B i r d
I C o m p a r a b l e I D i s p l a y a b l e
.
, M a i n ()
. :
class
BaseClass
class
class
Subclass
Program
{}
:
BaseClass
{}
{
public
public
s t a t i c
s t a t i c
void
void
SomeFunction(BaseClass
AnotherFunction()
be)
{}
{
Subclass
sc
new
Subclass();
SomeFunction(sc);
S u b c l a s s
S u b c l a s s
B a s e C l a s s ,
B a s e C l a s s .
, B i r d ,
I C o m p a r a b l e , B i r d ICompara
ble.
D i s p l a y A r r a y ()
b i r d s
, B i r d s I D i s p l a y a b l e .
:
V.
Lisa
100
Marge
85
Bart
50
Maggie
30
Homer
Blue
jay
Cardinal
I Finch
Hawk
Oriole
Robin
Sparrow
<Enter>
. . . "
, ,
.
" " . ,
, , .
,
.
//
ICompare
// ,
public
interface
ICompare
IComparable
{
//
int
GetValue
GetValue();
I C o m p a r e C o m p a r e T o ( )
I C o m p a r a b l e . G e t V a l u e ( ) .
I C o m p a r e I C o m p a r a b l e ,
. # - . .
, .
A b s t r a c t l n t e r f .
( , ) , , ,
.
14.
323
// A b s t r a c t l n t e r f
//
using
System;
namespace
Abstractlnterf
{
// ICompare - ,
// ,
p u b l i c i n t e r f a c e ICompare
:
IComparable
//
GetValue
int
}
//
//
//
int
GetValue();
BaseClass
- ICompare,
GetValue()
CompareTo()
abstract
public
class
BaseClass
ICompare
{
int
nValue;
public
BaseClass(int
nlnitialValue)
{
nValue
nlnitialValue;
}
//
//
public
int
ICompare:
GetValue()
{
return
nValue;
}
//
? 7
abstract
public
//
Subclass
//
public
^
//
CompareTo(object
rightObject);
CompareTo
Subclass:
'
int
class
ICompare
BaseClass
//
public
Subclass(int
nlnitialValue)
base(nlnitialValue)
{
}
//
CompareTo
II
//
override
324
public
IComparable;
int
CompareTo(object
rightObject)
V.
BaseClass
return
public
be
class
public
(BaseClass)rightObject;
GetValue().CompareTo(be.GetValue());
Program
s t a t i c
Subclass
Subclass
void
scl
sc2
MyFunc ( s c l ,
=
=
Main(string[]
new
new
strings)
Subclass(10) ;
S u b c l a s s (20) ;
s c 2 ) ,-
//
Console.WriteLine(" <Enter> " +
" ...");
Console.Read();
}
// MyFunc
//
//
public
s t a t i c
ICompare
,
void
MyFunc(ICompare
icl,
ICompare
ic2)
Console.WriteLine(" i c l
{0},
" +
"a
ic2
{l}",
i c l . G e t V a l u e () ,
i c 2 . G e t V a l u e ());
string
s;
switch
(icl.CompareTo(ic2))
{
case
s
0 :
=
"";
break;
case
s
- 1 :
=
"" ;
break;
case
s
1:
=
"";
default:
s = "";
break;
}
Console . W r i t e L i n e (
"
icl
{o}
fma 14.
ic2".
s) ;
325
I C o m p a r e ,
. I C o m p a r e C o m p a r e T o ()
I C o m p a r a b l e , , G e t V a l u e (),
.
i n t , GeT
V a l u e ()
.
B a s e C l a s s I C o m p a r e GetValue
n V a l u e . C o m p a r e T o ( ) ,
I C o m p a r e , .
a b s t r a c t ,
C o m p a r e T o ( ) .
.
S u b c l a s s C o m p a r e T o ( ) , ,
.
,
Subclass
ICompare
, . B a s e C l a s s
I C o m p a r e , a S u b c l a s s B a s e C l a s s . , Sub
C l a s s ICompare.
M a i n ()
S u b c l a s s
M y F u n c ( ) . M y F u n c ()
ICompare.
M y F u n c ()
C o m p a r e T o () , ,
G e t V a l u e ()
"" .
:
i c l 10,
a ic2
i c l
ic2
< E n t e r >
20
...
# .
- , i n t d o u b l e ,
:
int ;
n = 1;
326
//
//
V.
:
public
class
public
int
MyClass
n;
MyClass m c ;
//
mc = new M y C l a s s () ;
mc
//
. i n t d o u b l e
-.
, ,
, # -
, .
, ?
#
# , ,
- .
, :
public
struct
MyStruct
public
int
public
double
public
class
n;
d;
MyClass
public
int
public
double
n;
d;
, ,
- , :
//
int ;
n = 1;
/ / i n t
MyStruct m s ;
//
ms.n = 3 ;
// ,
ins.d = 3 . 0 ;
//
//
// new
MyClass mc = n e w M y C l a s s ;
men = 2 ;
mc.d = 2 . 0 ;
s t r u c t , .
ms ,
. ms , ,
. 1 4 . 1 .
km 14.
327
. 14.1. ms ""
, ,
-
. 100 101
n e w ( ):
M y C l a s s [ ] mc = new M y C l a s s [ 1 0 0 ] ;
f o r ( i n t i = 0; i < m s . L e n g t h ; i + + )
{
mc [ i ] = new M y C l a s s ( ) ;
}
m c [ 0 ] . n = 0;
, yb
, .
,
.
M y C l a s s
- .
,
100 ,
.
, :
//
i n t [] i n t e g e r s
integers[0] =
//
M y S t r u c t [ ] ms
ms [ 0] . n = 0 ;
i n t
= new i n t [ 1 0 0 ] ;
//
0;
= new M y S t r u c t [ 1 0 0 ] ; //
328
V.
,
, ,
public
struct
public
public
MyStruct
i n t n;
double
d;
}
HyStruct m s = n e w M y S t r u c t ( ) ; / / n e w
,
d.
, -
. :
public
struct
Test
(
;
private i n t n;
public T e s t ( i n t
n)
{
this.n
public
class
n;
Program
{
public
static
void
test
new
Main ( s t r i n g []
args)
{
Test
Test (10);
, t e s t = n e w T e s t ( 1 0 ) ;
, -.
, , .
, .
. ,
( ) .
, ,
r e f .
( O b j e c t ,
) .
.
8, " ". 7, " ",
.
12, "".
14.
329
( ) O b j e c t ,
. O b j e c t .
,
T o S t r i n g ( ) ,
.
T o S t r i n g ( ) , O b j e c t
, M y N a m e s p a c e . M y C l a s s . .
// S t r u c t u r e E x a m p l e -
//
using
System;
using
System.Collections;
namespace
StructureExample
{
public
string
interface
IDisplayable
ToString();
/ /
public s t r u c t Test
:
IDisplayable
{
/ / ,
// ( ) ;
//
private int n;
p r i v a t e s t a t i c double d = 2 0.0;
/ / -
//
public Test(int n)
{
t h i s . n = n;
}
//
//
()
public int N
get
set
public
{ r e t u r n n;}
{ n = value;
static
double
330
V.
get
set
{
{
r e t u r n d; }
d = value; }
}
//
p u b l i c v o i d C h a n g e M e t h o d ( i n t nNewValue,
double
dNewValue)
{
n = nNewValue;
d = dNewValue;
}
// T o S t r i n g - T o S t r i n g
// I D i s p l a y a b l e
override public string ToString()
{
return string.Format("({0:N},
{l:N})'\ n, d ) ;
public
class
Program
{
public
static
void
Main(string[]
args)
{
// T e s t
T e s t t e s t = new T e s t ( 1 0 ) ;
Console.WriteLine("
OutputFunction(test);
test");
// ,
//
ChangeValueFunction(test,
100,
200.0);
C o n s o l e . W r i t e L i n e ( " t e s t " +
" C h a n g e V a l u e F u n c t i o n ( 1 0 0, 2 0 0 . 0 ) " ) ;
OutputFunction(test);
// ,
//
ChangeReferenceFunction(ref t e s t ,
100,
200.0);
Console.WriteLine(" t e s t " +
" ChangeReferenceFunction(100, 200.0)");
OutputFunction(test);
//
test.ChangeMethod(1000,
2000.0);
C o n s o l e . W r i t e L i n e ( " t e s t " +
" ChangeMethod(1000, 2 0 0 0 . 0 ) " ) ;
OutputFunction(test);
/ /
Console.WriteLine(" <Enter> " +
" . . . " ) ;
14.
331
Console.Read();
}
// C h a n g e V a l u e F u n c t i o n - j
public s t a t i c void ChangeValueFunction(Test t,
i n t newValue,
d o u b l e dNewValue)
{
t . N = newValue,T e s t . D = dNewValue;
}
// C h a n g e R e f e r e n c e F u n c t i o n -
//
public s t a t i c void ChangeReferenceFunction(ref Test t,
i n t newValue,
d o u b l e dNewValue)
{
t . N = newValue;
T e s t . D = dNewValue;
}
/ / O u t p u t F u n c t i o n - ,
// T o S t r i n g ( )
p u b l i c s t a t i c void O u t p u t F u n c t i o n ( I D i s p l a y a b l e id)
{
Console.WriteLine("id
{o}",
id.ToString());
S t r u c t u r e E x a m p l e I D i s p l a y a b l e J
T e s t , . T e s t d. d 20; .
T e s t , N
D.
T e s t C h a n g e M e t h o d () !
T o S t r i n g ( ) . T o S t r i n g ( ) T e s t I D i s p l a y a b l e .
M a i n () t e s t . M a i n () Outp u t F u n c t i o n () .
M a i n () C h a n g e V a l u e F u n c t i o n ( ) ,
t e s t . C h a n g e V a l u e F u n c t i o n ()
d T e s t .
C h a n g e V a l u e F u n c t i o n () O u t p u t F u n c t i o n ( ) ,
, d , .
C h a n g e V a l u e F u n c t i o n () t e s t ,
t t e s t ,
. , t. N
t e s t e M a i n ( ) . T e s t -
332
V.
d, T e s t . D d
, t e s t .
C h a n g e R e f e r e n c e F u n c t i o n ( ) ,
C h a n g e V a l u e F u n c t i o n ( ) ,
r e f .
t e s t e , t
t e s t , .
M a i n () C h a n g e M e t h o d ( ) ,
, ,
, M a i n ( ) .
:
t e s t
id = ( 1 0 . 0 0 , 2 0 . 0 0 )
t e s t C h a n g e V a l u e F u n c t i o n ( 1 0 0 , 2 0 0 . 0 )
id = ( 1 0 . 0 0 , 2 0 0 . 0 0 )
t e s t C h a n g e R e f e r e n c e F u n c t i o n ( 1 0 0 , 2 0 0 . 0 )
id = ( 1 0 0 . 0 0 , 2 0 0 . 0 0 )
t e s t C h a n g e M e t h o d ( 1 0 0 0 , 2 0 0 0 . 0 )
id = ( 1 , 0 0 0 . 00 , 2 , 0 0 0 . 0 0 )
< E n t e r > . . .
:
Object, .
.
- .
- .
Int32,
d o u b l e
i n t
Double
. 14.1 .
14.1. -
bool
Boolean
byte
Byte
sbyte
SByte
char
Char
decimal
Decimal
double
Double
float
Single
int
Int32
uint
UInt32
14.
333
. HI
s t r i n g , -,
. s t r i n g
S t r i n g . s t r i n g # ,
. 9, "
# " .
i n t I n t 3 2 .
O b j e c t , i n t .
,
.
// T y p e U n i f i c a t i o n -
// ,
// Object
using System;
namespace
, i n t I n t 3 2
TypeUnification
{
public
class
Program
{
public
static
void
Main(string[]
args)
{
// i n t
i n t i = new i n t ( ) ;
/ / ,
/ / 1
// I F o r m a t t a b l e , I n t 3 2
i = 1;
OutputFunction(i);
II 2
OutputFunction(2);
IFormattable
//
//
C o n s o l e . W r i t e L i n e ( " = { } " ,
3.ToString());
//
//
334
, ,
:
V.
Console.WriteLine("\ ");
object[]
o b j e c t s = new o b j e c t [ 5 ] ;
objects[0] = " ";
o b j e c t s [ 1 ] = 2;
o b j e c t s [ 2 ] = new P r o g r a m ( ) ;
o b j e c t s [ 3 ] = 4;
objects[4] = 5.5;
f o r ( i n t i n d e x = 0; i n d e x < o b j e c t s . L e n g t h ; index++)
{
if
(objects[index]
is
int)
{
int n = (int)objects[index];
Console.WriteLine(" {}
{l}",
index,
n);
}
}
// -
// ,
Console.WriteLine("\ " ) ;
i n t nCount = 0;
f o r e a c h ( o b j e c t in o b j e c t s )
{
Console.WriteLine("Objects [{o}]
nCount++, o . T o S t r i n g ( ) ) ;
- <{l}>",
}
/ /
Console.WriteLine(" <Enter> " +
" . . . " ) ;
Console.Read() ;
// O u t p u t F u n c t i o n - ,
// T o S t r i n g ()
public s t a t i c void O u t p u t F u n c t i o n ( I F o r m a t t a b l e id)
Console.WriteLine(" OutputFunction =
id.ToString());
{}",
}
// T o S t r i n g -
override public string ToString()
{
return
"TypeUnification
Program";
M a i n () i i n t .
I n t 3 2 ( i n t ) ,
i .
i 1. ,
.
14.
335
M a i n () i O u t p u t F u n c t i o n ( ) ,
, I F o r m a t t a b l e .
I F o r m a t t a b l e I D i s p l a y a b l e ,
. I F o r m a t t a b l e ToStrirqj
O u t p u t F u n c t i o n () I F o r m a t t a b l e
, T o S t r i n g ( ) . I n t 3 2 ,
, . ,
O u t p u t F u n c t i o n ( 2 ) . 2
I n t 3 2 , I F o r m a t t a b l e . ,
M a i n () 3. T o S t r i n g () .
M a i n () :
O u t p u t F u n c t i o n = 1
O u t p u t F u n c t i o n = 2
= 3
M a i n ( ) O b j e c t
s t r i n g , i n t , Program ..
, S t r i n g , I n t 3 2 P r o g r a m Object.
P r o g r a m P r o g r a m , ?
. Main()
, ,
I n t 3 2 i s .
:
12
34
,
Obj e c t .. T o S t r i n g ( ) . ,
, .
M a i n () , T o S t r i n g ()
. :
O b j e c t s [0]
- >
O b j e c t s [ 1 ] - <2>
Objects[2]
- < T y p e U n i f i c a t i o n Program>
O b j e c t s [ 3 ] - <4>
Objects[4]
- <5.5>
< E n t e r > . . .
P r o g r a m T o S t r i n g ( ) ,
, .
T o S t r i n g ( ) , , C o n s o l e .
W r i t e ( ) . , , ,
W r i t e () O b j e c t .
T o S t r i n g () ,
{} .
336
V.
-
- i n t , b o o l ,
char # ,
(boxing).
- .
- . ,
:
int i = 9 9 9 ;
object = i;
int j = ( i n t ) ;
// i n t ( - )
// i
// 99 9
, , ,
. T y p e U n i f i c a t i o n
o b j e c t , o b
ject .
. 20
, 4 . ,
,
.
, , ,
, , o b j e c t [ ] , W r i t e
Line () . ,
ToString () , W r i t e L i n e ( ) ,
object , 15,
" ".
[ 14.
337
15
:
,
# ,
6, " ".
I , , " "
, A r r a y L i s t ,
. ,
.
,
6
. (generics) #, 2.0.
, ,
. , ,
. , L i s t < T > , A r r a y L i s t .
{) ,
i n t , i n t :
List<int> m y L i s t = new L i s t < i n t > () ; // i n t
,
List<T> s t r i n g , S t u d e n t , B a n k A c c o u n t
, ,
. .
# , L i s t < T > ,
, .. .
.
. , # ,
. ++,
, , ,
. # generic class, generic.
, ,
"". " ". . .
, ,
-.
. -
.
. Vi
ual Basic, # !
, , ?
, 1
, .
# )
. .
# . . lil
. ,
( ,
).
340
V.
, . ,
( f o r e a c h ) .
.
// N o n g e n e r i c C o l l e c t i o n s
//
using S y s t e m ;
using S y s t e m . C o l l e c t i o n s ;
namespace N o n g e n e r i c C o l l e c t i o n s
(
public
class
Program
{
// A r r a y L i s t , S t a c k , Queue
public s t a t i c void M a i n ( s t r i n g [ ]
args)
Hashtable
{
//
ArrayList
//
/ / A r r a y L i s t (
// , )
A r r a y L i s t a L i s t W i t h S p e c i f i e d S i z e = new A r r a y L i s t ( 1 0 0 0 ) ;
A r r a y L i s t a L i s t = new A r r a y L i s t ( ) ;
// no
// (16)
aList.Add("one");
//
aList.Add("two");
// - " o n e " , " t w o "
a L i s t . A d d ( " t h r e e " ) ; // - " o n e " , " t w o " , " t h r e e "
Console.WriteLine("{0}
items
in the ArrayList:",
aList.Count);
/ / f o r e a c h
f o r e a c h ( s t r i n g s in aList)
{
// A r r a y L i s t
Console.WriteLine(s + " ( { } ) " , aList.IndexOf(s));
//
//
Stack
//
//
S t a c k s t a c k = new S t a c k ( ) ;
//
//
stack.Push("one");
stack.Push("two");
// "two","one"
stack.Push("three"); // "three","two","one"
Console.WriteLine("{0} :
",
stack.Count);
foreach
( s t r i n g s in stack)
{
Console.WriteLine(s) ;
}
(ma
15.
341
s t r i n g sval = ( s t r i n g ) s t a c k . P o p ( ) ; // "two","one"
Console.WriteLine("
:");
Console.WriteLine(sval);
Console.WriteLine(" :
{}",
stack.Peek());
//
//
Queue
//
//
Q u e u e q u e u e = new Q u e u e ( ) ;
//
queue.Enqueue("one");
queue.Enqueue("two");
queue.Enqueue("three"); // "one","two","three"
Console.WriteLine("{0}
:",
queue.Count);
foreach
( s t r i n g s in queue)
{
Console.WriteLine(s);
}
Console.WriteLine(" :
{}",
queue.Dequeue() ) ;
Console.WriteLine(" :
{}",
queue.Peek());
//
//
Hashtable
//
// H a s h t a b l e
()
H a s h t a b l e t a b l e = new H a s h t a b l e ( ) ;
S t u d e n t s t u d e n t l = new S t u d e n t ( " R a n d y " ) ;
S t u d e n t s t u d e n t 2 = new S t u d e n t ( " C h u c k " ) ;
/ / S t u d e n t ,
table.Add(studentl.Name,
studentl);
t a b l e . A d d ( s t u d e n t 2.Name,
student 2);
//
Console.WriteLine("{0}
: " ,
table.Count);
/ / , ,
// DictionaryEntry
foreach(DictionaryEntry de in table)
{
// D i c t i o n a r y E n t r y . V a l u e
// Student
Student stu =
(Student)de.Value;
Console.WriteLine(stu.Name);
/ /
Console.WriteLine(" <Enter> " +
" . . . " ) ;
Console.Read();
public
class
Student
342
V. !
private
s t r i n g name;
public S t u d e n t ( s t r i n g
{
t h i s.name = name;
name)
}
public
string
Name
{
get
r e t u r n name;
,
-, ,
.
- "System.Collections namespace"
.
, # ,
.
: .
, ,
. i n t ,
i n t , #
i n t .
, .
, ..
, .
, .
. #
O b j e c t , O b j e c t
, -, - (.
14, " ").
- (, b o o l , s t r u c t ) ,
(.
14, " ").
,
( )
A r r a y L i s t , .
ArrayList a L i s t = n e w A r r a y L i s t ( ) ;
// - , . . .
string m y S t r i n g =
( s t r i n g ) a L i s t [4] ; / /
15.
string
343
, A r r a y L i s t
. , ,
:
A r r a y L i s t a L i s t = new A r r
a L i s t . A d d ( " a s t r i n g " ) ; //
aList.Add(3);
//
aList.Add(aStudent);
//
ayList
string
int
Student
;
-- OK
-- OK
-- OK
A r r a y L i s t (
, ,
? S t u d e n t , string
.
is ( 12, "")
a s :
if(aList[i]
{
Student
is
Student)
aStudent
//
(Student)aList[i];
//
Student?
//
...
S t u d e n t a S t u d e n t = a L i s t [i] as
i f ( a S t u d e n t != n u l l )
{
/ / a S t u d e n t
Student;
/ / S t u d e n t
/ / , "as"
// n u l l
}
,
: , |
.
O b j e c t .
- ]
.
, .
i n t , 20 (
) .
.
, .
, ,
, , . . 15.2 -
344
V.
(
).
, ,
, I C o l l e c t i o n < T > I L i s t < T > .
"System.Collections.Generic
namespace" .
<>
<> ,
. , :
|ist<int> i n t L i s t =
new L i s t < i n t > ( ) ;
//
int
, L i s t < T > i n t ,
string S t u d e n t . , , <dummy> < m > . , U, V ..
List<T>
A r r a y L i s t
, L i s t < T > . G e n e r i c C o l l e c t i o n s . (
, ,
[ .) -.
// G e n e r i c C o l l e c t i o n s -
using S y s t e m ;
using S y s t e m . C o l l e c t i o n s ;
using System. Collections .Generic;
namespace GenericCollections
{
public c l a s s P r o g r a m
15.
345
public
static
void
Main(string[]
args)
{
// A r r a y L i s t
A r r a y L i s t a L i s t = new A r r a y L i s t ( ) ;
// L i s t < T > :
//
List<string> sList =
new L i s t < s t r i n g > ( ) ; / / s t r i n g
sList.AddC'one") ;
/ / !
sList.Add(3);
sList.Add(
n e w S t u d e n t ( " d u B o i s " ) ) ; / / !
// i n t
L i s t < i n t > i n t L i s t = new L i s t < i n t > ( ) ;
intList.Add(3);
/ /
intList.Add(4);
Console.WriteLine("
intList:");
f o r e a c h ( i n t i in i n t L i s t )
/ / f o r e a c h
//
{
// :
Console.WriteLine("int i = " + i.ToString());
}
// S t u d e n t
L i s t < S t u d e n t > s t u d e n t L i s t = new L i s t < S t u d e n t > ( ) ;
11
S t u d e n t s t u d e n t l = new S t u d e n t ( "Vigil ) ;
S t u d e n t s t u d e n t 2 = new S t u d e n t ( " F i n c h " ) ;
studentList.Add(studentl);
studentList.Add(student2);
S t u d e n t [ ] s t u d e n t s = new S t u d e n t [ ]
{ new S t u d e n t ( " M o x " ) , new S t u d e n t ( " F o x " )
};
studentList.AddRange(students);
//
// L i s t
Console.WriteLine(" studentList = { } " ,
studentList.Count);
/ / I n d e x O f ( )
Console.WriteLine("Student2 " +
studentList.IndexOf(student2));
s t r i n g n a m e = s t u d e n t L i s t [3] . N a m e ; / /
/ /
i f ( s t u d e n t L i s t . C o n t a i n s ( s t u d e n t l ) ) / / C o n t a i n s ( )
{
Console.WriteLine(studentl.Name + " ");
}
studentList.Sort();
// , S t u d e n t
// IComparable
s t u d e n t L i s t . I n s e r t ( 3 , new S t u d e n t ( " R o s s " ) ) ;
studentList.RemoveAt(3);
/ /
/ / n a m e
Console.WriteLine(" { } " , name);
Student[] moreStudents =
s t u d e n t L i s t . T o A r r a y ( ) ; //
346
V. !
/ /
C o n s o l e . W r i t e L i n e ( " <Enter> " +
"
. . . " ) . ;
Console.Read();
public c l a s s S t u d e n t :
IComparable
// .
// -
L i s t < T > : i n t , s t r i n g
Student. :
, ;
L i s t < T > f o r e a c h ,
;
, , ;
( , ICom
parable);
;
;
, ;
;
.
L i s t < T > .
, .
,
, , .
, #
, .
, ,
.
<>. , , ,
. ,
:
LinkedList<int> a L i s t = new L i n k e d L i s t < i n t > () ;
MyClass<int> a C l a s s = n e w M y C l a s s < i n t > () ;
toa 15.
347
: ,
. ,
, .
,
.
, .
- : "
S t u d e n t " , ,
.
, ,
, .
FedEx.
, .
: (
), ,
.
,
. , .
,
, .
. ,
, . .
,
, .
, !
,
.
.
# ,
. ,
, ?
- (wrapper class) :
class
Wrapper
//
PriorityQueue!
{
Q u e u e q u e u e H i g h = new Queue ( ) ;
Q u e u e q u e u e M e d i u m = new Queue ( ) ;
Queue q u e u e L o w = new Queue ( ) ;
/ / . . .
( )
.
Q u e u e , #, :
348
V.
E n q u e u e () ;
D e q u e u e () .
( ),
. ,
() .
.
E n q u e u e ( ) , ,
,
. D e q u e u e ( ) ,
. - P r i orityQueue.
:
// P r i o r i t y Q u e u e -
//
// ,
//
using S y s t e m ;
using S y s t e m . C o l l e c t i o n s . G e n e r i c ;
namespace P r i o r i t y Q u e u e
(
class
Program
{^
//Main - ,
//
static void M a i n ( s t r i n g [] args)
{
Console.WriteLine(" :");
P r i o r i t y Q u e u e < P a c k a g e > pq =
new P r i o r i t y Q u e u e < P a c k a g e > ( ) ;
Console.WriteLine(" " +
" (0 - 2 0 ) " +
" : " ) ;
Package p a c k ;
P a c k a g e F a c t o r y f a c t = new P a c k a g e F a c t o r y ( ) ;
// , 2 0
Random r a n d = n e w R a n d o m ( ) ;
// 0 - 2 0
int numToCreate = r a n d . N e x t ( 2 0 ) ;
Console.WriteLine("^ {} :
",
numToCreate);
f o r ( i n t i = 0; i < n u m T o C r e a t e ; i + + )
{
Console.Write("\t\treHepam^ "
" { } " , i ) ;
pack = f a c t . C r e a t e P a c k a g e ( ) ;
C o n s o l e . W r i t e L i n e ( " { } " ,
pack.Priority);
15.
349
pq.Enqueue(pack);
}
Console.WriteLine("
:");
int nTotal = pq.Count;
Console.WriteLine(" : { } " , nTotal);
Console.WriteLine(" " +
" : 0 - 2 0 : ") ;
i n t numToRemove = r a n d . N e x t ( 2 0 ) ;
C o n s o l e . W r i t e L i n e ( " ^ {0} " ,
numToRemove);
f o r ( i n t i = 0; i < numToRemove; i + + )
{
pack = pq.Dequeue();
if
(pack != n u l l )
Console.WriteLine("\\ "
" { } " ,
pack.Priority);
// " "
Console.WriteLine("
{0} " ,
nTotal - pq.Count);
/ /
Console.WriteLine(" <Enter> " +
" . . . ") ;
Console.Read();
/ / P r i o r i t y -
// 3,
. . .
enum P r i o r i t y // enum
{
Low, M e d i u m , H i g h
1,
2,
}
// I P r i o r i t i z a b l e - :
// , P r i o r i t y Q u e u e ,
/ /
interface
IPrioritizable
{
//
Priority Priority { get;
}
}
/ / P r i o r i t y Q u e u e - ;
// , ,
// I P r i o r i t i z a b l e
class PriorityQueue<T>
w h e r e T : I P r i o r i t i z a b l e // <--
//
350
//Queues - (!)
p r i v a t e Queue<T> q u e u e H i g h
= new Q u e u e < T > ( ) ;
p r i v a t e Queue<T> q u e u e M e d i u m = new Q u e u e < T > ( ) ;
p r i v a t e Queue<T> queueLow
= new Q u e u e < T > ( ) ;
V.
//Enqueue -
//
public void Enqueue(
item)
{
switch
(item.Priority)
//
{
//
IPrioritizable
case
Priority.High:
queueHigh.Enqueue(item);
break;
case
Priority.Low:
queueLow.Enqueue(item);
break;
case
Priority.Medium:
queueMedium.Enqueue(item);
break;
default:
t h r o w new
ArgumentOutOfRangeException(
item.Priority.ToString(),
" P r i o r i t y Q u e u e . E n q u e u e " ) ;
/ / D e q u e u e - T
//
public Dequeue()
{
//
Queue<T> q u e u e T o p = T o p Q u e u e ( ) ;
//
if ( q u e u e T o p != n u l l && q u e u e T o p . C o u n t > 0)
return
queueTop.Dequeue();
//
//
}
// , n u l l
(
/ / )
r e t u r n d e f a u l t ( ) ; //
//TopQueue -
p r i v a t e Queue<T> T o p Q u e u e ( )
{
if
( q u e u e H i g h . C o u n t > 0)
r e t u r n queueHigh;
if ( q u e u e M e d i u m . C o u n t > 0)
r e t u r n queueMedium;
if ( q u e u e L o w . C o u n t > 0)
r e t u r n queueLow;
:
- r e t u r n queueLow;
//IsEmpty public bool
,
IsEmpty()
//
//
//
//
//
//
//
?
?
?
{ ,
// t r u e ,
r e t u r n (queueHigh.Count
==0) &
15.
351
(queueMedium.Count = = 0 ) &
(queueLow.Count
== 0 ) ;
/ / C o u n t - ?
p u b l i c i n t Count //
{
//
get { r e t u r n queueHigh.Count
+
queueMedium.Count +
queueLow.Count; }
}
}
/ / P a c k a g e - ,
//
c l a s s Package : I P r i o r i t i z a b l e
{
private
Priority priority;
//
public
Package(Priority
priority)
{
this.priority
priority;
}
//Priority
//
public
get
Priority
{
Priority
return priority;
}
//
/ /
...
ToAddress,
FromAddress,
Insurance,
}
//
//
P a c k a g e F a c t o r y
-
}
P r i o r i t y Q u e u e
, .
P a c k a g e
. ,
P a c k a g e , , . ,
P a c k a g e -
,
( )
.
P a c k a g e :
I P r i o r i t i z a b l e , .
(enum) P r i o r
i t y . :
352
V.
//Priority -
// 3, . . .
e n u mP r i o r i t y
1,
2,
{
Low,
Medium,
High
IPrioritizable
, P r i o r i t y Q u e u e ,
( - , ).
, P a c k a g e
,
, .. , , P r i o r i t y Q u e u e ,
.
, I P r i o r i t i z a b l e :
// I P r i o r i t i z a b l e - :
// , P r i o r i t y Q u e u e ,
I I
interface
IPrioritizable
Priority
Priority
get;
{ g e t ; } ,
. , g e t ,
, P r i o r i t y
P r i o r i t y .
P a c k a g e
Priority:
public
get
Priority
{
Priority
return priority;
Main()
P r i o r i t y Q u e u e ,
, . M a i n ( ) :
//Main - ,
//
static v o i d Main ( s t r i n g [] a r g s )
{
Console.WriteLine(" :");
P r i o r i t y Q u e u e < P a c k a g e > pq =
new P r i o r i t y Q u e u e < P a c k a g e > ( ) ;
Console.WriteLine(" " +
15.
353
" (0 - 2 0 ) "
*
"
:");
Package pack;
P a c k a g e F a c t o r y f a c t = new P a c k a g e F a c t o r y ( ) ;
/ / , 2 0
R a n d o m r a n d = n e w R a n d o m () ;
// 0 - 2 0
i n t numToCreate = r a n d . N e x t ( 2 0 ) ;
C o n s o l e . W r i t e L i n e ( " ^ {0} :
",
numToCreate);
f o r ( i n t i = 0; i < n u m T o C r e a t e ; i + + )
{
Console.Write("\t\tTeHepauHH "
" { } " , i ) ;
pack = f a c t . C r e a t e P a c k a g e ( ) ;
C o n s o l e . W r i t e L i n e ( " { } " ,
pack.Priority);
pq.Enqueue(pack);
}
C o n s o l e . W r i t e L i n e ( " : ") ;
int nTotal = pq.Count;
Console.WriteLine(" : { } " , nTotal);
C o n s o l e . W r i t e L i n e ( " " +
" : 0-2 0 : " ) ;
i n t numToRemove = r a n d . N e x t ( 2 0 ) ;
C o n s o l e . W r i t e L i n e ( " ^ {0} " ,
numToRemove);
f o r ( i n t i = 0; i < numToRemove; i + + )
{
pack = pq.Dequeue();
if
(pack != n u l l )
{
Console.WriteL
i n e ( " \ t \ t
" {}",
pack.Priority);
}
}
// " "
Console.WriteLine(" {} ",
nTotal - pq.Count);
/ /
Console.WriteLine(" <Enter> " +
" . . . " ) ;
Console.Read();
, M a i n () ?
1 . P r i o r i t y Q u e u e P a c k a g e .
2. P a c k a g e F a c t o r y ,
. (
, .)
354
V.
3. R a n d o m
.NET, P a c k a g e F a c t o r y
P a c k a g e .
4. P r i o r i t y Q u e u e
p g . E n q u e u e ( p a c k ) .
5. ,
P r i o r i t y Q u e u e .
6. P r i o r i t y Q u e u e
.
<>? ,
, , .
, <>. ,
, P r i o r i t y Q u e u e P a c k a g e ,
, "".
P r i o r i t y Q u e u e
:
public
class
PriorityQueue
//Queues - (!)
1
private Queue<Package> queueHigh
= new Q u e u e < P a c k a g e > ( ) ;
p r i v a t e Q u e u e < P a c k a g e > q u e u e M e d i u m = new Q u e u e < P a c k a g e > ( ) ;
p r i v a t e Queue<Package> queueLow
= new Q u e u e < P a c k a g e > ( ) ;
//Enqueue - Package
//
public void Enqueue(Package item)
{
switch(item.Priority)
//
Package
{
case
Priority.High:
queueHigh.Enqueue(item);
break;
case
Priority.Low:
queueLow.Enqueue(item);
break;
case Priority.Medium:
queueMedium.Enqueue(item);
break;
}
}
// . . .
. ,
, P a c k a g e
<> (, , ).
355
, -
P r i o r i t y Q u e u e .
P r i o r i t y Q u e u e ,
Q u e u e < T > , .
P r i o r i t y Q u e u e , :
/ / P r i o r i t y Q u e u e - ;
// , ,
// I P r i o r i t i z a b l e
class
PriorityQueue<T>
where T : I P r i o r i t i z a b l e
{
//Queues - (!)
p r i v a t e Queue<T> q u e u e H i g h
= new Q u e u e < T > ( ) ;
p r i v a t e Queue<T> q u e u e M e d i u m = new Q u e u e < T > ( ) ;
p r i v a t e Queue<T> queueLow
= new Q u e u e < T > ( ) ;
/ / - . . .
- Q u e u e < > ,
Q u e u e < T > .
Enqueue()
E n q u e u e P r i o r i t y Q u e u e . ,
.
s w i t c h . -,
P r i o r i t y . H i g h , E n q u e u e ()
q u e u e H i g h . P r i o r i t y Q u e u e . E n q u e u e ( ) :
//
//
public
void
Enqueue(
item)
{
switch (item.Priority)
//
{
//
IPrioritizable
case
Priority.High:
queueHigh.Enqueue(item);
break;
case
Priority.Low:
queueLow.Enqueue(item);
break;
case
Priority.Medium:
queueMedium.Enqueue(item);
break;
default:
t h r o w new
ArgumentOutOfRangeException(
item.Priority.ToString(),
356
V.
"
PriorityQueue.Enqueue");
Dequeue()
Dequeue () .
.
Dequeue () T o p Q u e u e ( ) , .
D e q u e u e () D e q u e u e ()
, . D e q u e u e ( ) :
//Dequeue -
//
public D e q u e u e ()
//
Queue<T> q u e u e T o p = T o p Q u e u e ( ) ;
//
if
(queueTop
!= n u l l
&& q u e u e T o p . C o u n t
>
0)
{
return queueTop.Dequeue();
//
//
/ / , n u l l
// )
return d e f a u l t ( ) ;
}
, ,
, .. P r i o r i t y Q u e u e ?
? D e q u e u e () n u l l .
, , P r i o r i t y Q u e u e . D e q u e u e ()
, D e q u e u e n u l l .
n u l l ? d e f a u l t ( ) , . d e
fault () .
TopQueue()
D e q u e u e () T o p Q u e u e () ,
.
TopQueue () q u e u e H i g h C o u n t .
0, , T o p Q u e u e ()
( T o p Q u e u e ()
Queue<T>). q u e u e H i g h , T o p Q u e u e ()
q u e u e M e d i u m q u e u e L o w .
, ?
TopQueue () n u l l ,
. D e q u e u e () De
queue () , n u l l .
T o p Q u e u e ( ) :
15.
357
/ / T o p Q u e u e -
p r i v a t e Queue<T> T o p Q u e u e ( )
{
//
if ( q u e u e H i g h . C o u n t > 0)
// ?
r e t u r n queueHigh;
i f ( q u e u e M e d i u m . C o u n t > 0 ) //
// ?
r e t u r n queueMedium;
//
if ( q u e u e L o w . C o u n t > 0)
// ?
r e t u r n queueLow;
//
r e t u r n queueLow;
}
PriorityQueue
, P r i o r i t y Q u e u e , ,
( !).
Is E m p t y ()
C o u n t P r i o r i t y Q u e u e .
P r i o r i t y Q u e u e .
P r i o r i t y Q u e u e , , i n t , s t r i n g S t u d e n t , .. ,
, ,
,
I P r i o r i t i z a b l e . P r i o r i t y Q u e u e
, I P r i o r i t i z a b l e ,
.
D e q u e u e () P r i o r i t y Q u e u e null
. <>
n u l l , , , int
s t r i n g . D e q u e u e () .
P r i o r i t y Q u e u e
. ,
P r i o r i t y Q u e u e , I P r i o r i t i z a b l e ,
P a c k a g e . P a c k a g e I P r i o r i t i z a b l e
:
class
Package
IPrioritizable
P r i o r i t y I P r i o r i t i z a b l e .
,
, ,
. .
,
, , .
358
V.
I P r i o r i t i z a b l e
P r i o r i t y Q u e u e :
c l a s s P r i o r i t y Q u e u e < T > where T :
IPrioritizable
,
w h e r e . (enforcer), ,
I P r i o r i t i z a b l e , .. :
", , I P r i o r i
t i z a b l e , ".
, w h e r e
:
, (
);
, ,
.
s t r u c t , c l a s s
inew(). new () , s t r u c t
class "Generics, constraints" .
. ,
:
class MyClass<T>
where
class,
IPrioritizable,
new ()
T , -;
I P r i o r i t i z a b l e .
? (,
!) w h e r e :
c l a s s MyClass<T,
U>
: where T:
IPrioritizable,
where U:
new()
null T
, ,
"" . i n t 0 ( 0.0).
s t r i n g " " . b o o l f a l s e , ,
Package, n u l l .
P r i o r i t y Q u e u e , # ,
n u l l .
, D e q u e u e () P r i o r i t y Q u e u e
: D e q u e u e ( ) , .
, ""? P a c k a g e ,
n u l l .
( , , , n u l l ) .
15.
359
null
,
. De
q u e u e () :
return
default();
//
null
, ,
n u l l . P a c k a g e ,
,
n u l l . ,
, .
, P r i o r i t y Q u e u e
, -
P r i o r i t y Q u e u e - , -|
P r o g r a m m i n g T o A n l n t e r f .
.
. D e q u e u e () Priori
t y Q u e u e . ,
, .
.
, Swap ( ) ,
.
( . 6.2 6, "
"). , ,
Swap () r e f , -
, (
r e f 7, " ").
,
Swap ( ) .
// G e n e r i c M e t h o d - ,
//
using System;
namespace GenericMethod
{
class
Program
{
// Main - ;
// ,
// M a i n ( ) ;
s t a t i c void Main(string[] args)
{
360
V.
//
C o n s o l e . W r i t e L i n e ( " " +
" : \ " ) ;
C o n s o l e . W r i t e L i n e ( " \ t n p o B e p K a i n t " ) ;
i n t nOne = 1;
i n t nTwo = 2 ;
C o n s o l e . W r i t e L i n e ( " \ t \ t n e p e f l : n O n e = { 0 } , nTwo = {1} " ,
nOne, nTwo);
// i n t
Swap<int>(ref nOne,
ref n T w o ) ;
Swap<string>(ref sOne,
ref s T w o ) ;
C o n s o l e . W r i t e L i n e ( " \ t \ t r i o a n e : s O n e = { o } , sTwo =
sOne,
sTwo);
Console.WriteLine("\ " +
" " ) ;
Console . W r i t e L i n e ( " ^
Console.WriteLine("\t GenericClass.Swap " +
" i n t " ) ;
n O n e = 1;
nTwo = 2;
{l}",
int");
intClass.Swap(ref nOne,
{o},
nTwo =
{1}",
ref n T w o ) ;
C o n s o l e . W r i t e L i n e ( " \ t \ t n o ^ e : n O n e = { o } , nTwo =
nOne, nTwo);
C o n s o l e . W r i t e L i n e ( " \ t n p o B e p K a " +
" s t r i n g " ) ;
Console.WriteLine("\t GenericClass.Swap " +
" s t r i n g " ) ;
sOne = " o n e " ;
sTwo = " t w o " ;
{l}",
GenericClass<string> strClass =
new GenericClass<string>();
C o n s o l e . W r i t e L i n e ( " : sOne = { o } ,
sOne,
sTwo);
strClass.Swap(ref sOne,
sTwo
{l}",
ref sTwo);
C o n s o l e . W r i t e L i n e ( " : s O n e = { o } , sTwo =
sOne,
sTwo);
/ /
C o n s o l e . W r i t e L i n e ( " <Enter> " +
" . . . " ) ;
Console.Read();
15.
{l}",
361
} // e n d Main
/ / s t a t i c Swap -
p u b l i c s t a t i c v o i d Swap<T>(ref T l e f t s i d e ,
ref T rightSide)
{
T temp;
temp = l e f t s i d e ;
leftside = rightSide;
r i g h t S i d e = temp;
} // e n d P r o g r a m
/ / G e n e r i c C l a s s -
/ / Swap
class
GenericClass<T>
{
//Swap '- ,
/ / ; ,
/ / Swap<T>,
//
p u b l i c void Swap(ref T l e f t s i d e ,
ref T rightSide)
{
T temp;
temp = l e f t s i d e ;
l e f t s i d e = rightSide,r i g h t S i d e = temp;
Swap () (
) P r o g r a m , :
public
static
void
Swap<T>(ref
leftside,
ref
rightSide)
<>.
,
.
M a i n () Swap ( ) ,
i n t , s t r i n g (
). ( ,
):
Swap<int>(ref
nOne,
r e f nTwo);
// i n t
Swap<string>(ref
ref
362
sOne,
sTwo);
//
string
V.
Swap () i n t i n t
^ . , Swap () s t r i n g
s t r i n g . .
Swap ()
. .
G e n e r i c C l a s s ,
Swap ( ) , (
, , ):
class G e n e r i c C l a s s < T >
public
void
//
//
Swap(ref
<>
Swap()
leftside,
ref
rightSide)
...
Swap ()
Program ,
G e n e r i c C l a s s , Swap () ( ,
). Swap () <>,
Swap ( ) .
, Swap () . Swap () M a i n ( ) :
GenericClass<int> i n t C l a s s =
new G e n e r i c C l a s s < i n t > ( ) ;
//
//
i n t
//
GenericClass<string> s t r C l a s s =
new G e n e r i c C l a s s < s t r i n g > ( ) ;
//
//
s t r i n g
S t r C l a s s . Swap ( r e f
//
i n t C l a s s . Swap ( r e f
nOne,
sOne,
ref
ref
nTwo) ;
sTwo) ;
SwapO
SwapO
i n t s t r i n g .
S w a p () ,
.
,
,
^ P r i o r i t y Q u e u e .
:
static void Sort<T>(T[]
tArray) where T:
IComparable<T>
( ... }
, ,
I C o m p a r a b l e
.
15.
363
, ]
! (
14, " ".)
,
.
.
//
interface IDisplayable
/ /
interface
ICertifiable<T>
{
void Display();
void
Certify(T
criteria);
. ,
.
:
//
c l a s s MyClass :
/ /
c l a s s MyClass
IDisplayable
...
ICertifiable<MyCriteria>
...
:
//
c l a s s MyClass
IDisplayable
{
public
void
Display()
{
/ /
c l a s s MyClass
:
ICertifiable<MyCriteria>
//
{
public
void
Certify(MyCriteria
criteria)
{
, ,]
]
, M y C r i t e r i a .
, :
<>,
. , ,
. ,
,
L i s t < T > Swap<T> ( i t e m l , i t e m 2 ) .
V.
,
_ . .
I #, I C o m p a r a b l e < T > ,
/ -.
,
. ( , /, 14,
| " ".)
,
.
()
" "
P a c k a g e .
:
// P a c k a g e F a c t o r y
// P r i o r i t y Q u e u e , -
// P a c k a g e F a c t o r y - , ,
// ;
/ /
class P a c k a g e F a c t o r y
Random r a n d = n e w R a n d o m ( ) ; / /
//CreatePackage -
// ,
public Package C r e a t e P a c k a g e ( )
{
/ / .
// 0, 1 2 ( 3)
i n t nRand = r a n d . N e x t ( 3 ) ;
//
// ,
//
// s w i t c h
r e t u r n new P a c k a g e ( ( P r i o r i t y ) n R a n d ) ;
P a c k a g e F a c t o r y - (
, , P r o g r a m ) .
P a c k a g e F a c t o r y ,
Random r a n d . R a n d o m #,
. (
P a c k a g e F a c t o r y W i t h l t e r a t o r
-.)
PackageFactory
P a c k a g e
C r e a t e P a c k a g e ( ) , :
15.
365
PackageFactory fact
I P r i o r i t i z a b l e pack
=
=
new P a c k a g e F a c t o r y ( ) ;
f a c t . C r e a t e P a c k a g e ( ) ; / /
//
C r e a t e P a c k a g e ()
0 2 ,
P a c k a g e , (
P a c k a g e , I P r i o r i t i z a b l e ) .
!
(
P r i o r i t y Q u e u e ) .
, .
!
(dependency) .
, , ""
. , CBHL
. ( (decoupling)
, , , I P r i o r i t i z a b l e !
P a c k a g e . )
,
new, . |
, , .
, !
? , G e n e r i c l n t e r f a c e a
-.
// G e n e r i c l n t e r f -
//
u s i n g System;
using
System.Collections.Generic;
namespace G e n e r i c l n t e r f a c e
{
class
Program
{
static
void
Main(string[]
args)
{
C o n s o l e . W r i t e L i n e ( " " +
" Blob ");
GenericFactory<Blob> blobFact =
new G e n e r i c F a c t o r y < B l o b > ( ) ;
C o n s o l e . W r i t e L i n e ( " " +
" Students, " +
" " ) ;
GenericFactoryl<Student, string> stuFact =
new G e n e r i c F a c t o r y l < S t u d e n t ,
string>();
366
V. .
/ / .
// -
//
L i s t < B l o b > b L i s t = new L i s t < B l o b > ( ) ;
Student[]
s t u d e n t s = new S t u d e n t [ 1 0 ] ;
Console.WriteLine(" : " ) ;
f o r ( i n t i = 0; i < 10; i++)
{
C o n s o l e . W r i t e L i n e ( " ^ Blob - " +
" " +
".");
Blob b = b l o b F a c t . C r e a t e ( ) ;
b.name = "blob" + i . T o S t r i n g ( ) ;
bList.Add(b);
Console .WriteLine ("^ Student " +
" - " +
" " +
".");
s t r i n g sName = " s t u d e n t " + i . T o S t r i n g ( ) ;
students[i] = stuFact.Create(sName);
/ / . . . . -
}
/ / .
foreach(Blob b in b L i s t )
{
Console.WriteLine(b.ToString());
}
foreach(Student
in
students)
{ Console.WriteLine(s.ToString()
II
C o n s o l e . W r i t e L i n e ( " < E n t e r > '
" .
Console.Read();
}
// : S t u d e n t , B l o b (. - )
// B l o b - (
/ / , # ) .
// ( . - )
// S t u d e n t -
//
c l a s s S t u d e n t : ISettable<string> / /
{
// , . -
public Student()
{}
/ /
//
p u b l i c S t u d e n t ( s t r i n g name)
//
{
//
this.name
name;
}
// ISettable
public void SetParameter(string name)
367
this.name = name;
//
ToString()
}
//
// ISettable,
interface ISettable<U>
{
void SetParameter(U u ) ;
}
interface ISettable2<U, V>
{ void SetParameter(U u, V v ) ;
}
//
// ISettable
class GenericFactory<T> where T : new()
{
public T Create()
{
return new T ( ) ;
}
}
//
//
class GenericFactoryl<T, U> where T : ISettable<U>, new()
{
// U
//
public
Create(U )
{
t = new ( ) ;
t.SetParameter(); // ISettable,
// SetParameter()
return t;
}
}
//
//
//
-
,
G e n e r i c l n t e r f
.
,
, .
, .
,
368
V.
. -
, .
, , ,
, (
<> ):
/ / C r e a t e ( )
public C r e a t e ()
GenericFactory<T>
{
return new (); //
- ,
.
:
//
class
//
class
// T
class
//
class
//
class
MyBase
M y C l a s s < T > : w h e r e : MyBase
I M y l n t e r f a c e
MyClass<T>: where T: I M y l n t e r f a c e
MyClass<T>: where : c l a s s
-
MyClass<T>: where : s t r u c t
M y C l a s s < T > : w h e r e .- n e w ( )
. ,
, .. , .
,
#.
n e w ()
, .
n e w (U) n e w (U, V)
.
:
interface
ISettable<T>
where
new()
...
, ,
. , ,
C r e a t e () :
public C r e a t e ( U )
/ / ,
//
| {
t = new ( ) ; / / new ( )
...
/ / ? . . .
15.
369
:
t = n e w T ( u ) ; //
/ /
t = n e w T ( U ) ; //
, , ?
. ,
: I S e t t a b l e < T > :
interface
ISettable<U>
{
void SetParameter(U u ) ;
}
:
// ; U
c l a s s G e n e r i c F a c t o r y l < T , U> w h e r e T: I S e t t a b l e < U > , new()
'{
T, ,
I S e t t a b l e < U > c U , s t r i n g .
S t u d e n t ,
S t u d e n t , , s t r i n g ,
, S t u d e n t I S e t t a b l e < s t r i n g > :
//
class Student:
ISettable<string>
string
{
private
s t r i n g name;
p u b l i c S t u d e n t ( ) {}
p u b l i c S t u d e n t ( s t r i n g name)
{
SetParameter(name);
//
//
//
//
}
public void
{
this.name
SetParameter(string
name)
//
II
ISettable<string>
= name;
}
//
Student
S t u d e n t , ,
new:
students[0]
370
new
Student("Juan
Valdez");
V.
G e n e r i c F a c t o r y l < T , U>:
// = S t u d e n t , U = s t r i n g
GenericFactoryl<Student,
string> factl =
new G e n e r i c F a c t o r y l < S t u d e n t ,
string>();
C r e a t e ()
:
//
s t u d e n t s [1]
= factl. Create ( "Richard Corey");
G e n e r i c F a c t o r y l < T , U> . C r e a t e ( ) :
public T C r e a t e (U u)
{
T t = new T(); // ,
/ /
t . S e t P a r a m e t e r ( u ) ; // ,
/ / I S e t t a b l e
return t;
}
C r e a t e () ,
S e t P a r a m e t e r () t.
S t u d e n t ,
. ,
S e t P a r a m e t e r () - , S t u d e n t :
: I S e t t a b l e < s t r i n g > . , S t u d e n t
SetParameter ( ) .
? , ,
. , , ,
!
, ?
? , I S e t t a b l e < U > .
I S e t t a b l e 2 < U , V>.
I S e t t a b l e 3 < U , V, W> .. ,
. ,
- . ,
I S e t t a b l e < U , V , . . . > .
, I S e t t a b l e < U > ,
I S e t t a b l e 2 < U , V > .
S e t P a r a m e t e r (U ) , S e t P a r a m e t e r (U u , V v ) . (
, S e t P a r a m e t e r () .)
15.
371
VI
?
# ,
, .
. 16, "
",
#, ,
.
#
- ++.
17, " # ++",
, #
++.
16
> The name 'memberName' does not exist in the class or namespace 'className'
> Cannot implicitly convert type 'x' into 'y'
> 'className.memberName' is inaccessible due to its protection level
> Use of unassigned local variable 'n'
} Unable to copy the file 'programName.exe' to 'programName.exe.' The process c a n n o t . . .
> 'subclassName.methodName' hides inherited member 'baseclassName.methodName'. Use
the new keyword if hiding was intended.
> 'subclassName' : cannot inherit from sealed class 'baseclassName'
> 'className' does not implement interface member 'methodName'
> 'methodName' : not all code paths return a value
> j expected
#
. 10
. ,
. # ,
, ,
- . ,
# , ,
. , v a r i a b l e N a m e ,
memberName c l a s s N a m e . , #
, ,
.
,
, :
for(index =
0;
index
<
10;
index++)
{
//
}
i n d e x (. 3, " ", , ).
:
for(int
index
0;
index
<
10;
index++)
{
II
}
To (. 6, "
").
.
,
class
Student
{
public
public
s t r i n g sStudentName;
i n t nID;
}
class
MyClass
{
static
public
void
MyFunction(Student
s)
{
Console.WriteLine(" =
Console.WriteLine("Id
=
"
"
+ s.sStudentName);
+ s.nld);
}
, M y F u n c t i o n O nld,
n I D . , #
. n l d , n l d
, # . (
: ' c l a s s . m e m b e r N a m e '
d o e s n o t c o n t a i n a defini
1
t i o n for
v a r i a b l e N a m e ' .
3, " -".)
, ,
, ,
c l a s s MyClass
{
static
public
void
AverageInput()
376
i n t nSum = 0 ;
i n t nCount = 0;
while(true)
{
//
string s = Console.ReadLine();
int n = Int32.Parse(s);
/ / ,
VI.
if
(n
<
0)
{
break;
}
//
nSum + = n ;
nCount++;
}
/ /
Console.WriteLine(" " + nSum);
C o n s o l e . W r i t e L i n e ( " " + nSum / < n C o u n t ) ;
//
Console.WriteLine(" " + s ) ;
. , s
, , w h i l e (.
5, " ").
, :
int nAge = 1 0 ;
//
int n F a c t o r e d A g e = 2 . 0 * n A g e ;
, 2 . 0 d o u b l e .
2.0,
double. # d o u b l e
n F a c t o r e d A g e i n t ,
, .
, :
class M y C l a s s
static
public
float
FloatTimes2(float
f)
{
//
float f R e s u l t =
return
fResult;
2 . 0 * f;
, , f l o a t .
, 2.0 f l o a t , a d o u b l e , d o u b l e , f l o a t ,
d o u b l e . # d o u b l e f l o a t
- , ,
(. 3, " -").
. | F l o a t T i m e s 2 () :
16.
377
class
MyClass
{
static
public
float
FloatTimes2(float
f)
{
/ /
float fResult =2
*
f;
return
fResult;
}
2 i n t . i n t , f l o a t , f l o a t ,
f R e s u l t f l o a t .
"" . , char,
# c h a r int.
:
class
MyClass
{
static
public
void
SomeFunction()
} }
, #
c h a r , cl 2
i n t ( c h a r
). , c h a r
(. 3, "
").
( )
. , - :
c l a s s MyClass
{
static
public
float
FloatTimes2(float
f)
{
//
float fResult =
(float)(2.0 * f);
return
fResult;
}
}
2 . 0 * f d o u b l e , , '
, f l o a t ,
(. 3, " -").
, !
:
378
VI.
class
MyClass
{
static
public
float
FloatTimes2(float
f)
{
// 2.OF
f l o a t f R e s u l t = 2 . O F * f;
return
fResult;
float
}
2.0 f l o a t , d o u b l e ,
, f l o a t , f l o a t , f l o a t .
,
. ,
(. 11, ""),
,
public c l a s s M y C l a s s
{
public void SomeFunction()
{
Y o u r C l a s s uc = new Y o u r C l a s s ( ) ;
// MyClass
uc.nPrivateMember = 1;
}
}
public
class
"{
YourClass
//
internal
(
public
void
SomeFunction()
{
Y o u r C l a s s u c = new Y o u r C l a s s ( ) ;
// MyClass
uc . n P r i v a t e M e m b e r = 1;
'public
class
YourClass
(
int n P r i v a t e M e m b e r
16.
= 0 ;
//
379
, S o m e F u n c t i o n () public
, M y C l a s s
.
. ,
.
, ,
. ,
, out-,
,
public
class
MyClass
{
public
void
SomeFunction()
{
i n t n;
// , #
// ;
SomeOtherFunction(out n ) ;
}
public
void
SomeOtherFunction(out
int
n)
{
n =
1;
}
S o m e F u n c t i o n ()
, S o m e O t h e r F u n c t i o n d
S o m e O t h e r F u n c t i o n () out-, 6|
. ( 3, " -",
, o u t 7, " ".)
. ,
. ,
.
1. .
2. D e b u g ^ S t a r t without
Debugging, < E n t e r >
. . ., - ( , -
380
VI.
# ,
(. 13,
"). :
public
class
BaseClass
v
public v o i d F u n c t i o n ()
public
class
public
void
Subclass
Function()
BaseClass
//
here's
the
overload
{
}
public
public
class
void
MyClass
Test()
S u b c l a s s s b = new
sb.Function();
Subclass();
}
16.
381
T e s t () B a s e C l a s s . F u n c t i o n () on
s b , S u b c l a s s . F u n c t i o n ( ) .
.
.
n e w S u b c l a s s ,
:
public
new
class
public
Subclass
void
BaseClass
Function()
. |
:
public
class
BaseClass
{
public
virtual
void
Function()
public
class
Subclass
BaseClass
{
public
{
}
override
void
Function()
. 13, "".
, .
, , , ,
, .
. ,
(.
13, "").
382
VI.
. ,
. .
. ,
. .
.
:
interface
void
Me
aFunction(float
public c l a s s M y C l a s s
public
void
f);
Me
aFunction(double
d)
M y C l a s s a F u n c t i o n ( f l o a t ) .
aFunction ( d o u b l e ) ,
.
,
(. 14, " ").
,
,
.
# , ,
,
. .
i f , .
, , .
:
public
class
MyClass
'
16.
383
public
string
ConvertToString(int
n)
{
// i n t n s t r i n g
string s = n.ToString();
}
public
string
ConvertPositiveNumbers(int
n)
{
//
if
( n > 0)
{
string s = n.ToString();
return s;
Console.WriteLine("
{}
/ /
",
return
n);
>
}
C o n v e r t T o S t r i n g () s t r i n g ,
. , re
t u r n S;.
C o n v e r t P o s i t i v e N u m b e r s () s t r i n g -
i n t , . ,
, .
. n u l l , ""
(. 7, " ").
, #
. - ,
. ,
.
. , ,
.
Visual Studio 2005.
384
VI.
17
# ++
>
>
> -
> # ++
)
> ,
>
>
>
#
++. , , Microsoft Visual ++,
1 Windows.
, , Visual ++.
I # : , , 4 ++ . . , , , ,
( ) # ++.
# , Java Visual Basic.
# Java, ++ ,
Java ++,
. # Java ,
.
.
Visual Basic .. Visual Basic .NET, Visual Basic 6.0
, Visual Basic .NET .NET Framework, #, #
CIL (Common Intermediate Language, ) #. # Visual Basic ,
- # Visual Basic.
++ - , ,
-
++. -
, ,
...
# -
- .
?
.
C/C++ ,
.
.
. . , . #
.
( ),
.
. ,
; ,
, , . ++
, , .
.
.
, ,
. ,
. , " ", -'
,
, ""
, ,
<Ctrl>, <Alt> <Del>,
, . . .
# .
, # . #
- .
386
VI.
.
. , . ++
.
, ,
.
, , , ...
, ...
, , .
-.
# , ,
.
, .
# (. 15, "
") ++,
(. ,
.
, ,
-.
,
. ,
, .
,
. ""
.
, , Visual
Basic, ++ .NET, #.
( ++.
? ,
++, . .
, .
(Brandon Bray)
weblogs . a s p . n e t / b r a n b r a y / a r c h i v e / 2 0 0 3 / 1 1 / 1 9 / 5 1 0 2 3 . a s p x .
17, # ++
387
++ .
,
.
.
# .
S t u d e n t , # ,
, .
, .
, -...
! ,
. :
p u b l i c c l a s s Account
{
p r i v a t e double balance;
private
i n t numChecksProcessed;
p r i v a t e CheckBook checkBook;
public Account()
{
balance = 0.0;
n u m C h e c k s P r o c e s s e d = 0;
c h e c k B o o k = new C h e c k B o o k ( ) ;
}
}
-
? ++ , ; C#
? # :
public
c l a s s Account
{
p r i v a t e double balance = 0.0;
p r i v a t e i n t numChecksProcessed = 0;
p r i v a t e CheckBook c h e c k B o o k = new C h e c k B o o k ( ) ;
//
}
, , , , # , - . , ,
-. (
.)
388
VI.
++ . ,
.
, i n t - , a l o n g .
.
# . i n t 32 , a l o n g
64 , .
++ . ,
S l e e p e r S o f (-) B e d () S o f a
(). ,
. ,
.
# ,
. # ,
, .
90%
, ,
, . ,
P e r s i s t a b l e r e a d () w r i t e ( ) .
r e a d () w r i t e () ,
.
,
. ,
, r e a d () w r i t e ( ) :
interface
IPersistable
void r e a d ( ) ;
void write () ;
}
++
.
++ .
, -
17.
++
389
. ,
, , .
.
,
. ,
.
# , - .
- , (
; . 14, " ").
,
:
M y C l a s s m y O b j e c t = n e w M y C l a s s ,/ / " m y O b j e c t "
Console.WriteLine(myObj e c t . T o S t r i n g ( ) ) ;
i n t i = 5;
/ / i n t
Console.WriteLine(i.ToString());
/ / 5
Console.WriteLine(5.ToString());
i n t
M y C l a s s , 5. #.
390
VI.
VII
-.
, .
18
>
}
}
, , ( )
, .
, ,
. ,
. ,
,
,
.
, ,
# .
,
.
,
, ,
...
FactorialWithErr o r , , .
.
N N*(N-l)*(N-2)*... *1. , 4
4*3*2*1 = 24.
.
, .
// FactorialWithError -
// ,
using System;
namespace FactorialWithError
{
// MyMathFunctions -
//
public class MyMathFunctions
{
// Factorial -
//
public static double Factorial(double dValue)
{
// , 1
double dFactorial = 1.0;
// nValue, 1,
//
//
do
{
dFactorial *= dValue;
dValue -= 1.0;
} while(dValue > 1 ) ;
//
return dFactorial;
}
}
public class Program
{
public static void Main(string[] args)
{
//
// 6 -6
for (int i = 6; i > -6; I--)
{
//
Console.WriteLine("i = { } , = {l}",
i, MyMathFunctions.Factorial(i));
//
Console.WriteLine(" <Enter> " +
" .. . ") ;
Console.Read();
Factorial () -
1. ,
nValue, nValue
1. .
Factorial ()
. Main () ,
.
-6.
394 1, Main ()
VII.
i = 6, = 72 0
i = 5, = 12 0
i = 4, = 24
i = 3 , = 6
i = 2, = 2
1 = 1 , = 1
| = 0 , = 0
i = -1, = -1
i = -2, = -2
i = -3, = -3
i = -4, = -4
i - -5, = -5
<Enter> ...
, . -,
. -, ,
, . , .
Factorial ()
, Factorial
: . Factorial ()
.
Factorial ( ) , ? ,
, ,
.
, . ,
. ,
- 1 , , -2
.
. ,
,
, .
.
18.
395
jjjdWfeK
Facto
// FactorialErrorReturn -
// , , -
//
using System;
namespace FactorialErrorReturn
{
// MyMathFunctions -
//
public class MyMathFunctions
{
//
//
public const int NEGATIVE_NUMBER
= -1;
public const int NON_INTEGER_VALUE = -2;
// Factorial -
//
public static double Factorial(double dValue)
{
// :
if (dValue < 0)
{
return NEGATIVE NUMBER;
}
// :
int nValue = (int)dValue;
if (nValue != dValue)
{
return NON INTEGER VALUE;
// , ,
// 1
double dFactorial = 1.0;
// nValue, 1,
//
//
do
{
dFactorial *= dValue;
dValue -= 1.0;
} while(dValue > 1 ) ;
//
return dFactorial;
}
}
public class Program
{
public static void Main(stririg[] args)
396 {
VII.
//
// 6 -6
}
if (dFactorial == MyMathFunctions.NON INTEGER VALUE)
{
'
"
Console.WriteLine
("Factorial() ");
break;
//
Console.WriteLine("i = { } , = {l}",
i, MyMathFunctions.Factorial(i));
//
Console.WriteLine(" <Enter> " +
" ...");
Console.Read ();
Factorial () .
.
7
, 0 , .
, .
, , .
0.
Main () , Factorial ( ) ,
. -1 -2
, MyMathFunctions
. NEGATIVE_NUMBER - 1 , a NON_INTEGER_VALUE
2.
, , Main ( ) ,
.
,
.
MyMathClass. NEGATIVE_NUMBER.
, ,
.
" ".
"" , , 0
1. . .
18.
397
, const, .
enum MathErrors
NegativeNumber,
NonlntegerValue
Factorial () MathErrors, (
.NET Framework):
i = 6, = 720
i = 5, = 12 0
i = 4, = 24
i = 3, = 6
i = 2, = 2
1 = 1 , = 1
i = 0, = 0
Factorial()
<Enter> ...
( .)
FORTRAN. ?
? FORTRAN!
, . ,
.
, ,
. ,
398
VII.
.
, .
, ,
, .
,
,
, out,
. ,
,
.
. ,
Factorial -1,
. ,
, .
.
Factorial ( ) ,
. ,
: ",
, ", ,
.
, Factorial ()
,
,
, - . :
// SomeFuncO, ,
//
errRtn = SomeFunc () ;
if (errRtn == SF_ERRORl)
(
if (errRtn == SF ERROR2)
Console .WriteLine (" 2 SomeFuncO");
return My_ERROR 2;
}
// , ..
.errRtn = SomeOtherFunc;
if (errRtn == SOF_ERRORl)
{
Console.WriteLine(" 1 " +
"SomeOtherFunc () ") ;
return MY ERROR 3;
)
399
"SomeOtherFunc()");
return MY_ERR0R_4;
}
.
. -|
...
,
.
,
. , .
, .
# ,
. try, catch, throw fi
nally. .
(try) . ,
(throw) , (catch),
, , (finally) ,
:
// -
// ,
SomeOtherFunction();
// . . . - . . .
}
catch(Exception )
{
// ,
// try ,
// , , ,
// , try-
// , . Exception
//
' " ". . .
400
VII.
finally
{
// :
// , ..
// ,
// .
}
}
public void SomeOtherFunction()
{
I I . . . - . . .
11 . . . "" ""
// ,
// catch
throw new Exception(" ");
I I . . . . . .
Some Function () ,
try. , ( ,
, ...),
try-.
try catch ,
, - try- .
, catch (Exception):
catch
,
:
catch (MyException)
{
// ,
//
}
finally
, ,
. ""
, ..
++, catch
, # ,
Exception .
, - SomeOt h e r F u n c t i o n ( ) ... , -
18.
401
Exception throw
, !
try/catch ,
. ,
;
, .
FactorialException
.
// FactorialException -
// ,
//
using System;
namespace FactorialException
{
// MyMathFunctions -
//
public class MyMathFunctions
{
// Factorial -
//
public static double Factorial(int nValue)
{
// :
if (nValue < 0)
{
//
string s = String.Format(
" Factorial { } " ,
nValue);
throw new Exception(s); // ...
// ,
// 1
double dFactorial = 1.0;
// nValue, 1,
//
//
do
{
dFactorial *= nValue,} while(--nValue > 1 ) ;
//
return dFactorial;
402
VII.
args)
try // Factorial()
//
""
{
//
// 6 -6
for (int i = 6; i > -6; i--)
{
//
double dFactorial = MyMathFunctions.Factorial(i);
//
Console.WriteLine("i = { } , = {l}",
i, MyMathFunctions.Factorial(i));
catch(Exception e) // ...
{
Console.WriteLine(":");
Console.WriteLine(e.ToString());
}
//
Console.WriteLine(" <Enter> " +
" ...");
Console.Read();
"" Main ()
try-.
Main () try-,
Main () . ,
- , Main ( ) .
,
Windows,
.
catch Main () Exception ToString () ,
.
Exception.Message
,
. ToString ().
Factorial ()
, (
, ). , Factorial ()
403
,
, . Factorial ()
Exception,
.
:
i = 6,
i = 5,
i = 4,
i = 3,
i = 2,
i = 1,
i = 0,
:
=
=
=
=
=
=
=
720
120
24
6
2
1
0
System.Exception: Factorial -1
at Factorial(Int32 nValue) in
:\c#programs\Factorial\Program.cs:line 21
at FactorialException.Program.Main(String[] args) in
:\c#programs\Factorial\Program.cs:line 49
<Enter> ...
9
6 0 . -1 .
,
Factorial ( ) . ,
1.
.
, .
Factorial (int) 21 Program.cs
Factorial () Main (string [] ) 49
. , Main ()
, .
Visual Studio.
, .
, .
,
.
.
, ,
,
,
. (
, ,
.)
404
, , 0! = 1. . .
VII.
Exception, #,
.
, ,
. Exception
. ,
. ,
.
.
Exception ,
.
Exception, ApplicationException,
Exception, :
// CustomException - MyClass
//
public class CustomException : ApplicationException
{
private MyClass myobject;
//
//
CustomException(string sMsg, MyClass mo) : base(sMsg)
{
myobject = m o ;
' }
// ,
//
public MyClass MyCustomObject{ get
{return myobject;}}
}
CustomException
, MyClass. Ap
plicationException , ,
MyClass, .
.
CustomExcep
tion MyClass:
public class Program
{
public void SomeFunction()
{
try
{
// ...
SomeOtherFunctionO ;
// ... ...
}
catch(MyException me)
[{ 18.
405
// Exception
// ApplicationException
string s = m e . T o S t r i n g ;
// ,
//
MyClass mo = me.MyCustomObject;
// , MyClass
//
string s = mo.GetDescription();
public
void
SomeOtherFunctionO
{
// myobject
MyClass myobject = new M y C l a s s 0 ;
// ... myobject ..
throw new MyException(" MyClass",
myobj ect) ;
// ... ...
}
SomeFunction () SomeO-|
therFunction () try. SomeOtherFunction()
myobject. - SomeOtherFunction()
. Exception Ap
plicationException, SomeFunction
MyExcept ion,
, .
catch Main () ,
MyExcept ion. ,
Exception, , , ToString ( ) .
catch- , ,
MyClass, .
MyExcept ion.
catch :
public
void SomeFunction()
{
try
{
SomeOtherFunctionO ;
}
catch(MyException me)
{
}
}
406
VII.
SomeOtherFunction () Excep
tion - , MyException?
, ,
. , #
catch, .
catch , ,
. #
catch-, catch, :
public void SomeFunction ()
(
try
SomeOtherFunctionO ;
catch(MyException me) //
{
//
// MyException
} // catch-
//
catch(Exception )
//
//
//
SomeOtherFunction () Exception,
catch (MyException), Exception MyExcep
tion. catch (Exception).
, MyException, MyException:
class MySpecialException : MyException
{
// ... - ...
}
MyException MySpecialException.
(
. ApplicationException.)
catch-
. ,
:
public void SomeFunction ()
(
try
SomeOtherFunction();
}
18.
407
catch(Exception me) // - !
{
// MyException
}
catch(MyException )
{
// -
//
}
}
,
, #, catch-, ?
catch-? ?
:
// MyException - ,
//
// ,
using System;
namespace MyException
{
// MyClass
public class MyClass{}
// MyException - - MyClass
//
public class MyException : ApplicationException
private MyClass myobject;
public MyException(string sMsg, MyClass mo)
: base(sMsg)
myobject = mo,-
//
public MyClass MyCustomObject{ get {return myobject;}}
public class Program
{
// fl - -
public void f1(bool bExceptionType)
try
{
f2(bExceptionType);
catch(Exception e)
{
408
VII.
Console.WriteLine(" " +
" fl ") ;
Console.WriteLine(.Message);
}
}
// f2 - - MyException
public void f2(bool bExceptionType)
{
try
{
f3(bExceptionType);
catch(MyException
me)
{
Console.WriteLine(" MyException f 2 ( ) " ) ;
Console.WriteLine(me.Message);
// f3 - - He
public void f3(bool bExceptionType)
{ f4(bExceptionType);
// f4 - -
public void f4(bool bExceptionType)
{
//
MyClass mc = new MyClass ;
if(bExceptionType)
{
//
//
throw new MyException(" MyException " +
" f 4 ( ) " , mc) ;
}
throw new Exception(" f4 () " );
public
static void Main(string[] args)
{
// ...
Console.WriteLine(" " +
" ");
new P r o g r a m O . fl (false) ;
// ...
Console.WriteLine("\ " +
"MyException");
new P r o g r a m O .fl(true) ;
//
Console.WriteLine(" <Enter> " +
" ...");
Console.Read();
18.
409
Main () Program
f 1 ( ) , , , f 2 ( ) ,
f 3 ( ) , f 4 ( ) . f 4 ()
MyException,
Exception, bool.
Exception f3 ( ) . #
catch-, f 2 ( ) ,
MyException .
Exception,
. , f 1 () catch-,
.
Main () f 4 () MyEx
ception, 2 ( ) .
f 1 ( ) , f 2 ().
( Main () ,
, Main () .. Program? ,
? . 14, " ".)
:
fl()
f4()
' MyException
MyException f2()
MyException f4()
<Enter> ...
f3 ( ) , catch-,
. ,
catch-. ,
. ComputeX()
Factorial () ,
, Factorial () ?
ComputeXO
BrilliantLibrary
System. BrilliantLibrary ,
System,
.
410
VII.
, :
.
: , ,
.
,
, " "
. catch- ,
(-, ).
, , F (),
. - F ()
G (). , G (), , F ()
,
. F () catch-
( finally), . F()
,
.
"" .
(, , )
:
public void f 1 ()
{
try
{
f 2 () ;
}
// ...
catch(MyException me)
// ... ...
Console .WriteLine (" MyException fl ()")'';
// ...
//
throw new Exception(
", f l ( ) " ) ;
, .
Exception MyEx
ception
f 1 ().
,
, .
, fit)
.
( 18.
411
throw
,
:
public
void
fl()
{
try
{
f 2 () ;
}
// ...
catch(Exception e)
{
// ... ...
Console.WriteLine(" f l ( ) " ) ;
// ...
//
throw;
}
}
( ?).
,
.
( ) - ,
.
catch-? , :
;
;
(, ) .
?
.
, ,
, - ,
, .
.
, ,
..
. ,
. , ...
412
VII.
, ,
. ,
.
, , , .
.
,
.
, ,
. ,
.
,
Exception Appli
cationException:
// MyException -
// MyClass
public class MyException : ApplicationException
{
: base(sMsg)
{
myobject = m o ;
}
//
//
public MyClass MyObject{ get {return myobject;}}
BrilliantLibrary. ,
MyException,
, .
, BrilliantLibrary
MyException.
, Exception Applica
tionException, BrilliantLi
brary .
CustomException.
// CustomException - ,
//
using System;
namespace CustomException
{
public class CustomException : ApplicationException
{
private MathClass mathobject;
private string sMessage;
18.
413
MathClass mo)
}
override public string Message
{
get{return
string s = Message;
s += "\ ";
s += TargetSite.ToString(); // ,
//
return s;
}
}
// MathClass -
public class MathClass
{
private int nValueOfObject;
private string sObjectDescription;
public MathClass(string sDescription,
int nValue)
{
nValueOfObject = nValue;
sObjectDescription = sDescription;
public int Value {get {return nValueOfObject;}}
// Message -
// MathClass
public string Message
{
get
{
return String.Format("({0} = { l } ) " ,
sObj ectDescription,
nValueOfObject);
}
}
// ToString -
// Message Message
//
override public string ToString()
{
}
// 1/x
public double Inverse()
414
VII.
{
if
{
(nValueOfObject == 0)
throw new CustomException(" 0",
this);
}
return 1.0 /
(double)nValueOfObject;
args)
try
{
// take the inverse of 0
MathClass mathObject = new MathClass("Value", 0 ) ;
Console.WriteLine(" d.Value { } " ,
mathObj ect.Inverse() ) ;
catch(Exception e)
Console.WriteLine(
"\ :\n{0}",
e.ToString());
//
Console.WriteLine(" <Enter> " +
" .. . ") ;
Console.Read();
}
CustomException . ,
MyException .
Message,
, ,
ToString ( ) , .
, ,
Exception, .
.
Main () MathClass 0, . ,
, 0,
, .
Intel 1.0/0.0: .
,
, .
NaN (Not_a_Number, ).
( 18.
415
Inverse () .
CustomException,
.
Main () , . " ", , ,
" ". Main ()
, - , ToString ().
: T o S t r i n g ()
Object.
14, " ". , ,
ToString () .
. ,
ToString ( ) , .
GetString ( ) ,
; . ,
Student. ToString () .
#
ToString () . , ToString ()
, ,
, , .
Cus
tomException, CustomException. ToString ().
Message () Excep
tion, ,
.
Message MathClass
ToString ( ) . MathClass.ToString()
, .
, .
ToString () ,
.
,
. (
) .
CustomException :
:
< 0>, (Value = 0)
CustomException.MathClass
Double Inverse()
<Enter> ...
416
VII.
: " :"
Main ( ) . " < 0>, < ~ ~ > "
CustomException. Value = 0 MathClass.
, Double Inverse ( ) ,
CustomException. ,
.
18.
417
19
>
>
>
# .
. ,
- , .
, # .
, .
, ,
, .
# :
. ,
.
, (assemblies).
. , 11, "",
,
. .
.
.
"" ""
.
.
, , ,
, ,
, -
, ...
.
Pro
gram, s . ,
, , .
.
.
, ResAgentlnterf . cs, GateAgentlnterf . cs, Res-j
,
. ,
. ,
, .
, .
(solution). (
FileRead FileWrite, ,
.)
Visual # Visual Studio Solution Explorer
# Visual Studio
2005. Solution Explorer 21, "
Visual Studio".
420
VII.
,
.
(namespace). ( .)
,
, - ..
programName..
, ( )
, .
. . -, . DLL-, . ,
, .
. -, Visual
Studio, (startup project).
Debug^Start Debugging (F5) or Debugs
Start Without Debugging (<Ctrl+F5>).
Solution Explorer
Set as Startup Project.
Solution Explorer . Solution Explorer
21, " Visual Studio".
. , ,
, ,
, .
,
, .DLL, "DLL" (DLL dynamic link library
( ) , ,
).
taa 19.
421
, ,
, . (
. -, ,
.
. DLL, .
, , !)
,
.
,
" ", ,
. , ,
, MathRoutines.
( )
. ,
. , Point.cs =
Point, T h r e e D S p a c e . c s ThreeDSpace,
. Point.cs, ThreeD
Space . cs # MathRoutines (,
, MathRoutines).
. (
, .)
.
, .
, , ,
MathRoutines,
.
, MathRoutines.
. ,
Convert,
.
.
FilelO TranslationLibrary,
: FilelO. Convert, ,
TranslationLibrary.Convert.
namespace,
.
,
namespace
MyStuff
422
VII.
class MyClass {}
class UrClass {}
}
MyClass UrClass
MyStuff.
, ,
.
. Namespace2,
Namespacel, :
namespace Namespacel
{
// Namespacel
namespace Namespace2
{
// Namespace2
public class Class2
public void AMethod() { }
}
}
}
Class2 Namespace2 -
Namespacel :
Namespacel.Namespace2.Class2.AMethod();
,
, public.
: MyCompany .MathRoutines. (, .
.
- CMSCo.MathRoutines Sphar.MathRoutines.)
,
, , MathRoutines.
" " ,
, System. Data
, Data, System.
,
System. 10, System. Data System. Text.
Visual Studio Application Wizard
, , .
, Application Wizard.
, AlignOutput AlignOutput.
Program.cs,
. Program.cs , :
AlignOutput. ( ,
.
Properties .)
19.
423
, #
. .
,
, 11,
"" ( , public, private
protected, internal protected internal).
.
, . .
NamespaceA
NamespaceB, .
NamespaceB
NamespaceA. , .
?
, Classl NamespaceA
NamespaceB. Class2 . AMethod ( ) , .
1. Class2 NamespaceB
Classl? ,
.
2. "", " " Class2
AMethod () Classl ? " "
Classl. ,
11, "".
Class2 , Classl,
(publ ic) Classl .
, Class2 internal.
public, protected, internal private.
,
internal
11,
Class2
. protected
.
"".
"" , Classl
Class2.
.
# NamespaceB
NamespaceA.
424
VII.
Name
spaceB NamespaceA.
, ,
:
System.Console.WriteLine("my s t r i n g " ) ;
NamespaceA
using
NamespaceB:
using NamespaceB;
using.
using
.
,
. ,
using :
namespace MathRoutines
{
class Sort
{
Paint
{
public class
PaintColor
int nBlue)
{}
}
namespace MathRoutines
{
public class Test
{
static public void Main(string[]
args)
// Sort
// , ,
//
Sort obj = new S o r t ( ) ;
obj.SomeFunction();
//
// ,
//
19.
425
Sort Test
MathRoutines, .
( ).
Sort Test
#, .
Test. Main () Sort
, ,
Main () Paint
PaintColor, Paint, PaintColor. StaticPaint ( ) .
.
,
black. Paint ( ) , black
using
. #
using. using
, #
. - :
namespace Paint
{
public class PaintColor
{
public PaintColor(int nRed, int nGreen, int nBlue)
public void Paint() {}
public static void StaticPaint() {}
{}
namespace MathRoutines
{
// Paint ,
//
using Paint;
public class Test
{
static public void Main(string[] args)
{
//
//
// ,
// "using"
PaintColor black = new PaintColor(0, 0, 0 ) ;
426
VII.
black.Paint();
PaintColor.StaticPaint();
using : "
, ,
, ". ,
( ,
), " ".
using System,-.
,
, WriteLine ().
NamespaceUse
using ,
.
// NamespaceUse -
//
using System; //
// String
// Console
namespace MainCode
{
using LibraryCodel; // MainCode
public class Classl
{
public void AMethod()
{
Console.WriteLine("MainCode.Classl.AMethod() ") ;
}
// M a i n ( ) :
static void Main(string[] args)
// ,
// Main,
Classl cl = new C l a s s l ; // MainCode.Classl
cl.AMethod();
//
// Main() !
// LibraryCodel.Classl
//
// MainCode.Classl, , ,
// using,
//
Classl 2 = new Classl(),
2.AMethod();
//
// .
19.
427
//
// using,
// Classl
LibraryCodel.Classl = new LibraryCodel.Classl();
c3 .AMethod () ,// LibraryCodel.Class2
// ,
// using
// ; # Class2
Class2 4 = new C l a s s 2 ( ) ;
4.AMethod();
// LibraryCode2.Classl
// , -
// using LibraryCode2,
// , Classl
// :
// , LibraryCode2.Classl internal,
// public,
//
LibraryCode2.Classl 5 = new LibraryCode2.Classl();
5.AMethod();
//
Console.WriteLine(" <Enter> " +
" ...");
Console.Read();
}
namespace LibraryCodel
{
public class Classl
// Classl
{
//
public void AMethod() //
{
//
Console.WriteLine("LibraryCodel.Classl.AMethod()");
}
}
public class Class2
// ,
{
//
public void AMethod()
Console.WriteLine("LibraryCodel.Class2.AMethod()");
namespace LibraryCode2
{
class Classl
{
public void AMethod()
// ,
// :
// internal
Console.WriteLine("LibraryCode2.Classl.AMethod()");
}
428
VII.
: MainCode,
Classl, Main()
AMethod ( ) . LibraryCodel :
Classl Classl MainCode, Class2
. LibraryCode2 , Classl,
Classl ,
.
AMethod ().
Main () MainCode . Classl MainCode. Classl (- M a i n O ) , LibraryCodel. Classl, Library
Codel .Class2 LibraryCode2 .Classl.
AMethod () .
. :
MainCode.Classl.AMethod()
MainCode.Classl.AMethod()
LibraryCodel.Classl.AMethod()
LibraryCodel.Class2.AMethod()
LibraryCode2.Classl.AMethod()
<Enter>
...
M a i n O .
, using,
. MainCode using
LibraryCodel, M a i n O MainCode. Classl
LibraryCodel. Class2
.
LibraryCodel. Classl
. , MainCode. Classl,
LibraryCodel.
Classl MainCode.
, .
using LibraryCodel
Main () Library
Codel . Classl, .
using,
LibraryCodel. Classl.
Main () Library2 .
Classl , Library2
, MainCode. ,
Library2 .Classl ,
public, internal.
(
private).
.
19.
429
using, ,
, , , .
Visual Studio 2005
Class Library New Project.
,
.
Visual # Express,
.
, Microsoft Visual # Express,
, .
430
VII.
, ,
XML.
4. Edit"=>Find , :
<OutputType>Exe</OutputType>
Windows, ,
<OutputType>WinExe</Output>
.
5. ( WinExe) Library:
<OutputType>Library</OutputType >
!
6. Visual # Express.
7. . ,
- Main ().
, . DLL-, . -.
,
, .
ClassLibrary ,
(
, ).
// ClassLibrary -
// -
// ClassLibrary.cs
using System;
namespace ClassLibrary
{
public
class MyLibrary
public
void
LibraryFunctionl()
{
Console.WriteLine("
LibraryFunctionl()");
}
public
int LibraryFunction2(int
input)
{
Console.Write(" LibraryFunction2(), " +
" { } , i n p u t ) ;
return input; //
}
}
//
// Program.cs
using System;
19.
431
using ClassLibrary;
namespace
{
class
//
/ /
ClassLibraryDriver
Program
{
static void Main(string[]
args)
//
//
MyLibrary ml = new M y L i b r a r y ( ) ;
ml.LibraryFunctionl();
//
int nResult = MyLibrary.LibraryFunction2(27);
Console . WriteLine (nResu-lt. ToString () ) ;
//
Console.WriteLine(" <Enter> " +
" .. . ") ;
Console.Read();
-, ,
:
LibraryFunctionl()
LibraryFunction2(), 27
27
<Enter> ...
.
.
, , ,
. ClassLi
brary .
1. Solution Ex
plorer ClassLibrary Add : New Project.
,
.
2. New Project C o n s o l e Application
ClassLibraryDriver .
3. New Project,
, B r o w s e L o c a t i o n .
, ,
O p e n .
432
VII.
, ,
. ClassLibraryDriver
, ClassLibrary, ClassLi
brary. ClassLibrary
.
,
ClassLibrary.
.
4. , New
Project .
Visual Studio Wizard .
Solution Explorer : ClassLibrary ClassLi
braryDriver.
5. ClassLibraryDriver
Set as Startup Project.
#, Main ()
. , .
6. Program, cs ClassLibraryDriver
Main () :
MyLibrary myLib = new M y L i b r a r y ( ) ;
//
//
//
myLib.LibraryFunctionl();
//
int result = MyLibrary.LibraryFunction2();
, ,
. ,
, WriteLine () Con
sole .NET Framework. (Console
System mscorlib . dll.)
.
ClassLibrary.
7. P r o j e c t s Add Reference.
8. Add Reference Projects.
ClassLibrary .
using ClassLi
brary Program, cs ClassLibraryDriver,
.
, ,
using
.DLL-, ,
.
.NET Framework.
19.
433
. ,
, ,
.
System.IO.
- FileStream.
. open
. ,
.
-: - ?
-
. read ( ) ,
,
-.
# System.IO -.
- read ()
, - ,
.
, , .
- .
,
.
, , , ,
, .
-
, .
# .
FileStream. FileStream ,
FileStream -.
F i l e S t r e a m ,
-. ,
90% . ,
. # .
FileStream .
, . , System.0 ,
, , FileStream
.
BinaryReader/BinaryWriter ,
-: ReadChar (), Write-
434
VII.
):
Strin-
gReader/StringWriter StreamReader/StreamWriter.
StringReader/StringWriter ,
. ,
( [ ] ) , foreach
String Split (), Concatenate () IndexOf ( ) .
, .
, .
StreamReader StreamWriter, .
StreamReader/StreamWriter
. , StreamWriter WriteLine (),
Console. StreamReader
ReadLine () ReadToEnd ( ) ,
string ( StringReader, foreach ).
TextReader/TextWriter ,
, StreamReader/StreamWriter.
FileWrite FileRead,
-.
Stream Writer
.
.
(, Student,
).
,
. StreamWriter StreamReader
.
ASCII, ANSI-.
,
. ANSI
19.
435
, ,
, ,
Unicode, ANSI-
, ,
. Unicode ,
# UTF8.
FileWrite
// FileWrite -
using System;
using System.10; //
namespace FileWrite
{
- public class Program
{
public static void Main(string []
args)
// while
//
// ,
StreamWriter sw = null;
string sFileName = "";
while(true)
try
{
// ( Enter
// )
Console.Write(" "
+ "( ):");
sFileName = Console.ReadLine();
if (sFileName.Length == 0)
{
//
break;
}
// ;
// , :
// FileMode.CreateNew - ,
//
// ; FileMode.Append
//
// ; FileMode.Create
//
// .
// FileAccess: FileAccess.Read,
// FileAccess.Write, FileAccess.ReadWrite
FileStream fs = File.Open(sFileName,
FileMode.CreateNew,
FileAccess.Write);
// UTF8- (
436
VII.
// UTF8,
// )
sw = new StreamWriter(fs,
System.Text.Encoding.UTF8);
// ,
// FileStream
Console.WriteLine(" " +
"( ) " ) ;
while(true)
{
// ;
// ,
string slnput = Console.ReadLine();
if (slnput.Length == 0)
{
break;
}
//
sw.WriteLine(slnput);
}
//
sw.Close();
sw = null; //
//
}
catch(IOException fe)
{
//
//
//
string sDir = Directory.GetCurrentDirectory();
string s = Path.Combine(sDir, sFileName);
Console.WriteLine(" {}", s ) ;
//
//
Console.WriteLine(fe.Message);
//
Console.WriteLine(" <Enter> " +
" ...");
Console.Read();
FileWrite Sys
tem. 10 System. System. 10 ,
.
, Sys tern.Text. StringBuilder,
,
. ,
+ .
19.
437
Main ( ) , while
try-. ,
. ( , StreamReader,
, .)
- try- ,
.
, -
, .
while .
,
-. ,
, ,
, .
break try-,
. ;
break , ,
FileWrite
. break,
.
:
FileStream fs = File.Open(sFileName, FileMode.CreateNew,
FileAccess.Write);
/ / ...
sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
FileStream,
, . FileStream
.
: , .
filename. txt ,
( FileWrite \bin\Debugn
; , , . -).
,
\directory\
filename.txt, .
,
(,
\\machine\
directory\filename.txt), ,
. ,
.
: ,
.
(CreateNew), (Append) (Create). Creat
eNew , IOException,
. Create , ,
, . , Append ,
438
VII.
, ,
.
: , .
FileStream ,
, ,
. , ,
,
. ,
, , .
""
FileStream StreamWriter. StreamWriter
FileStream, .
""
StreamWriter ""
( ) FileStream
FileStream, . StreamWriter
FileStream.
12, "", .
StreamWriter FileStream.
. UTF8.
. , StreamWriter
. StreamReader
.
.
FileWrite , .
,
, WriteLine () StreamWriter.
StreamWriter.WriteLine()
Console.WriteLine()
, .
19.
439
, a Directory
).
. , : \user\directory\!
text. txt, : \user\directory.
Combine () , ,
c:\test.txt Path
Path. Combine () ,
, - (\)
. ( Windows - \.
,
, Path.DirectorySeparatorChar.
.NET Framework ,
# ,
.)
w h i l e try-,
catch,
.
(
).
( ):TestFilel.txt
(, )
-
. . .
( ):TestFilel.txt
:\C#Programs\FileWrite\bin\Debug\TestFilel.txt
The file already exists.
( ):TestFile2.txt
( )
-
TestFile2.
( ) :
<Enter> ...
, TestFilel. txt.
TestFilel.txt
The
file
already exists ( ).
, .
TestFile2 . txt, .
StreamReader
, ,
.
, ,
FileWrite .
440
VII.
// FileRead -
//
using System;
using System. 10;
namespace FileRead
//
StreamReader sr;
string sFileName = " ";
// ,
// (
//
// <Ctrl+C>)
while(true)
{
try
{
//
Console.Write(" : " ) ;
sFileName = Console.ReadLine();
// ,
// ,
//
if (sFileName.Length == 0)
{
throw new
IOException(" " ) ;
}
// ;
// -
FileStream fs = File.Open(sFileName,
F i1eMode.Open,
FileAccess.Read);
// StreamReader -
//
// (
// )
sr = new StreamReader(fs, t r u e ) ;
break;
}
//
catch(IOException fe)
{
Console.WriteLine("{0}\n\n", fe.Message);
}
}
//
Console.WriteLine("\ :") ;
try
19.
441
{
//
while(true)
{
//
string slnput = sr.ReadLine() ;
// ,
if (slnput == null)
{
break;
}
//
Console.WriteLine(slnput);
}
catch(IOException
fe)
//
Console.Write(fe.Message);
//
try
sr.Close();
catch {}
sr = null;
//
Console.WriteLine(" <Enter> " +
" .. . ") ;
Console.Read();
}
, ,
FileRead, \bin\Debug FileRead (
\bin\Debug FileWrite).
FileRead, \bin\Debug
, .
TestFilel.txt, !
FileWrite.
FileRead
. .
, .
, .
, .
, Fil
eRead, ,
. , .
<Ctrl+C> .
442
VII.
while, FileWrite. .
, :
. ,
FileStream . File.Open() ,
FileWrite.
.
. FileMode. Open :
" , , , ".
OpenNew,
. (
?), , ,
?
FileStream.
Write ReadWrite. (
FileRead Write, ?)
f s FileStream sr
StreamReader .
try- while. try-
.
, ,
, .
, break
.
FileRead FileWrite
.
try-, FileWrite,
t ry-.
try- , ,
.
, FileRead
ReadLine ( ) .
Console .WriteLine ,
.
, ReadLine () null. ,
, .
, Close () try. catch (
catch (Exception)). , Close ,
. catch ,
. ,
Close ( ) ,
. Close ()
19.
443
finally catch ,
, .
catch .
try- , ,
- .
.
:
:TestFilex.txt
Could not find file "C:\C#Programs\FileRead\TestFilex.txt".
:TestFilel.txt
:
-
...
<Enter> ...
, , TestFilel. txt, FileWrite ( !
\FileRead\bin\debug?).
444
VII.
20
...
> LinkedLi st
> LinkedList
>
> #
,
. , .
, # .
15, " ",
, , 19, " ".
, ()
, . ,
( ,
15, " ", )
.
, .
FileRead FileWrite
19, " ".
-.
LoopThroughFiles
,
. (
, . -
.)
,
.
.
, , ,
<Ctrl+C>.
.
// LoopThroughFiles - ,
// .
// ,
//
using System;
using System. 10,namespace LoopThroughFiles
{
public class Program
{
public static void Main(string [] args)
{
// ...
string sDirectoryName;
if (args.Length == 0)
{
// ... ...
sDirectoryName = Directory.GetCurrentDirectory();
}
else
{
// ... ,
//
//
sDirectoryName = a r g s [ 0 ] ;
}
Console.WriteLine(sDirectoryName);
//
FileInfo[] files = GetFileList(sDirectoryName);
//
//
foreach(Filelnf file in files)
{
//
Console.WriteLine("\\
{}:",
file.FullName);
//
DumpHex(file);
//
Console.WriteLine("\ <Enter> " +
" " ) ;
Console.ReadLine() ;
}
// !
Console.WriteLine("\ " ) ;
//
Console.WriteLine(" <Enter> " +
" ...");
Console.Read();
}
// GetFileList -
//
public static FileInfo[]
GetFileList(string sDirectoryName)
446
VII.
{
//
Filelnfot] files = new Filelnf [Obtry
{
//
Directorylnf di =
new Directorylnf(sDirectoryName);
//
files = di.GetFiles();
}
catch(Exception e)
{
Console .WriteLine ( " \"" + sDirectoryName +
" \" ") ,Console.WriteLine(e.Message);
return files;
}
// DumpHex -
//
public static void DumpHex(Filelnf file)
{
//
FileStream fs;
try
{
string sFileName = file.FullName;
fs = new FileStream(sFileName, FileMode.Open,
FileAccess.Read);
// Filelnf
// file.OpenRead(), FileStream
// ,
}
catch(Exception )
{
Console.WriteLine("\ \"" +
file.FullName + " \ " " ) ;
Console.WriteLine(.Message);
return;
}
//
for(int nLine = 1; true; nLine++)
{
// 10 ( ,
// ) ; ,
//
byte[] buffer = new byte [10] ;
int numBytes = fs.Read(buffer, 0, buffer.Length);
if (numBytes == 0)
return;
, 447
20. //
//
Console.Write("{0:D3} - ", n L i n e ) ;
DumpBuffer(buffer, numBytes),// 2 0 ,
//
if ((nLine % 20) == 0)
{
Console.WriteLine(" <Enter> " +
" 20 " ) ;
Console.ReadLine();
}
}
// DumpBuffer -
//
public static void DumpBuffer(byte[] buffer,
int numBytes)
for(int index = 0;
index++)
byte b = buffer[index] ,
Console.Write("{0:X2}, ", b) ,Console.WriteLine();
}
}
}
, apryl
. - [
( , ):
loopthroughfiles
c:\randy\temp
, .!
( -j
. " ".)
(0 1),
.
0-9 , , , D, , F = 10, = 11,
F = 15. (
): OxD = 13. 0x10 = 16: 1*16 + 0 * 1 . 02 = 42: 2*16 + *1 ( *1 =
10*1). , : F , .
, ,
.
FileRead FileWrite
,
. , ,
.
448
VII.
0),
Visual
LoopThroughFiles
. (args. Length
Directory. GetCurrentDirectory ( ) .
Studio, ,
bin\Debug LoopThroughFiles.
Directory
, Filelnf ,
.
GetFileList ( ) . Filelnf .
Filelnf , (
, FullName, Name),
. Main ()
foreach. DumpHex ()
.
DumpHex () .
GetFileList () Filelnf ,
.
Get...List ():
, .
. ,
PriorityQueue 15,
" ",
(
, ). GetFileList (}
, .
GetFileList () Directorylnf .
, Directorylnf ,
Filelnf . Directorylnf ,
Filelnf ,
Filelnf.
, GetFileList () ,
, try-. catch
(, , , ..
).
DumpHex () - .
DumpHex () . Filelnf
, . DumpHex ()
, . FileStream
. catch ,
FileStream .
DumpHex () 10
. 20
<Enter>.
20.
449
25 (,
, ).
, 20 ,
.
(%) .
(nLine%20) = = 0 nLine, 20, 40, 60, 80.... ,
. , ,
.
DumpBuf fer ()
2. 2 -
, " " (. 9, " # " ) .
byte 0 255, O x F F ..
.
20 output.txt.
...
:\C#ProgramsVi\holdtank\Test2\bin\output.txt:
001 - 53, 74, 72, 65, 61, 6D, 20, 28, 70, 72,
002 - 6F, 74, 65, 63, 74, 65, 64, 29, 0D, OA,
003 - 20, 20, 46, 69, 6C, 65, 53, 74, 72, 65,
004 - 61, 6D, 28, 73, 74, 72, 69, 6E, 67, 2C,
005 - 20, 46, 69, 6C, 65, 4D, 6F, 64, 65, 2C,
006 - 20, 46, 69, 6C, 65, 41, 63, 63, 65, 73,
007 - 73, 29, OD, OA, 20, 20, 4D, 65, 6D, 6F,
008 - 72, 79, 53, 74, 72, 65, 61, 6D, 28, 29,
009 - 3B, OD, OA, 20, 20, 4E, 65, 74, 77, 6F,
010 - 72, 6B, 53, 74, 72, 65, 61, 6D, OD, OA,
011 - 20, 20, 42, 75, 66, 66, 65, 72, 53, 74,
012 - 72, 65, 61, 6D, 20, 2D, 20, 62, 75, 66,
013 - 66, 65, 72, 73, 20, 61, 6E, 20, 65, 78,
014 - 69, 73, 74, 69, 6E, 67, 20, 73, 74, 72,
015 - 65, 61, 6D, 20, 6F, 62, 6A, 65, 63, 74,
016 - OD, OA, OD, OA, 42, 69, 6E, 61, 72, 79,
017 - 52, 65, 61, 64, 65, 72, 20, 2D, 20, 72,
018 - 65, 61, 64, 20, 69, 6E, 20, 76, 61, 72,
019 - 69, 6F, 75, 73, 20, 74, 79, 70, 65, 73,
020 - 20, 28, 43, 68, 61, 72, 2C, 20, 49, 6E,
<Enter> 2 0
. 0x61 . ,
, 0x65 . 0x20 .
" S t r e a m ( " . , ?
"ASCII, table of codes" .
Unicode,
# ( Unicode ,
"Unicode characters").
450
VII.
, :
""
Could not find a part of the path
"C:\C#Programs\LoopThroughFiles\bin\Debug\x".
<Enter> ...
?...
:
, ,
, .
,
, 15, "
". " ",
, .
,
, .. , , .
.
, .
, ,
, .
, ,
. ,
. ,
, .
.
.
.
(
), , .
.
.
,
.
, , ,
. ,
,
.
20.
451
,
(
List<T>,
15, " ".
, # 2.0,
, .
System. Collections .Generic).
,
System. Collections System. Collections . Specialized.
,
System. Collections . Generic.
.
// LinkedListContainer - ""
// .
// IEnumerable , foreach.
// ,
// IEnumerator
using System;
using System.Collections;
namespace LinkedListContainer
{
// LLNode - LLNode .
// LLNode ,
//
public class LLNode
{
// ,
internal object linkedData = null;
//
internal LLNode forward = null; //
internal LLNode backward = null; //
internal LLNode(object linkedData)
{
this.linkedData = linkedData;
}
// ,
public object Data
{
get
{
return linkedData;
}
}
452
VII.
II LinkedList -
public class LinkedList : IEnumerable
{
// . internal
//
internal LLNode head = null;
//
internal LLNode tail = null;
//
public IEnumerator GetEnumerator()
return new LinkedListlterator(this);
// AddObject -
public LLNode AddObject(object objectToAdd)
}
// AddObject-
public LLNode AddObject(LLNode previousNode,
object objectToAdd)
{
//
LLNode newNode = new LLNode(objectToAdd);
// .
if (head == null && tail == null)
{
// ...
head = newNode;
tail = newNode;
return newNode;
}
// ?
if (previousNode != null &&
previousNode.forward != null)
//
LLNode nextNode = previousNode.forward;
//
newNode.forward = nextNode;
previousNode.forward = newNode;
//
nextNode.backward = newNode;
newNode.backward = previousNode;
return newNode;
}
// ?
if (previousNode == null)
20.
453
{
//
LLNode nextNode = head;
newNode.forward = nextNode;
nextNode.backward = newNode;
head = newNode;
return newNode;
}
//
newNode.backward = previousNode;
previousNode.forward = newNode;
tail = newNode;
return newNode;
}
// RemoveObject -
public void RemoveObject(LLNode currentNode)
{
//
LLNode previousNode = currentNode.backward;
LLNode nextNode
= currentNode.forward;
//
currentNode.forward = currentNode.backward = null;
// ?
if (head == currentNode && tail == currentNode)
head = tail = null;
return;
// ?
if (head != currentNode && tail
!= currentNode)
previousNode.forward = nextNode;
nextNode.backward = previousNode;
return;
// ?
if (head == currentNode && tail
!= currentNode)
head = nextNode;
nextNode.backward = null;
return;
// ...
tail = previousNode;
previousNode.forward = null;
// LinkedListlterator -
454
VII.
// LinkedList
public class LinkedListlterator : IEnumerator
{
//
private LinkedList linkedList;
// "" "" .
// private
//
private LLNode currentNode = null;
private LLNode nextNode = null;
//LinkedListlterator -
public LinkedListlterator(LinkedList linkedList)
{
this.linkedList = linkedList;
Reset ( ) ;
}
// Current-
public object. Current
{
get
{
if
{
(currentNode == null)
return null;
}
return currentNode.1inkedData;
// Reset - , ,
//
public void Reset()
{
currentNode = null;
nextNode = linkedList.head;
}
// MoveNext - ,
//
public bool MoveNext()
{
currentNode = nextNode;
if (currentNode == null)
{
return false;
}
nextNode = nextNode.forward;
return true;
20.
455
}
// foreach
Console.WriteLine("\ foreach");
foreach(string s in 11c)
Console.WriteLine(s);
}
//
Console.WriteLine(" <Enter> " +
" . . . " ) ;
Console.Read();
}
}
LinkedList LLNode
. . 20.1 ,
( "") , LinkedList
"". , , .NET,
.
LLNode.
forward , backward
. LinkedList . head
, tail . , ,
.
456
VII.
. 20.1.
L i n k e d L i s t
LLNode
AddObject
AddOb j ect ().
RemoveObject ().
, ,
. , .
1. . ;
. 20.2. head null, tail.
,
.
. 20.2.
2. .
null. . 20.3
. ,
forward backward , , -
20.
457
, . . 20.4
( ,
"" - ,
,
).
. 20.3.
. 20.4.
458
VII.
. 20.5.
3. . .
,
, . . 20.5.
4. , . ,
3.
R e m o v e O b j e c t () ,
.
A d d O b j e c t () R e m o v e Ob j e c t ()
. 20.2-20.5 .
,
, .
, .
, L i n k e d L i s t C o n t a i n e r
: L L N o d e , L i n k e d L i s t L i n k e d L i s t l t e r a t o r . L i n k e d L i s t l t e r a t o r
.
, .
.
L i n k e d L i s t l t e r a t o r
. c u r r e n t N o d e n u l l , a n e x t N o d e
. M o v e N e x t () c u r r e n t N o d e
, , a n e x t N o d e
. M o v e N e x t () , c u r r e n t N o d e
20.
459
n u l l , ,
f a l s e . .
M a i n () ,
s t r i n g . ,
.
.
M o v e N e x t () ,
f a l s e , , .
, .
M a i n () , f o r e a c h .
f o r e a c h ,
M a i n () .
f o r e a c h . ,
. :
i n t i,
.
f o r e a c h . re
m o v e d l t e m s , ,
f o r e a c h
. f o r e a c h
r e m o v e d l t e m s ,
, ,
:
List<string>
removedltems
new
List<string>();
// no o r i g i n a l C o l l e c t i o n
foreach(string s
in o r i g i n a l C o l l e c t i o n )
{
// s ,
//
removedltems
removedltems.Add(s);
}
foreach(string
in
removedltems)
//
no
removedltems
{
originalCollection.Remove(s);
}
L i n k e d L i s t C o n t a i n e r :
460
foreach
VII.
< E n t e r >
...
G e n e r i c L i n k e d L i s t C o n t a i n e r
-. , G e n e r i c L i n k e d L i s t C o n t a i n e r
I E n u m e r a t o r ,
,
I E n u m e r a t o r < T > .
. ,
L i n k e d L i s t , , ,
.
.
. " ,
. s t r i n g
." ,
,
?
, .
, 1000 ,
1000 , , . ,
2000 , ,
. (, ,
,
?)
. ,
, 256
, c h a r [ 2 5 6 ] .
, ,
, ,
, (
). , C/C++:
# .
.
( , #)
, I E n u m e r a t o r .
.
20.
461
(iterator) (enumerator)
. ,
.
, .
.
:
.
, , .
:
//
void myClearFunction(Collection aColl,
i n t index)
{
aColl[index]
= 0; //
//
//
... ...
}
( ).
, G e t N e x t ()
; P u s h ( )
P o p () ..
, ,
. X I t e r a t o r X .
X, I t e r a t o r X I E n u m e r a t o r ,
. , ,
.
() .
.
I E n u m e r a t o r ,
.
.
, .
.
-
.
(, ),
.
,
.
f o r e a c h , I E n u m e r a t o r
.
, , .
,
, .
462
VII.
I E n u m e r a t o r .
R e s e t () ,
. : I E n u m e r a t o r , I E n u m e r a t o r < T > ,
R e s e t ( ) . L i n k e d L i s t
M o v e N e x t ( ) .
M o v e N e x t ()
.
C u r r e n t ( ), ,
.
.
M y C o l l e c t i o n ( )
, I t e r a t o r M y C o n t a i n e r (
I t e r a t o r X , ).
C o n t a i n e d D a t a O b j e c t s M y C o l l e c t i o n .
I E n u m e r a t o r :
// M y C o l l e c t i o n
// C o n t a i n e d D a t a O b j e c t d a t a
void M y F u n c t i o n ( M y C o l l e c t i o n
myColl)
{
// ,
MyCollection,
// I t e r a t o r M y C o l l e c t i o n ;
//
// myColl
I E n u m e r a t o r i t e r a t o r = new I t e r a t o r M y C o l l e c t i o n ( m y C o l l ) ;
// " "
//
while(iterator.MoveNext
())
{
//
//
ContainedDataObject containedData;
// data
contained =
(ContainedDataObject)iterator.Current;
//
. . . c o n t a i n e d . . .
M y F u n c t i o n ( )
Con
t a i n e d D a t a O b j e c t s . I t e r a t o r M y
C o l l e c t i o n . M o v e N e x t ( ) .
M o v e N e x t () .
M o v e N e x t () " . "
M o v e N e x t () f a l s e ,
.
C u r r e n t
. C o n t a i n e d D a t a O b j e c t
, c o n t a i n e d . C u r r e n t ,
M o v e N e x t ( ) t r u e .
20.
463
foreach
I E n u m e r a t o r , #
f o r e a c h .
f o r e a c h ,
I E n u m e r a b l e , , :
void
MyFunction(IEnumerable
containerOfStrings)
{
foreach(string
in
containerOfStrings)
{
Console.WriteLine("
{}",
s);
}
}
I E n u m e r a b l e G e t E n u m e r a t o r ( ) ,
I E n u m e r a t o r .
f o r e a c h G e t E n u m e r a t o r () .
.
,
. , I E n u m e r a b l e I E n u m e r a t o r , . # 2.0
. S y s t e m . C o l l e c t i o n s . G e n e r i c
.
, f o r e a c h :
foreach(int
nValue
in
myContainer)
// ...
}
f o r :
for(IEnumerator i =
myContainer.GetEnumerator();
i.MoveNext();
)
int
//
nValue
(int)i.Current
//
//
//
//
//
}
f o r .
M o v e N e x t () . M o v e N e x t ()
, .. . ( ,
, .
i . M o v e N e x t ( ) . )
i n t ( C u r r e n t
O b j e c t ) . i n t , #
.
464
VII.
: c o n t a i n e r []
- c o n t a i n e r . ,
.
#
. ,
. ,
i n t , ,
, s t r i n g .
, ,
t h i s [ ] :
class
MyArray
{
public
{
get
string this[int
index]
//
// " t h i s "
{
return
array[index];
}
set
{
array[index]
value;
}
s = m y A r r a y [ i ] ; g e t ,
i . m y A r r a y [ i ] = " " ;
s e t , i " " .
i n t . ,
. ,
.
I n d e x e r
K e y e d A r r a y ,
, , ,
s t r i n g .
// I n d e x e r -
//
//
20.
465
using
System;
namespace
Indexer
{
public
class
KeyedArray
//
""
/ / ,
private
string[]
sKeys;
//
//
object
private
object []
KeyedArray
II
//
public
oArrayElements;
KeyedArray(int
KeyedArray
nSize)
sKeys = new s t r i n g [ n S i z e ] ;
oArrayElements
= new o b j e c t [ n S i z e ] ;
// Find - ,
// sTargetKey
( ,
-1)
private
int
Find(string
sTargetKey)
for (int
if
0,-
<
sKeys.Length;
(String.Compare(sKeys[i],
return
i++)
sTargetKey)
==
0)
i,-
}
}
return
- 1 ;
}
// F i n d E m p t y -
//
private
int
FindEmpty()
for
(int
if
(sKeys[i]
0;
i
==
<
sKeys.Length;
i++)
null)
{
return i;
}
}
throw
new
Exception("
/ /
//
");
set
{
466
VII.
//
int
if
index
Find(sKey);
(index
<
0)
{
// -
index = FindEmpty();
sKeys[index]
= sKey;
}
//
oArrayElements[index]
= value;
}
get
{
int index
if
(index
=
<
Find(sKey);
0)
{
return
null;
}
return
oArrayElements[index];
}
public
class
Program
{
public
s t a t i c
void
Main(string[]
args)
{
//
K e y e d A r r a y ma = new K e y e d A r r a y ( 1 0 0 ) ;
//
ma [ " B a r t " ] = 8;
["Lisa"]
= 10;
["Maggie"]
= 2;
/ / L i s a
Console.WriteLine("
Lisa");
i n t age =
(int)ma["Lisa"];
Console.WriteLine(" Lisa {o}",
age);
//
Console.WriteLine(" <Enter> " +
" ...");
Console.Read();
K e y e d A r r a y . o A r r a y E l e m e n t s
K e y e d A r r a y . , s K e y s ,
, '- s K e y s
/- o A r r a y E l e m e n t s .
K e y e d A r r a y s t r i n g .
20.
467
o A r r a y E l e
m e n t s . F i n d ( ) , s e t [ ] F i n d E m p t y ( )
, .
g e t [] .
F i n d . F i n d ()
, g e t [] o A r r a y E l e m e n t s ,
. F i n d - 1 , g e t []
n u l l , , .
F i n d () s K e y s
, s t r i n g . F i n d ( )
( - 1 , ).
F i n d E m p t y () ,
.
F i n d ( ) F i n d E m p t y ()
, ,
.
,
L i n k e d L i s t ? , . , K e y e d A r
r a y s K e y s , ,
F i n d () F i n d E m p t y ( ) , .
L i n k e d L i s t
,
L i n k e d L i s t l t e r a t o r , f o r w a r d
. , .
, n u l l .
? "
".
M a i n () .
K e y e d A r r a y 100 (.. 100 ).
. ,
ma [" L i s a " ] .
,
, [ ] , K e y e d A r r a y ,
. ,
, i n t ,
K e y e d A r r a y (. 15, " ").
468
VII.
Lisa
Lisa
<Enter>
10
...
IList
object
,
t h i s [int].
I L i s t < T > , o b j e c t
.
.
M a i n ( ) ,
L i n k e d L i s t ? (
L i n k e d L i s t C o n t a i n e r -.) :
public
s t a t i c
void
Main ( s t r i n g []
args)
{
//
L i n k e d L i s t 11 = new L i n k e d L i s t ( ) ;
// . . .
Console.WriteLine("
LinkedListlterator
H i
=
");
(LinkedListlterator)11.GetEnumerator();
Hi . R e s e t ()
while ( H i .MoveNext () )
{
string s
=
( s t r i n g ) Hi . C u r r e n t ;
Console.WriteLine(s);
}
L i n k e d L i s t l t e r a t o r M o v e N e x t ( )
C u r r e n t . # 2.0
, .
G e t E n u m e r a t o r () (
).
M o v e N e x t ( ) .
C u r r e n t ,
.
f o r e a c h (#
).
, f o r e a c h L i n k e d L i s t
. G e t E n u m e r a t o r ( ) .
L i n k e d L i s t l t e r a t o r . ,
.
20.
469
(iterate
block) .
.
. ,
K e y e d L i s t P r i o r i t y Q u e u e
I E n u m e r a t o r .
f o r e a c h .
,
.
I t e r a t o r B l o c k s -.
// I t e r a t o r B l o c k s -
//
using
System;
namespace
IteratorBlocks
{
class
IteratorBlocks
{
//Main -
//
s t a t i c
void
Main(string[]
args)
{
// ,
//
M o n t h D a y s md = new M o n t h D a y s ( ) ;
//
Console.WriteLine(":\n");
foreach
(string
sMonth
in
md)
{
Console.WriteLine(sMonth);
}
//
S t r i n g C h u n k s sc = new S t r i n g C h u n k s ( ) ;
// - ,
//
Console.WriteLine("\:\");
foreach
( s t r i n g sChunk in sc)
{
Console.WriteLine(sChunk);
}
/ /
Console.WriteLine("\
: \ " ) ;
foreach
( s t r i n g sChunk in sc)
{
Console.Write(sChunk);
}
Console.WriteLine();
//
470
13
VII.
Y i e l d B r e a k E x yb = new Y i e l d B r e a k E x ( ) ;
// ,
13
Console.WriteLine("\
:\n");
foreach
(int nPrime
i n yb)
Console.WriteLine(nPrime);
}
//
EvenNumbers en = new E v e n N u m b e r s ( ) ;
// 10 4
Console.WriteLine("\
:\");
foreach
(int nEven in en.DescendingEvens(11,
3))
Console.WriteLine(nEven);
}
// double
P r o p e r t y l t e r a t o r p r o p = new P r o p e r t y l t e r a t o r ( ) ;
Console.WriteLine("\
double:\n");
foreach
(double db in prop.DoubleProp)
Console.WriteLine(db);
//
Console.WriteLine(" <Enter> " +
" ...");
Console.Read();
//
I t e r a t o r B l o c k s
// MonthDays - ,
//
class
MonthDays
{
string[]
months =
{
"January 31",
"February 28",
"March 3 1 " ,
"April 30",
"May 3 1 " ,
"June 30",
"July 31",
"August 3 1 " ,
"September 30",
"October 31",
"November 30",
"December 31"
};
//GetEnumerator -
public
System.Collections.IEnumerator GetEnumerator()
{
foreach
(string
sMonth
in
months)
//
y i e l d r e t u r n sMonth;
//
//StringChunks
//
class
StringChunks
{
//GetEnumerator - .
//
()
Main
20.
471
public
System.Collections.IEnumerator
GetEnumerator()
{
//
yie
yie
yie
yie
yie
ld return
"Using i t e r a t o r
ld return
"blocks
";
ld return
" i s n ' t
all
";
ld return
"that
hard";
ld
return
".";
";
}
}
//YieldBreakEx
//
yield
class
break
YieldBreakEx
{
i n t [ ] p r i m e s = { 2, 3, 5, 7,
11, 13,
17, 19, 23 };
/ / G e t E n u m e r a t o r -
// y i e l d
//
break
public
System.Collections.IEnumerator GetEnumerator()
{
foreach
(int
nPrime
in
primes)
if
(nPrime >
yield
return
//EvenNumbers
//
class
13)
yield
nPrime;
break;
//
EvenNumbers
{
//DescendingEvens - "" ,
// y i e l d
//
break
public
System.Collections.IEnumerable
{
DescendingEvens(int nTop,
i n t nStop)
// nTop
if
(nTop % 2
!= 0)
nTop -= 1;
// n S t o p
//
f o r ( i n t i = n T o p ; i >= n S t o p ; i -= 2)
{
if
(i < n S t o p )
yield
break;
//
//
yield
return
i;
//Propertylterator //
472
VII.
class
Propertylterator
{
doublet]
doubles =
{
1.0,
2.0,
3.5,
4.67
};
// D o u b l e P r o p -
"get"
public
System.Collections.IEnumerable
DoubleProp
get
{
foreach
(double
db
in
doubles)
{
yield
return
db;
I t e r a t o r B l o c k s
, :
//
MonthDays
//
class
MonthDays
{
string[]
{
months
"January
"April
"August
30",
31",
30",
"December
-
28",
"June
"September
//GetEnumerator
public
"February
"May
31",
"November
31",
"March
30",
30",
"July
"October
31"
31",
31",
31",
};
System.Collections.IEnumerator
GetEnumerator()
{
foreach(string
sMonth
in
months)
{
//
y i e l d r e t u r n sMonth;
//
}
M a i n ( ) ,
f o r e a c h :
//
//
M o n t h D a y s md
//
new M o n t h D a y s ( ) ;
Console.WriteLine(":\n");
foreach
(string
sMonth
in
md)
{
Console.WriteLine(sMonth);
20.
473
" ", ,
K e y e d A r r a y . , s t r i n g .
,
. ,
, GetEnumera
t o r ( ) . G e t E n u m e r a t o r ( ) S y s t e m . C o l l e c t i o n s ,
I E n u m e r a t o r . , ,
, -
-. ,
y i e l d r e t u r n . C#
: - M o v e N e x t ()
. .
, G e t E n u m e r a t o r ( ) ,
I E n u m e r a t o r .
:
;
;
, .
L i n k e d L i s t , . L i n k e d L i s t
, ,
.
.
(
,
, A d d () - . K e y e d A r r a y ,
, .
A d d ( ) , ,
f o r e a c h . )
, ,
, ,
I n d e x e r . (, ,
"" M o n t h D a y s ,
M o n t h D a y s . )
, , ,
"" ,
foreach.
I t e r a t o r B l o c k s ,
:
//StringChunks -
//
474
VII.
class
StringChunks
/ / G e t E n u m e r a t o r - . ,
//
()
Main
public
System.Collections.IEnumerator GetEnumerator()
//
yie
yie
yie
yie
yie
l
l
l
l
l
d return
" U s i n g i t e r a t o r /.
d return
"blocks
";
d return
"isn't all
";
d return
"that hard";
d
return
".";
}
S t r i n g C h u n k s , ,
. . ?
y i e l d r e t u r n ,
,
. "" ,
, M o n t h D a y s . ,
M a i n ( ) ,
f o r e a c h , y i e l d r e t u r n .
M a i n ( ) , ""
StringChunks:
//
StringChunks sc = new S t r i n g C h u n k s ( ) ;
// - ,
//
Console.WriteLine("\:\");
foreach
( s t r i n g sChunk in sc)
{
Console.WriteLine(sChunk);
C# 2.0 .
y i e l d r e t u r n M o v e N e x t ( ) C u r
r e n t . y i e l d b r e a k
b r e a k ,
switch.
yield return
y i e l d r e t u r n .
1. .
2. .
3. ...
20.
475
M o v e N e x t ( ) , |
L i n k e d L i s t . M o v e N e x t ()
M o v e N e x t () .
?
f o r e a c h , S t r i n g C h u n k s :
foreach
(string
sChunk
in
sc)
{
Console.WriteLine(sChunk);
}
, ,
.
f o r e a c h .
yield break
.
, y i e l d b r e a k .
, .
, y i e l d b r e a k :
//YieldBreakEx -
// yield break
class
YieldBreakEx
{
i n t [ ] p r i m e s = { 2, 3, 5, 7, 1 1 , 1 3 , 1 7 , 1 9 , 23 } ,/ / G e t E n u m e r a t o r -
// y i e l d
//
break
public
System.Collections.IEnumerator
GetEnumerator()
{
foreach
(int
nPrime
in
primes)
{
if (nPrime >
yield return
13) y i e l d
nPrime;
break;
//
i f ,
, (,
f o r e a c h ). 13, y i e l d b r e a k , .
, y i e l d r e t u r n
, .
:
public
System.Collections.IEnumerator
GetEnumerator()
476
VII.
yield
return
something;
G e t E n u m e r a t o r ( ) ,
, S y s t e m .
Collections. IEnumerable
IEnumerator,
G e t E n u m e r a t o r ( ) M y F u n c t i o n ( ) .
, , ,
, , : !
//EvenNumbers
//
//
class
EvenNumbers
{
//DescendingEvens - " ",
//
y i e l d break.
// f o r e a c h
// Main()
public
System.Collections.IEnumerable
DescendingEvens(int
nTop,
int
nStop)
//
//
if
nTop ,
(nTop % 2 != 0)
// n T o p
n T o p -= 1;
// nTop
// nStop ,
f o r ( i n t i = n T o p ; i >= n S t o p ; i -= 2)
if
//
//
yie
(i < nStop)
yield break;
ld
return
i;
D e s c e n d i n g E v e n s () ( ),
.
, , 1 .
n S t o p (, n S t o p ,
1 ). i n t , I E n u
m e r a b l e . y i e l d r e t u r n ,
f o r e a c h .
:
"",
"" , s t r i n g -
20.
477
C h u n k s . ,
, .
. (
, Web-). ,
, ,
-2, .
D e s c e n d i n g E v e n s () f o r e a c h
M a i n () ( , ,
%):
// "" EvenNumbers
EvenNumbers en = new E v e n N u m b e r s ( ) ;
// : 10 4
C o n s o l e . W r i t e L i n e ( " \ : " ) ;
f o r e a c h ( i n t even in en.DescendingEvens(11,
3))
{
Console.WriteLine(even);
}
10 4. ,
f o r e a c h . E v e n N u m b e r s (
). f o r e a c h :
EvenNumbers
foreach(int
en = new E v e n N u m b e r s ( ) ;
even in en.DescendingEvens(nTop,
nStop))
...
D e s c e n d i n g E v e n s () ,
.
, :
foreach(int even
(nTop,nStop))...
in
EvenNumbers.DescendingEvens
, "" ,
,
"" : ,
,
. (, ,
R a n d o m ) ?
P r i o r i t y Q u e u e
15, " ",
P a c k a g e F a c t o r y W i t h l t e r a t o r
-.
, . ,
P a c k a g e F a c t o r y P r i o r i
t y Q u e u e , .
478
VII. I
. ,
g e t ( )
. D o u b l e P r o p .
g e t () ,
d o u b l e :
// P r o p e r t y l t e r a t o r -
// get
class
Propertylterator
{
doublet]
doubles = {
1.0,
2.0,
3.5,
4.67
};
// D o u b l e P r o p - " g e t "
public
System.Collections.IEnumerable
DoubleProp
get
{
foreach(double
db
in
doubles)
{
}
yield
return
db;
}
}
D o u b l e P r o p , D e s c e n d
i n g E v e n s ( ) . I E n u
m e r a b l e , ,
g e t ( ) , s e t ( ) . g e t ( )
f o r e a c h ,
y i e l d
r e t u r n
d o u b l e .
M a i n ( ) :
// "" P r o p e r t y l t e r a t o r
P r o p e r t y l t e r a t o r p r o p = new P r o p e r t y l t e r a t o r ( ) ;
// :
double
foreach
(double db in prop.DoubleProp)
Console.WriteLine(db);
, ,
.
I t e r a t o r B l o c k s , ,
, M o n t h D a y s . , ,
S e n t e n c e C h u n k s , , D e
s c e n d i n g E v e n s , . ,
20.
479
, ,
, L i n k e d L i s t ?
L i n k e d L i s t W i t h l t e r a t o r B l o c k - . L i n k e d L i s t
G e t E n u m e r a t o r ( ) ,
.
LinkedListlterator.
GetEnu
m e r a t o r ( ) .
-.
// L i n k e d L i s t W i t h l t e r a t o r B l o c k
-
//
class LinkedList
//
":
I E n u m e r a t o r "
{
...
//
GetEnumerator
public
IEnumerator
GetEnumerator()
{
//
//
null,
//
//
if(currentNode
==
null)
{
currentNode
head;
}
//
//
while(currentNode
!=
GetEnumerator()
null)
{
yield return currentNode.Data;
currentNode = currentNode.forward;
} }
:
public
System.Collections.IEnumerator
GetEnumerator()
{}
, I E n u m e r a t o r , G e t E n u
m e r a t o r L i n k e d L i s t .
G e t E n u m e r a t o r ( ) .
, #
L i n k e d L i s t l t e r a t o r . .
L i n k e d L i s t WithlteratorBlock.
G e t E n u m e r a t o r ()
y i e l d r e t u r n
, .
.
480
VII.
, I E n u
m e r a t o r , .
.
, .
L i n k e d L i s t - c u r r e n t N o d e ,
.
c u r r e n t N o d e n u l l , .
, c u r r e n t N o d e ,
.
h e a d n u l l ( ), c u r r e n t N o d e
. ,
n u l l ,
f o r e a c h .
,
M o v e N e x t () :
// ,
while(currentNode
!= null)
MoveNext
// , C u r r e n t
y i e l d r e t u r n c u r r e n t N o d e . . . ; //
currentNode = currentNode.forward;
}
f o r e a c h ( S t r i n g C h u n k s , ).
y i e l d r e t u r n , ""
L L N o d e .
s t r i n g , i n t , S t u d e n t .. . c u r
r e n t N o d e , :
y i e l d r e t u r n c u r r e n t N o d e . D a t a ; / /
currentNode = currentNode.forward;
To , , . D a t a
L L N o d e O b j e c t .
,
, O b j e c t .
w h i l e y i e l d b r e a k ,
. G e t E n u m e r a t o r ()
f o r e a c h M a i n ( ) , .
, ,
L i n k e d L i s t l t e r a t o r
LinkedList.
20.
481
f o r e a c h .
s t r i n g , f o r e a c h :
f o r e a c h ( s t r i n g s in 11c)
{
Console.WriteLine(s);
//
//
foreach
}
G e n e r i c L i n k e d L i s t C o n
t a i n e r -.
L i n k e d L i s t
s t r i n g , i n t . , ,
f o r e a c h .
L i n k e d L i s t < T >
S y s t e m . C o l l e c t i o n s . G e n e r i c .
,
.
. , ^
...
...
L i n k e d L i s t
, L i n k e d L i s t .
,
.
. i t e r a t o r l ,
i t e r a t o r 2 .
(
, L i n k e d L i s t ) . I t e r a t o r B l o c k l t e r a t o r
,
.
// I t e r a t o r B l o c k l t e r a t o r -
// ,
// L i n k e d L i s t ,
//
using System;
using
System.Collections;
namespace
IteratorBlocklterator
{
class
Program
{
//
//
//
sta
{
s
482
)
t i c void Main(string[]
args)
t r i n g []
strs
new
s t r i n g []
VII.
{
"Joe",
"Bob",
"Tony",
"Fred"
};
M y C o l l e c t i o n mc = new M y C o l l e c t i o n ( s t r s ) ;
//
MyCollectionlterator mcil = mc.GetEnumerator();
foreach
(string si in mcil)
/ /
// -
Console.WriteLine(si);
/ /
i f ( s i == "Tony")
{
/ /
//
M y C o l l e c t i o n l t e r a t o r mci2 = m c . G e t E n u m e r a t o r ( ) ;
foreach
(string s2
in mci2)
//
//
if(s2
==
"Bob")
{
Console.WriteLine("\t{0}
{l}",
s2,
s i ) ;
/ /
Console.WriteLine(" <Enter> " +
" .. . " ) ;
Console.Read();
}
//
public
class
MyCollection
{
// A r r a y L i s t
// i n t e r n a l
-
//
i n t e r n a l A r r a y L i s t l i s t = new A r r a y L i s t ( ) ;
public
MyCollection(string[]
strs)
foreach(string
in
strs)
{
list.Add(s);
}
}
//
//
GetEnumerator -
public
MyCollectionlterator
LinkedList,
GetEnumerator()
{
return
new
MyCollectionlterator(this);
}
}
// M y C o l l e c t i o n l t e r a t o r -
public
class
MyCollectionlterator
20.
MyCollection
483
//
p r i v a t e M y C o l l e c t i o n mc;
public
MyCollectionlterator(MyCollection
mc)
{
this.mc
mc,-
}
// G e t E n u m e r a t o r - ,
//
public
System.Collections.IEnumerator GetEnumerator()
{
//
//
foreach
(string
in
internal
mc.list)
{
yield
return
s;
//
} }
I t e r a t o r B l o c k l t e r a t o r -
A r r a y L i s t . G e t E n u m e r a t o r ( )
, , L i n k e d L i s t .
//
//
GetEnumerator -
public
LinkedList,
MyCollectionlterator
GetEnumerator()
{
return
new
MyCollectionlterator(this);
}
.
G e t E n u m e r a t o r ( ) . ,
. :
// G e t E n u m e r a t o r - ,
//
public
System.Collections.IEnumerator GetEnumerator()
{
//
//
foreach
(string
in
internal
mc.list)
{
yield
return
s;
//
}
}
A r r a y L i s t ',
y i e l d r e t u r n
M a i n ( ) ,
. f o r e a c h f o r e a c h
, :
Joe
Bob
Tony
484
VII.
Bob
Tony-
Fred
// -
.
M a i n ( ) :
MyCollectionlterator
foreach
(string
si
in
mcil
mcil)
// -
Console.WriteLine(si);
/ /
i f ( s i == "Tony")
mc.GetEnumerator();
//
{
/ /
//
M y C o l l e c t i o n l t e r a t o r mci2 = m c . G e t E n u m e r a t o r ( ) ;
foreach
( s t r i n g s2 in mci2)
//
//
i f ( s 2 == "Bob")
{
Console.WriteLine("\t{o}
{l}",
s2,
s i ) ;
}
, , M o v e N e x t () C u r r e n t ,
...
20.
485
21
Visual Studio
>
Visual Studio
>
>
, . #,
# .
, Visual Studio, , ,
. , Visual Studio.
Visual Studio 2005,
Visual # Express.
Visual Studio:
: F o r m Designer, T o o l b o x Properties.
1, " Windows- # " .
Visual Studio
.
.
(Closed)
(Floating)
(Docked)
(Tabbed)
.
, .
V i e w , . 21.1.
V i e w ; V i e w ^ O t h e r W i n d o w s .
D e b u g .
Visual Studio,
. 21.2. . ,
, "
" Visual Studio, .
Visual Studio .
,
, ,
.
,
W i n d o w ^ D o c k a b l e . ""
Visual Studio.
Visual Studio
.
. 21.3, , Output , Error
List . ,
.
, , , ,
, Visual Studio. . 21,3
Solution E x p l o r e r C l a s s V i e w ,
Visual Studio.
, .
,
, , , , .
488
VII.
. 21.1. View
489
,
( "" ,
)
. Output . 21.3,
Visual Studio.
, , .
(auto-hide).
. ,
, .
( ).
, , ,
.
, .
.
, , ,
. . 21.4 Visual Studio, . 21.3,
O u t p u t
Visual Studio.
490
VII.
" "
, .
. 21.4.
, ,
, ,
.
, (
. 21.4
).
, - (
).
, . ,
, . 21.3,
O u t p u t , E r r o r List ,
. 21.5. E r r o r List
Visual Studio, (
. 21.6 ). ,
.
"" (
). ,
. . 21.7 ,
U n i v e r s i t y . c s , S t u d e n t , c s P r o g r a m , c s .
Solution E x p l o r e r ( )
, .
491
. 21.5.
. 21.3, . ,
. 21.6
. 21.6.
492
VII.
. 21.7.
Visual Studio
Solution E x p l o r e r
V i e w C l a s s D i a g r a m . Visual Studio C l a s s D i a g r a m l . c d . C l a s s D e s i g n e r
UML ( "class dia
gram, presentation and documentation" ).
UML-,
.
Visual Studio 2005,
# , ,
.
, , , Visual Studio
Microsoft.
" ?" .
, 19, "
". ,
493
: , ,
.
.
,
. 19, " ",
P r o g r a m , s ,
, ,
,
.
, .
.
, , . , Solution Ex
plorer
. . 21.8 Solution Explorer
#.
. C S P R O J ,
.
Solution Explorer.
,
, , ,
.
19, " ":
, ,
, .
.
, .
, , .
Visual Studio (
. S L N ) .
,
. , , ,
, Visual Studio.
, , Visual
Studio, .
,
C l a s s L i b r a r y -.
, , "",
.
19, " ".
.
V S I n t e r f a c e , U n i v e r
s i t y S t u d e n t . .
S t u d e n t U n i v e r s i t y , .
494
. 21.8. no Pro
gram, cs, Solution Explorer
VSInterface.
1. File 1 ^New^Project.
2. Console Application.
3. VSInterface .
View"=>Solution E x p l o r e r ,
V S I n t e r f a c e , . 21.8. , V S I n
t e r f a c e , V S I n t e r f a c e .
Solution Explorer
Solution Explorer , P r o p e r t i e s R e f
e r e n c e s . P r o p e r t i e s A s s e m b l y I n f o . c s ,
"" "". , ,
, , ,
, , (
) . ..
R e f e r e n c e s ,
P r o j e c t s A d d Reference. , ,
u s i n g ( "" ,
; ,
). : P r o g r a m , cs
, . (
Windows,
, 1, " Windows- #".)
495
Solution E x p l o r e r
( ). P r o g r a m , cs
# M a i n ( ) . ,
.
Visual Studio P r o g r a m , cs C l a s s l . c s ,
. P r o g r a m , s ,
.
. Solution Explorer
A d d ^ N e w Folder.
. Visual Studio , , .
A s s e m b l y l n f . c s ,
, ,
. , ,
. V e r s i o n in
f o r m a t i o n ( ) . -,
Windows
P r o p e r t i e s .
.
.
Solution E x p l o r e r Add^Existing
Item. .
Solution Explorer
Properties,
. .
Properties , ,
: "project
properties" .
Properties
Settings.
Application U s e r
:
// u s i n g
u s i n g MyAppName. P r o p e r t i e s ,//
. . .
//
:
//
string
:
myString
=
Settings.Default.MyAppSetting;
// :
Settings.Default.MyUserSetting = myUserString;
//
,
S y s t e m . 10,
19, " ". , -
496
VII.
,
Application Data . (
: \ D o c u m e n t s a n d S e t t i n g s \ < u s e r n a m e > . ) ,
S e t t i n g s . D e f a u l t . S a v e ( ) (, M a i n ( )
W i n d o w s F o r m ) .
.
D.
C l a s s V i e w ,
.
Solution E x p l o r e r ,
, ,
.
.
,
. ,
C l a s s View.
C l a s s V i e w ,
,
. ,
( ) .
. . 21.7,
U n i v e r s i t y C l a s s V i e w ;
E n r o l l ()
. ( C l a s s V i e w
.)
Solution Explorer C l a s s V i e w
,
.
, ,
.
, , ,
.
S c h o o l S t u d e n t .
H i g h S c h o o l , U n i v e r s i t y S c h o o l ,
.
L e s s e r C a n a d i a n G o o s e G r e a t e r C a n a d i a n G o o s e .
497
. 21.9.
Add New Item
U n i v e r s i t y V S I n t e r f ,
.
1. V S I n t e r f
Solution Explorer, Add
.
, !
2. Class, University. cs Name
O p e n .
. 21.9 Add N e w Item C l a s s .
U n i v e r s i t y . cs
P r o g r a m , c s ,
.
3. Student.
Student.cs University.cs Program.cs.
. 21.8,
. .
S t u d e n t , ,
.
// V S I n t e r f - S t u d e n t . c s
// S t u d e n t - ,
//
using
System;
namespace
VSInterface
498
///
<summary>
/// S t u d e n t -
///
</summary>
public
class
Student
{
private
string
sStudentName;
private int nID;
public
Student(string
sStudentName,
int
nID)
{
this.sStudentName
this.nID = nID;
sStudentName;
}
///
<summary>
/// Name -
///
</summary>
p u b l i c s t r i n g Name { g e t {
///
<summary>
/// T o S t r i n g -
///
</summary>
public
override
string
return
sStudentName;}
ToString()
{
return
String.Format("{0}
({l})",
sStudentName,
nID);
}
S t u d e n t .
T o S t r i n g ( ) O b j e c t .
. 18, " ",
T o S t r i n g ( ) .
S t u d e n t , / / / .
,
, (
).
8, " ".
,
S t u d e n t . cs ,
U n i v e r s i t y . c s P r o g r a m , c s . .
,
.
, .
U n i v e r s i t y . cs ,
:
//
//
V S I n t e r f - U n i v e r s i t y . c s
U n i v e r s i t y -
499
using
System;
using
System.Collections;
namespace
VSInterface
{
///
<summary>
///
University
///
</summary>
public
class
University
{
private
string
private
SortedList
public
sName;
students;
University(string
//
sNarae)
{
t h i s . s N a m e = sName;
s t u d e n t s = new S o r t e d L i s t ( ) ;
}
///
///
///
<summary>
Enroll
-
</summary>
public
void
Enroll(Student
student)
{
students.Add(student.Name,
student);
}
public
override
string
ToStringO
{
s t r i n g s = sNarae + " \ n " ;
s += " : " + " \ n " ;
//
//
IEnumerator i t e r = students.GetEnumerator;
while(iter.MoveNext())
{
object = iter.Current;
/ / ,
// S o r t e d L i s t ,
// ,
:
//
// Student s t u d e n t =
(Student);
// // :
//
( )
D i c t i o n a r y E n t r y de =
( D i c t i o n a r y E n t r y ) ,Student student =
(Student)de.Value;
s += s t u d e n t . T o S t r i n g ( ) + " \ n " ;
return
S ;
,
V S I n t e r f a c e . U n i v e r s i t y
. E n r o l l ( )
S t u d e n t S o r t e d
L i s t
, .
500
VII.
U n i v e r s i t y . T o S t r i n g ( ) ,
. , M o v e N e x t ()
,
C u r r e n t . S o r t e d L i s t ,
, ,
, ( D i c t i o n a r y E n t r y ) ,
, .
.
15, "
", 20, " ". M o v e N e x t ()
, ,
f o r e a c h :
foreach(DictionaryEntry
de
in
students)
{
Student student =
(Student)de.Value;
s += s t u d e n t . T o S t r i n g ( ) + " \ n " ;
}
, f o r e a c h .
S t u d e n t U n i v e r s i t y .
M a i n ( ) . ,
P r o g r a m .
P r o g r a m , c s :
// V S I n t e r f - ,
// . S t u d e n t U n i v e r s i t y
// .
// P r o g r a m . c s
using
System;
namespace
VSInterface
{
class
Program
{
static
void
Main(string[]
args)
{
University university =
new U n i v e r s i t y ( " " ) ;
university.Enroll(new Student("Dwayne",
1234));
university.Enroll(new Student("Mikey",
1235));
university.Enroll(new Student("Mark",
1236));
Console.WriteLine(university.ToString() ) ;
Console.WriteLine();
//
Console.WriteLine(" <Enter> " +
" ...");
501
Console.Read();
}
}
}
Visual Studio ,
U n i v e r s i t y . c s , S t u d e n t . c s
Program. cs.
( !)
:
:
Dwayne
(1234)
Mark
(1236)
Mikey
(1235)
Press
Enter
to
terminate...
, .
, ,
, ,
. ?
,
(), , ,
.
.
, . S t u d e n t ,
:
//
//
//
V S I n t e r f - S t u d e n t . c s
Student - ,
#region
using
Using
Directives
System;
#endregion
namespace
Using
Directives
VSInterface
{
III
<summary>
/// S t u d e n t -
///
</summary>
public
class
Student
#region
//
502
Private
Data
Fields
VII.
//
//
private
string
sStudentName;
private int nID;
#endregion
#region
Private
Fields
Constructors
//
Student
//
public
Data
Student(string
sStudentName,
int
nID)
{
this.sStudentName
this.nID = nID;
sStudentName;
}
#endregion
#region
Constructors
Public
///
<summary>
///
///
Name -
</summary>
public
///
///
///
string
Methods
and
Properties
Name
get{
return
sStudentName;}
<summary>
ToString -
</summary>
public
override
string
ToString()
{
return
String.Format("{o}
({l})",
sStudentName,
nID);
}
#endregion
Public
Methods
and
Properties
Visual Studio # r e g i o n
ttendregion
.
. <Ctrl+M>, < C t r l + 0 > ,
.
, .
X M L - , / / / .
Visual Studio ,
.
8, " ",
, XML-.
503
N D o c
( h t t p : / / n d o c . s o u r c e f o r g e . n e t )
V i s u a l S t u d i o
XML-.
, .
,
. , :
/ / D i s p l a y
// S t u d e n t .
:
//
,
, , ,
. D i s p l a y A l l S t u d e n t s ()
.
, ,
.
( D i s p l a y A l l S t u d e n t s ( ) ) ,
is has ( i s V a l i d , h a s l t e m s , c a n P a s t e ) ,
. .
, .
(. 3,
" -"),
.
,
( s s t r i n g , d d o u b l e ).
,
.
, ,
. , ,
.
. ,
. ,
. , ,
,
i f ( ( y p o s == -1) & (vowelPos == - 1 ) )
...
.
,
:
public
bool
HasNoVowels(int
int
indexOfLetterY,
indexOfFirstVowel)
{
return
( i n d e x O f L e t t e r Y == -1) &
( i n d e x O f F i r s t V o w e l == -1);
504
VII.
10
( Pig Latin , - )
, ,
. , i n d e x O f F i r s t V o w e l - 1 .
,
.
, s N o V o w e l s ( ) ,
, :
if ( H a s N o V o w e l s ( y p o s ,
vowelPos) )
return
'USE_WHOLE_WORD;
( ).
, . ,
, "
" (" ,
''"), :
public
string
ConvertToPigLatin(string
return GetBackPart(word)
word)
GetFrontPart(word)
"ay";
, ,
, ,
, .. , ,
.
i f , , .
" " ,
, , .
( ),
. , G e t B a c k P a r t () G e t
F r o n t P a r t () ,
.
, ,
.
,
, , ,
.
. , , ,
, .
Head First Design Patterns (Freeman) (O'Reilly, 2004).
: . , ,
.
10
" " ;
- "" "--- --- ---".
. .
505
,
, , S y s t e m . ,
# , ,
, .
Visual Studio D o c u m e n t Explorer.
.
F1
<F1>
, .
, n e w
. n e w
<F1>. Visual Studio ,
. 21.10. ( new, . ,
" ", new, ^ 12,
"", new. , ,
n e w () ,
15, " ".)
506
VII.
Visual Studio ,
, Web.
, ,
Web.
, . Visual
Studio T o o l s ^ O p t i o n s . E n v i r o n m e n t
< F 1 > , ,
,
(Index Help). , ,
, .
, ,
,
S y s t e m . C o l l e c t i o n s . H e l p ^ l n d e x ,
Index collections Look For,
, collections. ( Help.
.)
. N E T F r a m e w o r k , . 21.11.
. 21.11. ,
S y s t e m . C o l l e c t i o n s ,
C o l l e c t i o n s .
S o r t e d L i s t . ,
. , , . 21.12.
. 21.12.
. S o r t e d L i s t , ,
.
Filtered By Index H e l p
, . . 21.11 21.12
"Visual # " .
, #. Microsoft De
veloper Network (MSDN) , #.
, .
Help, S y n c with Table
of C o n t e n t s ( ,
).
C o n t e n t s . , , ,
S o r t e d L i s t , Class Library Reference .NET
Framework Software Development Kit (SDK). C o n t e n t s
.
H e l p F a v o r i t e s Help.
"". . . 21.10
H e l p F a v o r i t e s .
,
A d d to H e l p Favorites.
508
VII.
S e a r c h Help , ,
. .
, , .
H e l p O S e a r c h , S e a r c h .
sorted S e a r c h F o r
, collection classes.
. 21.13 "collection classes".
Help, : Local Help, M S D N
Online, C o d e z o n e C o m m u n i t y Questions.
.
Puc. 21.13. ,
<F1 >,
, ,
( ,
). . Local Help
, . M S D N Online
Microsoft Developer Network (MSDN). C o d e z o n e C o m
munity ,
# .
Q u e s t i o n s , #
, .NET.
( , , ,
509
,
, ,
.
, "collection class",
( 500), ,
Web.
Local Help
S e a r c h . .
,
. , ( .NET Win
dows Forms Office Applications) . . 21.13
: Articles and Overviews ( ), Contains Code (
), How-Tos ( ), Knowledge Base ( ), Other
Documentation ( ) Syntax/API Reference ( - ]
/API). ,
. ,
, Web. (
, "Help, online content".)
, Help Favorites . ,
S e a r c h A d d to Help Favorites
Help ( ""). ,
Help Favorites, .
H o w Do I . 21.13.
"how-to".
Help Visual Studio 2005 Com
munity,
# . ,
...
D y n a m i c Help.
,
.NET Framework, # . ,
, , , .
<F1> .
<F1>. .
.
, . ... ,
Web, . , :
. . ,
, - .
510
VII.
" " Visual Studio
Help. Visual Studio
.
. T o o l s ^ O p t i o n s .
Options T e x t Editor
All L a n g u a g e s ^ G e n e r a l . , Auto List
Members.
, ,
: ,
. , - S y s
t e m . C o l l e c t i o n s ,
n e w S y s t e m . C o l l e c t i o n s .
" C o l l e c t i o n s " , Visual Studio , ,
C o l l e c t i o n s . .
Visual Studio ,
n e w .
13, "".
S o r t e d L i s t .
Visual Studio , . 21.14.
, , .
. 21.14.
511
, ,
new
System.Collections.SortedList.
, 8, "
".
, ,
(
, ).
# . ,
. # ,
, ,
. , #
. ( , ,
, .)
(
, ,
).
.
.
"".
, Visual Studio.
: ?
( , ).
// VSDebug -
// ;
// ( VSDebugFixed)
using
System;
using
System.Collections;
u s i n g System.10;
n a m e s p a c e VSDebug
{
class
Program
{
static
void
Main(string[]
args)
{
//
512
VII.
//
//
Console.WriteLine("
!");
S t u d e n t si = new S t u d e n t ( " S t u d e n t 1 " ,
1);
S t u d e n t s2 = new S t u d e n t ( " S t u d e n t 2",
2);
//
display the
two
students
Console.WriteLine("Student 1
Console .WriteLine ( "Student 2
//
//
=
",
si.ToString()) ;
=
",
s2.. T o S t r i n g ( ) ) ;
Student
Student.OutputAllStudents();
//
Console.WriteLine(" <Enter>
"
"
., . " ) ;
Console.Read();
}
public
{
s
p
p
p
class
t a t i
riva
riva
ubli
Student
c ArrayList
allStudents
te
string
sStudentName;
te
int nID;
c S t u d e n t ( s t r i n g sName,
sStudentName
nID = nID;
new
int
ArrayList();
nID
sName;
allStudents.Add(this);
// T o S t r i n g -
public
override
string
ToString()
s t r i n g s = String.Format("{0}
({l})",
sStudentName,
return
public
nID);
s;
s t a t i c
void
OutputAllStudents()
IEnumerator i t e r = allStudents.GetEnumerator();
// for while
f o r ( i t e r . R e s e t ( ) ;
iter.Current
! = null;
iter.MoveNext())
{
Student
(Student) i t e r . Current-
{0}",
s.ToString());
.
, (
E r r o r List ). -
513
, ,
. , .
, . , ,
, .
-
V S D e b u g G e n e r i . e s ,
a l l S t u d e n t s A r r a y L i s t L i s t < T > , O u t p u t A H S t u d e n t s () !
,
(single stepping).
D e b u g ^ S t e p O v e r <F10>.
,
. D e b u g
(executable statement)
, - .
, C o n s o l e . W r i t e L i n e ()
. . 21.15 Visual Studio
.
Puc. 21.15.
514
VII.
Visual Studio ,
.
, .
, .
.
, , Locals (
; . 21.15 ).
, .. ,
. s i s 2 V S D e b u g . S t u d e n t
n u l l , .
, .
a r g s s t r i n g [] ( ) 0 ,
. .
<F10>
W r i t e L i n e ( ) . , <Alt+Tab>
, V S D e b u g .
!. , .
<Alt+Tab> Visual Studio.
< A l t + T a b > Windows,
.
"" , .
, V S D e b u g ,
. <Alt+Tab> ,
Visual Studio.
, <F10> S t u
d e n t si = ....
M a i n ( ) , S t u d e n t .
, .
Locals s 2 n u l l , s i
S t u d e n t . "" si ,
"".
Locals , . 21.16, s i .
n I D i
N a m e s t r i n g .
a l l S t u d e n t s
,
"", .
nt sStudent
A r r a y L i s t .
: s S t u d e n t N a m e
" S t u d e n t 1 " , , a n I D ,
. 1 ,
. - ...
# ,
Debug<=>Stop D e b u g g i n g . <F10> :
, , W r i t e L i n e ()
515
, , . <F10>
, , <F11> .
, 21.16. Locals
<F10> <F11> , ,
.
<F11> (step into)
.
.NET. .
.
t h i s Locals.
<F10>, n I D .
Locals .
, <F10> . t h i s . n I D
, n I D Locals 1.
V S D e b u g
D e b u g ^ B u i l d V S D e b u g <F10>
, E r r o r List
. S t u d e n t ,
n I D = n I D ,
. ,
, , ,
. , . 21.16,
.
516
VII.
.
n I D :
nID = nID;
, , .
:
this.nID
nID;
//
, ,
,
. "Assignment made to same variable; did
you mean to assign something else?" ("
. ?").
?
t h i s . n I D = n I D
. (
Visual Studio Edit
and Continue .)
si s2 Locals
, , , .
W r i t e L i n e () :
Student 1 =
? , T o S t r i n g () .
, .
-
, .
.
Visual Studio ,
.
(breakpoint).
W r i t e L i n e ( ) , .
,
, . ,
, D e b u g ^ S t a r t
<F5>. , ,
, .
, ,
. 21.17. ,
, W r i t e L i n e () .
<F10>, W r i t e
L i n e ( ) , 1, <F11>,
T o S t r i n g ( ) .
517
. 21.17. , -
S t r i n g . F o r m a t r e
t u r n , :
return
String.Format("{0}
({l})",
sStudentName,
nID);
T o S t r i n g ()
s:
public
override
string
ToString()
{
string s =
return s;
String.Format("{0}
({l})",
sStudentName,
nID);
}
. ( ,
,
.)
< F U > , s
, T o S t r i n g ( ) . Locals
, . 21.18. (: \ t , ,
. <> ,
S t r i n g . F o r m a t . .
E d i t ^ A d v a n c e d ^ S h o w W h i t e S p a c e ,
, .
.)
W r i t e L i n e ( ) .
<F10>, . ! { } ,
518
VII.
, T o S t r i n g ( ) , .
, . ,
W r i t e L i n e () :
// d i s p l a y t h e two s t u d e n t s
Console.WriteLine("Student 1
Console.WriteLine("Student 2
=
=
{o}",
{o}",
si.ToString());
s2.ToString());
. 21.18. . ?
,
WriteLine():
//
Console .WriteLine ("Student 1 = " + s i . ToString ())';
//
C o n s o l e . W r i t e L i n e ( " S t u d e n t 1 = {}",
si.ToString());
Visual Studio 2005: (DataTip).
, ,
. <F10>
M a i n ( ) , s i W r i t e
L i n e ( ) ( ) s i T o S t r i n g ( ) :
{ S t u d e n t ID ( 1 ) }. , -
.
"DataTip" .
" ", ..
, -
, ,
.
519
, . 21.19.
+ s l .
s l . +
S t a t i c
m e m b e r s , a l l S t u d e n t s , [ 0 ]
A r r a y L i s t a l l S t u d e n t s sl
A r r a y L i s t a l l S t u d e n t s .
. 21.19. ,
. (
W a t c h QuickW a t c h , Locals.)
(. . 21.17) ,
. ( D e b u g s
O u t p u t A l l S t u d e n t s ( ) ,
W r i t e L i n e ( ) .
<F5> ,
C o n s o l e . .
<F10>, O u t p u t A l l S t u d e n t s ( ) .
- .
. 21.20.
f o r , ,
C u r r e n t ( . 20, " ").
: E n u m e r a t i o n h a s n o t s t a r t e d . C a l l M o v e N e x t (
. M o v e N e x t ) - , .
520
VII.
521
, ,
, .
, . Call Stack ,
: Main() OutputAllStudents ()
64. , 64
for, .
Tools^OptionsOEditor^C* Line Numbers.
,
: Main() OutputAllStudents ( ) .
,
.
, View Detail
InnerException
( + ). StackTrace
. get_Current.
Current .
IEnumerator. Current ,
MoveNext () . ,
, , Stop
Debugging Debug . (
D e b u g ^ S t o p Debugging;
<Shift+F5>.)
,
Current . for,
Current, StackTrace InnerException
Current get_Current. (
get_
set_.) , Current?
MoveNext ( ) , Current
ArrayList.
, for .
, MoveNext () false, Cur
rent null. ( while)
:
public
{
IEnumerator iter = allStudents.GetEnumerator();
while(iter.MoveNext()) // 'while 1 , 'for'
{
Student s = (Student)iter.Current;
Console.WriteLine("Student = {o}", s.ToString());
}
522
VII.
MoveNext ()
Student. Student Current.
, MoveNext () false, ,
.
foreach
20, " ".
!
:
!
Student 1 = Student 1 (1)
Student 2 = Student 2 (2)
Student = Student 1 (1)
Student = Student 2 (2)
<Enter>
...
, ,
... , .
- VSDebugFixed.
,
, . ,
, ,
, "" ,
, ,
( ).
Web,
.
Visual Studio ,
. ,
. ,
#.
Visual Studio, 22, "#
", Visual Studio.
#.
21.
Visual
Studio
523
22
#
...
>
>
.NET
>
# SharpDevelop
>
# TextPad
>
>
# NUnit
>
# ,
, Visual Studio 2005 Microsoft.
(integrated development envi
r o n m e n t IDE), 21, " Visual Studio".
, # .
Visual Studio Windows
(GUI) , Web ASP.NET,
. ,
, , ,
.
Visual Studio . , :
" #, ?"
, .
Express Visual # (.
), SharpDevelop IDE,
Visual Studio.
# TextPad,
Java Perl. ( , Google
" # development environment").
,
Visual Studio. SharpDevelop, TextPad NUnit,
,
#. , Windows
Forms
Visual Studio.
, , .NET.
, ,
# :
, , TextPad
SharpDevelop;
#, C s c . ;
, #: CorDbg.
DbgCLR. , ;
Command. com Cmd . c o m (
Windows), Windows.
#.
.
#,
Microsoft (
), .
# ,
.
, , ,
(Knowledge Base) Microsoft Web- (Microsoft's Developer
N e t w o r k MSDN) h t t p : / / m s d n . m i c r o s o f t . com.
#, .NET, Windows .
, MSDN
" .NET". , Visual #,
h t t p : / / m s d n . m i c r o s o f t . c o m / v c s h a r p / 2 0 0 5 / .
.NET h t t p : / / m s d n . m i c r o s o f t . c o m / n e t f r a m e w o r k / .
,
, . Communities Newsgroups
Visual # #. ,
Web-, .
, ,
.
Visual Studio Visual # Express (, ,
,
, ,
Visual Studio).
(SDK) .NET, -
526
VII.
. MSDN Download,
Download & Code Center.
.NET SDK, , .
,
86. 64- , 64 .
SDK ; ,
, -
MSDN.
.NET,
, ,
# , System .
,
Windows .
#, Csc. . , ,
.
.NET, #
: \Windows\Microsof t. NET\Framework\v2 . . ,
. -
.NET 2.0, 50215,
, , , Visual Studio 2005. ,
.
: \Program Files. Microsoft .NET SDK Vis
ual Studio. \GuiDebug SDK. ,
(DbgCLR. ), . (
Windows.)
#
. .
1. ( Visual Studio,
SharpDevelop, TextPad ).
, Microsoft Word WordPad.
.
2. #, Visual Studio,
SharpDevelop, TextPad . .
3.
, ,
.
4. Visual Studio, SharpDevelop,
TextPad, Windows Explorer, NUnit,
.
22.
527
5. ,
Visual Studio, TextPad, SharpDevelop .
, , ...
SharpDevelop
SharpDevelop ,
#.
Visual Studio (, , Visual Studio 2005, ,
SharpDevelop # 2.0), . 22.1.
,
Visual Studio.
, , SharpDe
velop Visual Studio. . 22.1 SharpDevelop
Visual Studio.
528
VII.
. 22.1. SharpDevelop
( ) Visual Studio
Visual Studio (, Output, Toolbox, Properties)
SharpDevelop "" "".
,
, ,
2 1 , " Visual Studio", SharpDevelop
, .
22. #
529
, SharpDevelop
.NET SDK.
Tools SDK Web. Tools^Options 1 ^Tools.
Add. "Browse .NET Docs". Command
Web-. Internet Explorer , ,
: C:\Program Files\Internet Explorer\IExplore.exe.
Arguments StartHere.htm .NET
SDK. , , - : \Program Files.
StartHere.htm C:\Program
Files\Microsof t Visual Studio 8\SDK\v2 . 0\StartHere . htm,
Visual Studio.
, Microsoft
.NET SDK. .
Tools SharpDevelop.
.NET SDK,
, Cptools . chm \Docs
.NET SDK.
" "
. ,
.
TextPad,
SharpDevelop.
530
VII.
3. Add.
. Title
"New T o o l " .
4. "New Tool" Title - Debugger.
5. Browse Command
. N E T Framework SDK.
SDK GuiDebug. DbgCLR.exe
Open.
, SDK.
6. Options, .
Debugger CLR
. , ,
.
SharpDevelop
,
CLR. ,
CLR.
CLR ,
, Visual Studio.
, ,
,
Debug .
. ,
, , .
Visual Studio 2005 ,
,
. ,
.
, ,
. ,
CLR.
Debug !
SharpDevelop Tools^Debugger (
TextPad).
- ,
.
1. D e b u g s Program to Debug.
, ,
Mycode. .
532
VII.
SharpDevelop
SharpDevelop
# .NET.
(,
) .
1. Project ^ P r o j e c t Options.
2. Project Options C o n
SharpDevelop
Visual Studio ( 21,
" Visual Studio"). ,
Tools SharpDevelop ,
Microsoft CLR, .NET Framework SDK.
, .
SharpDevelop, TextPad.
Debugger SharpDevelop
.
1. T o o l s ^ O p t i o n s .
2. Options T o o l s External
Tools.
1
22. #
531
. bin\Debug
.
2. F i l e ^ O p e n ^ F i l e
. CS ( M a i n ( ) ) , , M y c o d e . c s .
3. .CS,
, ,
.
File Open
.
. 22.2 #.
. 22.2. #, CLR
File^Close Solution
, , # Visual Studio.
. DLN. ,
,
, File^Open Solution.
. . CS.
, , !
. -
Visual Studio ,
.
22.
533
CLR, Visual Studio?
, .
, CLR Visual Studio.
Registers, Disassembly Auto CLR , Vis
ual Studio, #. ,
Auto Registers;
Ildasm.
<F1>. ,
. ,
.NET Framework SDK, .
.NET, "" Win32. ? #
.NET.
. ,
Visual Studio.
, SharpDevelop
TextPad, . 22.3. ,
,
SharpDevelop Visual Studio.
, ,
,
.
TextPad . ,
. Web- www. t e x t p a d . c o m , (
30 .) .
Web- TextPad ,
T e x t P a d
Java Perl, #.
# TextPad SharpDevelop,
Windows Forms Web-.
,
. #
TextPad.
,
534
VII.
. 22.3. TextPad
TextPad ,
, .NET,
. ,
Visual Studio SharpDevelop
, .
.
TextPad . 22.3 ,
.
, ,
.
: TextPad
; ,
, # Visual Basic.
: TextPad
,
.
:
, ,
, , ,
, -
22.
535
. T e x t P a d
.
:
,
, , Windows Explorer ,
.
.
. ,
. CS #, , ,
.
-. , Visual Studio.
SharpDevelop, TextPad
Tools.
,
.
.
Prompt for Parameters ( ).
? , ,
,
a r g s M a i n ( ) .
.
Run Minimized ( ). ,
( -)
. .
Save Documents First ( ).
,
TextPad, . .
Capture Output ( ). ,
" " .
.
, , )
, ) , -
.
, ,
Command Results TextPad
. Run ,
Capture Output.
536
VII.
Build, (, !)
.
# Capture Output ,
.
Suppress Output Until Completed ( ).
, .
.
Sound Alert When Completed (
). (, ),
,
.
TextPad, Solution
Explorer, Class View, . . SharpDe
velop, TextPad
.
, ,
Vi, Fte, Emacs Brief,
( ).
.
, TextPad
, #. ,
.
TextPad,
#: . CS # #
Tools.
.CS #
# TextPad
( ).
1. C o n f i g u r e ^ N e w Document Class.
2. Document Class Wizard C#, ,
Next.
3. * . cs " ". Next.
4. Enable Syntax Highlighting
c s h a r p . s y n . Next,
Finish.
5. C o n f i g u r e ^ P r e f e r e n c e s .
Document C l a s s e s ^ C t t .
#, . 22.4.
22.
537
6. Tabulation #.
.
Apply.
,
, .
Convert New Tabs to Spaces Convert Existing Tabs to
Spaces When Saving Files. .
. 22.4. TextPad
#
7. Font
. Apply.
Lucida Console, , 9 .
8. Preferences,
-.
,
.
9. ,
. Preferences Editor
( General File).
: Build # Debug
Tools , #
,
, Tools, .
,
Build # Debug ,
.
538
VII.
1. C o n f i g u r e d Preferences.
Preferences Tools.
2. A d d d P r o g r a m . Se
lect a File ,
(. ) .
, ,
" ". , CLR
, . "
SharpDevelop" .
3. Csc. .
.
, .
. .
A d d o M e n u Separator ( Program).
( )
.
4.
, .
, T o o l s .
<Enter> Apply.
Build
Debug.
5. , , T o o l s
Preferences, ,
. 22.5.
, . 22.5,
. , .
Command ,
Parameters, , $ F i l e , Initial Folder
$ F i l e D i r . ,
, .
6. Parameters :
/debug
/out:$FileDir\bin\debug\Program.
refs.rsp
*.cs
- .
22.
539
. 22.5.
9. Registers , 1, 2 3.
10. Apply ,
Preferences.
,
.
1. , .
.
2.
Preferences
.
.
.
.
P r o g r a m to R u n ( ).
C s c . ( , "
" ).
M e n u N a m e ( ). B u i l d #
Release.
C o m m a n d (). ,
, : C s c . .
P a r a m e t e r s ( ) .
/out:$FileDir\bin\release\Program.
refs.rsp
*.cs
/debug). .
Initial Folder ( ) . .
540
VII.
Debug Release
.
TextPad .
.
Parameters
Build # Debug Build # Release, Regular Expres
sion Registers.
/debug
Parameters Build # Debug (
Build # Release) / d e b u g .
,
.
TextPad, , Pa
rameters. , /debug * .cs #
.
. $ F i l e
, TextPad ,
. TextPad
.
, , ,
, : #
/ d e b u g . . ,
, ,
, . ,
esc. / ? , , # (
).
/ -.
22. #
541
.
, YourProgram.
/? ,
Main ()
/ ? , .
/out
/out Parameters ,
. $FileDir.
, /out
Build # Debug.
$FileDir: "" TextPad
# (* . cs).
TextPad
. , . DLL ,
( . PDB), .
, ,
Visual Studio SharpDevelop.
. Visual Studio
$FileDir: $FileDir\bin\Debug,
$FileDir\bin\Release.
Visual Studio
/out Parameters
:
/out: $FileDir\bin\release\Program.exe.
.
$FileDir "tools"
TextPad "Tool Parameter Macros".
P r o g r a m . : /out
. Visual Studio , Main ( ) ,
P r o g r a m , , ,
P r o g r a m , s. (
.)
. / out - . ,
. "Program".
Configuration
P r o g r a m /out
. "Program.exe" ,
Parameters - .
@refs.rsp
Orefs.rsp
TextPad. ,
, . #
542
VII.
.
, .
.
1. TextPad F i l e ^ N e w
.
2. : # #.
3. r e f s. r s p .
, Save as Save as
1 1 Files (*.*).
,
. , using, using System;,
#, ,
, .. . DLL,
, using.
Visual Studio . DLL
( (assembly)), P r o j e c t ^ A d d Reference.
( .NET,
:
Mscorlib. dll. , ,
.)
TextPad (Debug Release)
/reference (/ ).
/ ( using)
( , Parameters ,
@ref s . rsp).
, . ,
Parameters!
/ ,
. /:
# #
/:System.Windows.Forms.dll
/ : " : \ P r o g r a m Files\NUnit 2 . 2 \ b i n \ n u n i t . f r a m e w o r k . d l l "
( ,
#) / reference, .
. DLL, System. Windows . Forms,
. DLL NUnit.
.DLL .
#, .
, .
, us i n g (
S y s t e m . W i n d o w s . F o r m s . F o r m ) ,
(Base Class Library BCL) .NET Framework.
. DLL,
/ / :
/ / :
/ / : * . c s
.
/ d e b u g . ( , Parameters,
Debug
Release).
d e b u g . r s p ( ).
* . cs . (
.)
,
.
,
$ F i l e D i r TextPad. ,
. CS.
r e f s . r s p (
).
#
, c s c . r s p , ,
S y s t e m . d l l .
. , #,
c s c . , .
544
VII.
Build # Debug, Build # Release
. , ,
.
Command Results
TextPad. :
, , ,
, , .
,
, NUnit,
.NET SDK.
. Apply
.
, ,
.
, .
, .
Program to Run ( ).
: C s c . .
, .
, .
22. #
545
$FileDir\bin\release\Program. .
Parameters,
, "Program", ).
.
Parameters (). .
Options (). Save All Documents First (
).
Remaining options ( ). .
Run Debug Run Release
, , M a i n ( ) ,
TextPad .
. NUnit, ,
. , NUnit,
.
Program to Run ( ).
C:\Program
Files\NUnit
2 . 2\bin\nunit-gui . (, ,
NUnit).
Menu Name ( ). Run NUnit.
Command (). .
Other options ( ).
.
Build #
Debug, ,
. ,
Debug .NET Program TextPad, .
Program to Run ( ).
, .NET Framework SDK,
C:\Program Files. \GuiDebug
DbgCLR. . "
" .
Menu Name ( ). Debug .NET Program.
546
VII.
Command (). .
Parameters and Initial folder ( ).
.
.
Options (). S a v e All Documents First (
).
.NET, #.
.
TextPad, SharpDevelop.
.NET SDK
TextPad # .NET Frame
work ( SharpDevelop),
.NET Framework SDK. , -,
Web-, Browse
.NET Docs, .
Program to Run ( ).
, Web-. ,
Internet
Explorer
C:\Program
Files\lnternet E x p l o r e r \ I E x p l o r e . .
Menu Name ( ). Browse
.NET Docs.
Command (). .
Parameters (). Windows Explorer .NET
SDK; , C:\Program Files.
, \StartHere . htm.
Other options ( ). .
. WinCV
.NET.
WinCV. \bin .NET SDK.
, Command Preferences.
(Intermediate Language Disassembler)
, #.
. I l d a s m . e x e \bin
.NET SDK. , Command.
Close DOS Window on Exit.
SDK
\ D o c s \ C p T o o l s . chm Windows Explorer .NET SDK.
, TextPad #.
. 22.6 Tools. TextPad
#, , -
22.
547
, NUnit.
Web- w w w . n u n i t . o r g -.
,
, .
Visual Studio, TextPad
SharpDevelop.
NUnit
NUnit -
, Tools.
NUnit.
NUnit Visual Studio
Tools'^ .
NUnit TextPad
T o o l s ^ R u n NUnit.
NUnit SharpDevelop Tools^NUnit.
548
VII.
. 22.7. NUnit ,
#, ,
, .
, ,
, .
,
.
, , .
, , - ? ,
,
. ,
. ,
, .
, NUnit,
. "", ,
. , BankAccount
() ,
.
22.
549
.
. .
( ),
. , .
NUnit
.
.
, ,
: , ,
, , , ,
.. , ,
" "
.
, ,
. ,
.
,
,
,
, , .
.
NUnit
NUnit
,
,
. ( , ,
, public, .
, ).
NUnit :
using System;
using NUnit.Framework;
//
namespace NUnitTestExample
{
//
[]
""
[TestFixture]
// : NUnit
//
class MyTestClass
public
{
// ,
// , // , () (
// )
public MyTestClass() {
}
//
// ;
550
VII.
// ,
//
[SetUp]
// SetUp (
// )
public void M y S e t u p O
{
// ,
//
}
//
// "" [Test]
// public void,
[Test]
public void StringLengthsGoodTest()
{
}
[Test]
public void CountSpacesTest()
{
}
// ,
// [Test])
private int CountSpaces(string s)
{
}
// ...
NUnit
NUnitTestExample -. . 22.8
NUnit TextPad.
Visual Studio SharpDevelop (
SharpDevelop
NUnit).
NUnit
.
u s i n g . using NUnit.
Framework, ( /) nunit. framework. dll
\bin NUnit ; , :\Program
Files\NUnit.
. ,
" " (test fixture),
, , .
, -
NUnitTestExample .
,
TrimAndPad () AlignOutput
ie
22.
551
9, " # " . N U n i t T e s t E x a m p l e .
. 22.8. , TextPad,
, Visual Studio
. #,
. "",
, # .
[TestFixture],
[Test].
NUnit , ,
. . D L L ,
( ).
" " .NET NUnit
( [TestFixture] !) ( [Test])
. ,
( ).
NUnitTestEx
ample -.
. ,
[Setup], [TearDown], [FixtureSetUp]
[FixtureTearDown].
,
. .
NUnit , [ S e t u p ] ,
.
. ,
,
552
VII.
, .
[Setup]
[TearDown].
[TearDown], ,
. ,
null, .
NUnit [FixtureSetUp] [FixtureTearDown], ,
. [FixtureSetUp] ,
[FixtureTearDown] .
,
, .
NUnit
NUnit ,
, , . (, , ,
!)
NUnitTestExample, -.
// StringLengthsGoodTest (
// )
// "" [Test]
[Test]
public void StringLengthsGoodTest()
// public void,
//
{
Console.WriteLine("StringLengthsGoodTest:");
//
// ( S e t u p )
stringsln = new string[] {"Joe
", "Rumpelstiltskin",
" Vanderbilt" };
//
//
stringsOut = Program.TrimAndPad(stringsln);
//
// , 16
// . NUnit.Framework Assert
// , IsTrue, IsFalse
// AreEqual
// . IsTrue
// ,
// , NUnit,
//
Assert.IsTrue(stringsOut[0].Length == 16,
" " ) ;
Assert.IsTrue(stringsOut[1].Length == 16,
"1 " ) ;
Assert.IsTrue(stringsOut[2].Length == 16,
"2 " ) ;
}
22.
553
NUnit.
1. (
[ S e t u p ] [ F i x t u r e S e t U p ] ) .
2. , .
, , ,
. , ,
#,
, , ,
19, " ", 20,
" ".
, .NET Framework ,
.
, ,
, .
"" .
( Web- www. ref actoring. com).
3. :
, . , .
, .
, , .
"", .
, NUnit .
, , ,
, .
.
4. " ".
, ,
. [TearDown] [FixtureTearDown]
.
, .
internal ( private public)
#region/#endregion.
TestSupport.
, , :
TestSupport.StoreSomethingForTestToCheck(something);
,
,
. TestSupport
, ,
.
554
VII.
Assert
NUnit Assert,
NUnit. Assert
.
Assert IsTrue (), IsFalse (), IsNull , IsNotNull (), AreEqual (), AreSame (), Fail () Ignore ( ) .
Assert \doc
NUnit Assertions.html. (
NUnit,
Web- www. nunit. org.)
# Assert,
System.Debug.
{
//
int nSpaces = CountSpaces(stringsOut[i]);
//
// stringsOut
Assert.AreEqual(nSpaces, spacesExpected[i],
" " + i + " " +
" " ) ;
}
.
NUnit
,
.
1. .
,
NUnit.
2. NUnit.
22.
555
. , , .
SharpDevelop NUnit Tools,
SharpDevelop Unit Tests.
TestDriven.NET ( Web- www.
testdriven. net),
NUnit Visual Studio.
3.
NUnit
File^Open
\bin\Debug \bin\Release, ,
, . .DLL.
NUnit . . DLL,
.
NUnit
(), Test.
4. Run.
NUnit ( !),
( )
( ).
5. Errors and Failures
.
,
, : ,
;
( 21, " Visual
Studio"). ( NUnit ,
Console.Output
Console.Error.)
, ,
NUnit.
( Reload
File). Run.
NUnit
.
. 22.7, , NUnit,
. ( ).
FailingTest () (, , MyTestClass
, ).
Run . () .
.
556
VII.
, ,
, NUnit,
. . . .
? ,
,
.
: NUnit,
Visual Studio TextPad. NUnit #?
. (
. .)
NUnit?
Windows ,
, .
,
. Visual
Studio NUnit ,
, Visual Studio (
CLR) NUnit .
, NUnit :
Visual Studio ( Visual Studio);
CLR ( SharpDevelop, TextPad
).
22. #
557
Title "" - N U n i t T e s t E x a m p l e .
N U n i t ( ). . 22.9
, Attach to Process
NUnit .
. 22.9. NUnit
6. Attach.
7. <Alt+Tab>
NUnit. Run.
.
.
,
, .
.
NUnit CLR
NUnit CLR
Visual Studio. , NUnit
TextPad SharpDevelop.
CLR .
1. CLR , T o o l s TextPad
SharpDevelop,
.
558
VII.
2. NUnit ( ).
3. , ,
. CS.
4.
.
5. D e b u g ^ P r o c e s s e s . Processes
n u n i t - g u i . ( n u n i t - c o n s o l e . )
Attach. Close.
6. <F10>
<F11> .
7. .
8. , ..
.
.
, Visual Studio (
CLR) Debug<=>Stop Debugging.
NUnit,
.
, ,
.
TextPad
Windows.
Windows, ,
SharpDevelop.
.
, ,
. ,
, #.
.
,
.
22.
559
,
.
.
, ,
.
1, " Windows- #",
Windows Forms ,
() .
.
Visual Studio,
1, " Windows- # " ,
ImitatingFormDesigner -.
, 1, " Windows- # " ,
, .
, , Visual Studio
, .
Visual Studio ,
. , ,
- , .
#,
textBoxl textBox2:
textBox2.Text = textBoxl.Text;
Windows (
, ), Visual Studio 2005
. Forml,
, .
Program, Main ().
.
Forml.cs: (partial class)
Forml. ( , .)
Forml ( ),
, , .
Forml .Designer, cs:
Forml. , Visual Studio 2005 .
. Forml
Forml. cs.
, .
Program, cs: Main ().
560
VII.
ImitatingFormDesigner
Forml. cs , :
buttonl textBoxl t e x t B o x 2 ,
1, " Windows- #", . 1.10
, .
Forms YourWay.
Forml .Designer, cs ImitatingFormDesigner,
, , .
,
Visual Studio ( SharpDevelop)
, , ,
, .
# 2.0.
.
(partial class), partial:
// 1
public partial class MyClass
{
// MyClass,
//
}
// 2
public partial class MyClass //
{
// MyClass, ,
// ?
}
,
:
;
, ,
, (), .
, ,
.
,
, ,
(, ) .
, .
,
.
22.
561
,
, . ,
, , ,
, ,
.
,
. ,
,
. .
,
Visual Studio, Imitat
ingFormDesigner.
,
,
FormsYourWay -.
,
.
FormsYourWay
, , .
,
Program, cs Main ( ) . Main ()
Forml. .
, Main ( ) . ,
Main ( ) , , Main ()
Windows.
,
, .
( - ).
Button, TextBox Label
System.Windows.Forms.
System.Windows.Forms.Button
buttonl;
using Sys
tem .Windows.Forms.
(
, ).
buttonl
= new System.Windows.Forms.Button()
(
, ). ,
Location Size ( ,
Size , 0).
buttonl.Location = new System.Drawing.Point(40,
buttonl.Name = "buttonl";
562
80);
VII.
buttonl.Tablndex = 1;
buttonl.Text = "Copy";
, ,
( ).
:
buttonl.Click += new Sys
tem. EventHandler (this . buttonl_Click) ;
, ,
Click . .
Forml, .
buttonl_Click ().
Controls ( ),
this.Controls.Add(buttonl);
,
( ; ).
:
private
e)
void
buttonl_Click(object
sender,System.EventArgs
{
//
MessageBox.Show("Hello");
// , FormsYourWay:
textBox2.Text = textBoxl.Text;
,
dows-
.
FormsYourWay
, . 1.10.
1, " Win
#", .
, Button, TextBox, ListBox ..,
Form .
.NET SDK.
, ,
, -
? , ( )
, ?
,
.
22.
563
, .NET? , Microsoft ,
, , ?...
Microsoft .NET
SDK. .NET redistrib
utable package D o t n e t f x . . MSDN
.
,
. N E T ,
. .NET,
, ,
"", (
, D o t n e t f x . .NET).
, Visual Studio (
SharpDevelop) . . MS I- (Microsoft Installer),
.
, .NET SDK.
. MS I-,
. Microsoft , ASP.NET, .
Visual Studio,
.
,
( 50 .) Visual Studio. 19, "
", Visual # Express
.
Visual Studio.
564
VII.
, 775
object, 273
, 302
, 263
, 430
, 244
, 246
, 177; 182
, 339
, 348
, 117
, 231
, 308
, 116
, 181
, 179
, 253
, 242
, 123
, 243
, 242
-, 141
, 561
, 116
, 117; 181
, 227
, 395
, 474
, 57
, 794; 499
, 246
, 77
, 724
, 725
Length, 729
, 725
, 182
, 235; 421
, 230; 261
, 276
, 287
, 720
, 704
, 339
, 347
, 777
, 720
, 59
DOS, 50
, 38
, 38
, 67
, 73
as, 274
break, 98
continue, 98
if, 86
is, 272
, 777
, 74
, 77
, 74
, 76
, 76
, 74
, 58; 75
, 76
, 79
, 83
, 73
, 572
, 574
, 577
__,
311
, 269
, 263
567
, 753; 253; 284
, 767
, 57
, 59
, 57
, 467
, 462; 474
, 81
, 292
, 230; 291
, 425
, 82
, 70
, 59
, 207
, 174; 420; 494
, 421; 422
, 422
, 755
, 67
, 434
, 419
, 300
, 504
, 67
, 290
, 148; 505
, 174; 420; 494
, 427
, 727
, 457
, 207
, 69
, 80
, 285
, 66
, 78
, 720; 131
568
, 67; 799-227
switch, 205
, 200
, 207
, 207
, 205
, 272; 218
, 218
, 275
, 327
, 329
, 333
, 81
, 82
, 62
-, 67
, 57 7
, 226
, 67
FileStream, 438
StreamWriter StreamReader, 435
, 420; 494
, 36
, 747
, 749
, 756
, 757
, 756
, 762
, 88
, 755; 253; 284
-, 747
, 60
, 93
do...while, 98
for, 104
foreach, 134
while, 93
, 203
, 106
, 97
, 47
, 33
, 117; 227
, 38
, 61
569
,
# 2005 ""
..
..
.. , .
..
""
127055, . , . , . 43, . 1
14.11.2007. 70x100/16.
Times. .
. . . 27,6. .-. . 46,44.
1000 . 5558
CtP
" " . . .
197110, -, ., 15.