Вы находитесь на странице: 1из 26

Java

2.
-

27 2003
:
( Sun ) <vyazovick@itc.mipt.ru>
( Sun ) < gene@itc.mipt.ru>
Copyright 2003 Sun , , .

-
() . Java (
8 ) ,
. , .

-.

Rendered by www.RenderX.com

ii

2. - ............................. 1
1. - ...........................................1
1.1. - ................... 1
1.2. - ........................4
1.3. ................................................................................................................ 5
1.3.1. . ....................................................................................................6
1.3.2. .................................................................................................... 6
1.3.3. ............................................................................................... 7
1.4. ................................................................................................................... 7
1.4.1. ................................................................................................8
1.4.2. ............................................................................................... 9
1.5. .....................................................................12
1.5.1. ................................................................................................... 12
1.5.2. ................................................................................................ 13
1.5.3. ............................................................................................ 14
1.5.4. ................................................................................................ 15
1.6. ............................................................................................... 16
1.7. ..................................................................................................17
1.8. ........................................................................................................... 18
1.9. .......................................................................................... 19

Java

Rendered by www.RenderX.com

. 1 24

2.

.
1. - .......................................... 1
1.1. - ......................... 1
1.2. - ..............................4
1.3. ...................................................................................................................... 5
1.3.1. . ......................................................................................................... 6
1.3.2. ..........................................................................................................6
1.3.3. ..................................................................................................... 7
1.4. ......................................................................................................................... 7
1.4.1. ......................................................................................................8
1.4.2. ..................................................................................................... 9
1.5. .......................................................................... 12
1.5.1. ......................................................................................................... 12
1.5.2. ...................................................................................................... 13
1.5.3. .................................................................................................. 14
1.5.4. ...................................................................................................... 15
1.6. ..................................................................................................... 16
1.7. ........................................................................................................17
1.8. ................................................................................................................. 18
1.9. ................................................................................................19

1. -

1.1. -

. ,
,
,
. ,
Java

Rendered by www.RenderX.com

. 2 24

.
, ,
. FOCAL (FOrmula
CALculator) FORTRAN (FORmula TRANslator).

.
,
.
. - . ,
,


.

.
.
.
,

.
, ,
, .
,
. , Pascal
:
Procedure printGreeting(name: String)
Begin
Print("Hello, ");
PrintLn(s);
End;

- "Hello, Name", Name


.
, ,
.
. ,
.
, , ,
.
,
(),
.

-.
19.701-90. - (. 1.1).

Java

Rendered by www.RenderX.com

. 3 24


.
,
goto .
, "bowl of spaghetti",
, , ,
. ,
goto .

.

,
.

.
, "-".
70- - 80 .
.
,
. ,
, .
, .
"-" "-".
,
, , .
Java

Rendered by www.RenderX.com

. 4 24

1.2. -

, , ,
.
,
.
,
. " "
,
, , .

.
,
.
.
, -
.
,
,
. 80-
,
. -
().
, , ,
. -
.
.
, ,
, ..
,
(. 1.2). -

,
..

,
.

Java

Rendered by www.RenderX.com

. 5 24

,
, .
"", , , .
.

.
() , .
- ,

-
,
.

, ,
.
-

.
.

1.3.
,
,
. :

();

.
Java

Rendered by www.RenderX.com

. 6 24

: , "", , 7461,
..
.
-
.
:
- , ,
.[1]
,
.

1.3.1. .
. ()
, , , .
, , , - ,
. -

.
.
, ( ),
, , . ,
, , ,
.
, ,
:

(, , );

( ).

, , ,
( ).
(state) - : ,
, ;
(,
) (, ) .
[1]

1.3.2.
, .
, :

Java

Rendered by www.RenderX.com

. 7 24


, .. , , , - ().
-
"" "" "" .
, , ,
. ,
.
- "", ""
"" . .. " ",
" " " -
" . . ,
, ,
, .
, ,
. , , ,
.
(behavior) - ,
; .
[1]

1.3.3.
- , . ,
, ()
( , ..)
- - .

.
Identity () , ,
, .
,
, , , Identity .

. , ..
( ) .
, (identity) - ; , .[1]

1.4.

( ). , ,
, - :
Java

Rendered by www.RenderX.com

. 8 24

. ""
"" "", -
.
.
,
. , , :
. ,
, ,
, ..
.
.
(, ), .
, , .
(record),
;
, (
).
-
, . (,
)

UML (Unified Modeling Language, )


:
, .
, - , - ,
().
,
:

(constructor) - ;

(destructor) - ;

,
, .

1.4.1.
(encapsulation) -
(). -
-
.
.
(
). , Java,
. .
Java

Rendered by www.RenderX.com

. 9 24

. ,
. ,
,
.

, .

. -, ,
,
.
-, .
, ,
, ,
.
, , .
,
, ,
.

, .

1.4.2.
-
.
" ". ,
- ,
.
,
- Point, Line, Circle, Box, ..
draw
.
, ,
,
, draw . ,
, :
,
draw.
:

// ,
// Point 1000
Point[] p = new Point[1000];
Line[] l = new Line[1000];
Java

Rendered by www.RenderX.com

. 10 24

Circle[] c = new Circle[1000];


Box[] b = new Box[1000];

// ,
//

for(int i = 0; i < p.length;i++){ // .


// draw() ,
// .
if(p[i]!=null) p.draw();
}
for(int i = 0; i < l.length;i++){
if(l[i]!=null) l.draw();
}
for(int i = 0; i < c.length;i++){
if(c[i]!=null) c.draw();
}
for(int i = 0; i < b.length;i++){
if(b[i]!=null) b.draw();
}


. ,

, Text, Star ..,
,
, .
,
. ,
. Point. ,
(1.3).

draw ,
.

Java

Rendered by www.RenderX.com

. 11 24

, ,
:

Point p[] = new Point[1000];


p[0] = new Circle();
p[1] = new Point();
p[2] = new Box();
p[3] = new Line();

for(int i = 0; i < p.length;i++){


if(p[i]!=null) p.draw();
}

p[] ,
Point. -
, .
, p[0] Circle, draw

p[0].draw()

, .
:
(polymorphism) - ,
(, ) ( )
. , , , [1].
,
.
,
.
, - (overloading).
.
Java PrintWriter,
.
println, /
. :
void println() //
// (true false)
void println(boolean x)
void println(String x) // -

Java

Rendered by www.RenderX.com

. 12 24

. Java
, .
.

1.5.
, , - ,
, .
. ,
, .
,
.
(
,
):

(Aggregation)

(Association)

(Inheritance)

(Metaclass)

1.5.1.
"" " "
. , ,
, .
(aggregation)
, .
.
"contain" . Aquarium
Fish. "",
. .
"home" Aquarium (
), "inhabitants" Fish ( ).
.
, . ..
Aquarium () inhabitants, Fish home.
, , .
"0..n" " ". :

"1..n" - ;

"0" - ;

"1" - ;

"n" - ;

Java

Rendered by www.RenderX.com

. 13 24

"0..1" - .

, , ,
, :
// Fish
public class Fish {
// home ( Aquarium)
private Aquarium home;
public Fish() {
}
}
// Aquarium
public class Aquarium {
// inhabitants ( Fish)
private Fish inhabitants[];
public Aquarium() {
}
}

1.5.2.
,
, ""
, (association).
, , ,
- , .
.
, . ,
- ,
.
--.
Programmer Computer
"0..n", ..
( ).
( ).
, , , , :
public class Programmer {
private Computer computers[];
public Programmer() {
}
Java

Rendered by www.RenderX.com

. 14 24

}
public class Computer {
private Programmer programmers[];
public Computer() {
}
}

1.5.3.
(inheritance) - ,
( ) (
) . "/",
.
.
,
" " " ". ,
. , 4 , ,
.. ,
, 5- 12-.
, "",
" " " "
.

. (- ),
- (- )
,
,
.
.
.

.
"",
"". .. , ,
.

, -
.
-
.
-
.

Java

Rendered by www.RenderX.com

. 15 24

Java
.

1.5.4.
, , . ,
, , ,
, , .
, , ,
. , ,
, ,
, .
, , (,
, , ..).
, .
, ,
. , ,
.
, ,
. , -
, , ,
(, ). ,
(,
, ) (
).
. ,
,
(,
) " " .
, , - . , ,
. ,
, Python. ,
:
set_XXX
get_XXX, XXX, .
, "-".
Java . , - Class
( ) java.lang.
. .class-,
, JVM Class,
. , Java
. Class (static)
. , , .
.

Java

Rendered by www.RenderX.com

. 16 24

1.6.
,
.
. ,
, . ,
.
.

,
,
.

,
,
, .
.

,

.

.
,
. ,
, - .
.
.

.
, ,
. .

.
,
, ,
.

.
,
.

(framework).

, (framework),
, .


- , .
,
.

Java

Rendered by www.RenderX.com

. 17 24

,
.

,
, .

, ,
,
.

,
,
.

1.7.
- ,
. ,
, , ,
. ,
. , , ,
. , ,
,
.
.
, .

. ,
. , ,
, , ,
.
, , ,
. .
, , ,
"" .
.
, . ,
, ,
.
, .
?
, ,
.
1. . Smalltalk

. ,
.
Smalltalk- C-.
Java

Rendered by www.RenderX.com

. 18 24

Oberon-2, Object Pascal C++


.
10% , .
,
.
, :
. ,
.
.
, , (..
, ), .
2. .

. ,
.
. , -

.
3. . ,
.
, .
, . ,
.
- ,
,
.
- .

.
, . ,
, -
. ,
,
.

1.8.
- ,
, .
.
, .
, , ,
.
UML.

Java

Rendered by www.RenderX.com

. 19 24

1.9.
2-1.

-
?

a.) ,

,
. 80-
,
. ().
2-2.

? .
a.) ,
,
. :

();

: , "", ,
7461, ..
2-3.

, :
1. ;
2. ;
3. ;
4. 113-043-429-18.
a.) ,
113-043-429-18 - .
,
. , 113-043-429-18
,
.

Java

Rendered by www.RenderX.com

. 20 24

2-4.

a.) .

, .

.
2-5.

.
a.) :
1. ;
2. ;
3. ;
4. , .

2-6.

? :
1. ;
2. ;
3. .
a.) : -
.

2-7.

?
a.) ,
( ),
, ,
.

2-8.

-
: ? .

Java

Rendered by www.RenderX.com

. 21 24

a.) : . . ,
,
, .
2-9.

: , , .
?
.
a.) :

2-10. :
1. Java2:
2. 342 Java2:
3.
4. ()
.
a.)

2-11. , .
, , ..
( ,
), ( ).
, , 2 :
1. ( , )
2. ( ,
)
Java

Rendered by www.RenderX.com

. 22 24

.
a.) :
1.

2.

2-12. . ,
.
a.) :

2-13. , , .
,
, ,
.
a.) : ,
.
, .

.
Java

Rendered by www.RenderX.com

. 23 24

2-14. ,
:

- ;

, .
a.) :
:

- ;

, .

Java

Rendered by www.RenderX.com

Java ()

Java

Rendered by www.RenderX.com