Professional Documents
Culture Documents
Thramboulidis 2
Thramboulidis 2
Thramboulidis 2
(A )
HPOOPIKH
'
II
(A )
K
E K T H M & T Y
ATPA 2001
HPOOPIKH
'
II
(A )
K
E K T H M & T Y
OMHPO PAO
T M
KPATH KATIKA
T M A
EPAIMO MPATH
BAIIKH HMHTPOOYOY
...
TYPORAMA
/ 19972001
ISBN: 9605381745
K : H 24/3
Copyright 2000
& , 26222 : (0610) 314094, 314206 : (0610) 317244
. 2121/1993,
.
K 1
B A
, ,
E ................................................................................................................. 11
1.1
A K
1.2
o A ............................................................................................................. 16
1.3
K ............................................................. 19
1.4
A
....................................................................................................................................... 19
........................................................................
................................................................................................................................................................
..................................................................................................................................................
13
22
22
K 2
E Java
, ,
E ................................................................................................................. 25
2.1
I Java
2.2
Java
..........................................................................................................................
........................................................
26
28
2.2.1 ................................... 29
2.2.2 Applet
..........................................................................
32
2.2.3 H Applet
.........................................................................
35
................................................................................................................................................................
39
..................................................................................................................................................
39
K 3
K A
, ,
E ................................................................................................................. 41
3.1
3.2
H Java
..........................................................................................
43
...................................................................................................................
46
3.2.1 .............................................................................................................. 46
3.2.2 ....................................................................................... 47
3.2.3 ................................................................................................. 48
3.2.4 ................................................ 49
I I
3.3
3.4
3.2.5 ...................................................................................................................... 50
..................................................................................................... 53
3.3.1 .................................................... 53
3.3.2 .................................................................................................. 53
3.3.3 ............................................................................................. 55
3.3.4 ........................................................................................... 56
3.3.5 ....................................................... 57
M ................................................................................... 58
3.4.1 .................................................................................................... 60
3.4.2 ............................................................................................................ 62
3.4.3 ............................................................................................................. 63
................................................................................................................................................................
..................................................................................................................................................
66
68
K 4
H Java C
, ,
E ................................................................................................................. 69
4.1
T ........................................................................................................................ 70
4.1.1 O Boolean ........................................................................................................... 70
4.1.2
....................................................................................................................
4.1.3
4.1.4
71
............................................................................................................
71
...................................................................................................................
73
4.1.5
..............................................................................................................
74
4.2
T ............................................................................................................................................. 75
4.3
...............................................................
76
....................................................................
78
.............................................................................................................
79
Java C
........................................................................
79
................................................................................................................................................................
80
..................................................................................................................................................
81
EPIEXOMENA
K 5
H K
, ,
E ................................................................................................................. 83
5.1 O ................................................ 85
5.1.1 ............................................................................................... 85
5.1.2 ............................................................................ 87
5.2 ............................................................................................... 91
5.3 K .............................................................................................................. 93
5.3.1 ............................................. 94
5.3.2 ............................................................................................................ 95
................................................................................................................................................................
..................................................................................................................................................
97
98
K 6
, ,
E ................................................................................................................. 99
6.1
..................................................................................................... 101
6.1.1
.................................................................................................................................
101
6.3
........................................................................................................................ 104
6.4
........................................................................................
6.4.1
...............................................................................
104
109
......................................................
6.4.4
...................................................
112
........................................................................
114
..........................................................................................
115
..........................................................................................................................
119
......................................................................................................................................
123
.............................................................................................................................................................
124
6.4.5
6.4.6
6.4.7 Interfaces
111
...............................................................................................................................................
125
I I
K 7
A A E
, ,
E .............................................................................................................. 127
129
7.1
7.2
7.3
................................................................................................................ 132
7.4
Operand
7.5
(GUI)
7.6
.......................................................................................................................................... 136
.............................................................................................
...................................................................................
.........................................................................
...............................
.............................................................................................................................................................
...............................................................................................................................................
129
133
135
139
139
K 8
X E
, ,
E .............................................................................................................. 141
8.1
H ....................................................................... 144
8.2
..........................................................................................................
8.2.2 try/catch/finally
.......................................................................
149
........................................................
152
.................................................................................
154
....................................................................................................
155
.............................................................................................................................................................
158
8.2.3
8.2.4
8.2.5
147
...............................................................................................................................................
159
K 9
, ,
E .............................................................................................................. 161
164
9.1
9.2
......................................
EPIEXOMENA
9.2.1 Thread
.........................................................................................................
9.2.2
9.2.3
..............................................................................
170
.......................................................................................
172
168
................................................................................
173
......................................................................................................................................
174
............................................ 174
.............................................................
175
.....................................................................
176
.............................................................................................................................................................
177
9.3.1
9.3.2 A
...............................................................................................................................................
.........................................................................
178
179
E .......................................................................... 207
.................................................................................
234
.......................................................................................................................................
239
B /
B A
, ,
.
:
,
5 ,
,
,
,
3 ,
2 ,
,
5 ,
,
.
(object)
(class)
(aggregation)
(instance)
(information hiding)
12
1: B A
(message passing)
(object interface)
(class diagram)
(object implementation)
, ,
,
.
.
(complexity) ,
. ,
, ,
. .
, , .
()
.
. (semantic
gap) ,
, (forward traceability)
(backward traceability) [Jacobson 92].
A K
.
[Booch 94] Complexity
.
1.1 A K
;
,
.
, ,
, ,
. T
, , 131,
. , ,
.
;
[1]
.
. , .
.
[1] T
13
14
1: B A
, ,
,
. (instances)
(classes)
.
O , 223
. ! . ,
,
, . ; ;
, .
,
.
(association)
. ,
.
(),
.
.
,
, , , , ... , , , .
,
,
(wholepart) (aggregation).
(aggregate) , ,
, , ... ,
.
A K
15
.
1
fast food
(fast food).
, ,
, , ,
. ,
fast food.
fast food .
,
, ,
.
, , .
, . , . . , , .
.
1/.1, ( ) . , 1.1
. ,
.
1.1
1: B A
16
1234567809.
(identifier). , , .
1.2 o A
fast food ,
. fast food
, , , ,
, , ... fast food
, ,
fast food.
.
.
1.2
1 , .
. .
Objects do things, and we ask them to perform what they do by sending them
messages [Booch 94].[2]
[2]
.
O A
, () .
.
. ( )
. , ,
,
.
. .
, .
. (, , , ,
email, .) .
We view the world as a set of autonomous agents that collaborate to perform
some higher level behavior [Booch 94] [3].
, , ,
START,
.
.
,
.
.
[3] B
17
18
1: B A
, . , ,
, .
fast food , ,
,
. , ,
fast food,
( .) . ,
, ,
.
, ,
,
.
,
. start,
stop open door. , , (interface) . O ,
, , .
(hides) ,
.
(information hiding). , .
: (object implementation)
(object interface) .
, ()
. , ,
(bjects).
, .
, ,
.
1.3 K
, fast food
. , .
. ,
fast food [4]
.
. fast food
, , , .. . .
1.4 A
,
, .
, , ,
. -
[4] ,
,
.
19
20
1: B A
. ,
, ,
.
,
Java,
. ,
, , ,
. :
(reusability)
(extendibility)
(correctness)
(robustness)
(maintainability)
(Object Technology) 3
[Martin 92].
Booch.
,
.
, Booch,
.
, .
. [Booch 94].
, , .
()
21
()
.
. 0, ,
. , , , , .
.
1,
. UML 1.2.
fast food
1.1
, ,
. ,
.
1.3
1: B A
22
,
.
. , , , , ... ,
.
,
(procedural) . ,
.
B
[1] [Booch 94]
B I B I O PA I A
.
.
[3] [Martin 92]
23
E Java
Java
Java .
Java .
:
Java, ,
Java
,
Java.
(keyword)
BNF
ASCII
(syntax error)
Unicode
(semantic error)
(reserved word)
case sensitive
() Simula, Smalltalk,
. Smalltalk
Simula, Bjarne Stroustrup,
C C++,
.
2 : E J AV A
26
.
Java . , Java , Java applet,
Java applet .
2.1 I Java
Java
C++
Smalltalk. H ,
(embedded software) project Green, C C++ . ,
1991
Oak Object Application Kernel.
(clientserver) .
T 1993 NCSA MOSAIC 1.0,
(Web browser)
,
. Sun,
( 93),
1994,
project. 1994
Java
WebRunner[1]. Van Hoff Java Java [2].
1995,
white
paper [Sun 95]. , Sun Java
I J AV A
27
2 : E J AV A
28
[Sun 95][3].
[Sutherland 95].
2.2 Java
Java.
Java.
Java Development Toolkit JDK Sun http://java.sun.com/
products/jdk/1.1/index.html. ,
, .
H Java .
Java :
A (standalone program).
. Java (interpreter).
Applet.
,
applet . applet applet viewer[4].
Java
.
[3] .
[4] E browser
applet.
J AV A
2.2.1
Java
javac. Java bytecodes,
Java.
, bytecodes. Java Java (Java virtual machine)
bytecodes.
(DOS prompt Windows 95 NT shell prompt UNIX) :
javac < >.java
, . [5] .class,
[5] Java.
29
2 : E J AV A
30
bytecodes .
,
, C, Pascal
Fortran. Java , :
java < >
! .class.
Java
bytecodes, Java.
Java
O Java , 2.1,
:
class loader
A bytecode
.
bytecode verifier
A (, )
bytecode ,
Java.
Java
(.. ), .
interpreter
T , ,
bytecode, ,
CPU .
H .
.
, .
J AV A
31
Java source
Bytecode Loader
Java
Bytecodes
Bytecode Verifier
Code Generator
Java Bytecode
Run time
2.1.
,
1.2 Java.
Java .
2.1
2.1
Java
Hello World.
Java
2.2. .
.
public class HelloWorld {
static final String message = "Hello World!";
public static void main(String[] arg){
System.out.println(message);
}
}
2.2
Hello World.
2 : E J AV A
32
Java public .
( ) main, ( Java
C), public static. Java
main. main
, Hello World.
, , editor
public .java. HelloWorld.java.
Java bytecode java
Java:
javac HelloWorld.java
, ,
HelloWorld.class
bytecode
Java. Java
:
java HelloWorld
HelloWorld!
2.2.2 Applet
applet :
1. .
editor .. Notepad.
2. bytecodes.
javac.
3. , HTML , -
J AV A
33
.class
4. applet.
applet viewer
HTML applet.
2.2
2.2
2.3, applet.
.
1 // 1. applet
2 import java.applet.Applet; // import
// Applet
3 import java.awt.*; // import awt
4
5 public class HelloWorld extends Applet {
6
// applet.
// Graphics
// Java.
10
11
2.3
Java applet.
2 : E J AV A
34
:
1: editor
HelloWorld.java. , .
,
.
2: bytecode
javac HelloWorld.java
,
HelloWorld.class. ,
.
, http://java.sun.com/docs/books/tutorial/getStarted
/problems/index.html, Common Compiler and
Interpreter Problems (and Their Solutions).
3: applet ,
HTML[6] , . HTML
:
) text editor
Hello.html,
HelloWorld.class .
:
<HTML>
<HEAD>
<TITLE> applet </TITLE>
</HEAD>
<BODY>
J AV A
Java applet:
<APPLET CODE="HelloWorld.class" WIDTH=350 HEIGHT=200>
</APPLET>
</BODY>
</HTML>
applet,
pixels
applet.
Hello.html.
4: applet
( Hello.html) applet
viewer[7]. browser applet viewer HTML
applet, Java class loader applet. bytecode verifier, applet Java. , applet :
Java applet: Hello World
2.2.3 H Applet
applet .
, ,
. -
35
36
2 : E J AV A
//
( 2).
( 1).
C.
, . . , Java,
. ,
(package).
Java class library Java Applications Programming
Interface (Java API). Java JDK.
, import
. import .
import,
, ( awt 3) ,
( applet 2).
,
. ,
.
. , .
Java . ,
J AV A
( 5).
(subclass)
(derived) , (inherits)
super class. , .
(inheritance) .
applet
. applet , , '
, . Java applet
applet
( 5) public class HelloWorld extends pplet.
( 610)
HelloWorld.
, applet .
,
.
,
. H public ( 5)
( start ).
applet,
HelloWorld
.
, Hello World.
H 8, .
C
.
, paint
37
2 : E J AV A
38
.
paint.
, void. paint public, ( paint) . g, Graphics
.
H paint
drawString g ( 9).
g
Graphics.
drawString
, pixels .
2.3
2.4
2.5
, http://java.sun.com/products/
jdk/1.2/docs/ api/index.html Java TM
Platform 1.2 API Specification Java. awt Graphics.
, ,
.
Java.
applet 2
, , .
. .
applet )
(10, 20), 100 150, )
)
.
.
Y N O H / B I B I O PA I A
39
2.3
,
.
import
extends
applet
Applet
awt
paint
public
Graphics
class
HelloWorld
drawString
, Java
, Java Java applet. , Java applets.
.
applet, , Java.
B
[1] [Sun 95]
Jeff Sutherland Smalltalk, C++ and OO COBOL: The good the bad
and the ugly, OBJECT MAGAZINE May 95.
2.1
K A
Java .
:
5
,
2 ,
2 ,
,
,
,
2 ,
,
(dynamic binding),
,
,
2 2 .
3: K A
42
(abstraction)
(constructor)
(method overloading)
(encapsulation)
this
static
final
(information hidding)
(class variable)
(class method)
(dynamic binding)
, ,
(compile time), ,
(run time),
( ), , , .
.
. ,
,
(Abstract Data Type ADT). ,
Java. , . , ,
Java , , .
, .
. , 1.
3.1
.
,
(stack).
C ) , ) , )
, ) , )
.
3.1 ;
(stack). , ,
,
(Last In First Out/LIFO). ,
.
(procedural) , C:
) push(), pop(), is_empty() clear(),
)
.. int buf[100]; int *buf_ptr=buf;,
, .. struct stack {int buf[100];
int*buf_ptr=buf;} s1;
,
,
.
.
, , stack.c.
static,
, ,
43
3.1
3: K A
44
.
. !
.
( )
( ).
,
.
. ,
pop(). , push(). ,
(methods) . , .
,
Java 3.1.
class stack {
//
push
Java
.
em
pop
3.1.
pty
is
.
:
public ,
private .
stack 3.1, ,
.
, .
private
, . , private void reportError(), push pop
.
(class) (type).
abstract data type (ADT)
,
. , ADT,
Java
.
Java .
, 3.2 ADT ,
[Winder 98]. 3.4
.
ADT
[Meyer 88].
45
3: K A
46
Nane:
Stack of elementType
Operations:
create
Stack
push(Stack.elementType)
Stack
pop(Stack)
Stack
top(Stack)
elementType
isEmpty(Stack)
Stack
Axioms:
3.2
pop(create)=error
pop(push(S,i)) = S
3.2 H Java
3.2.1
, , , , ..
. , ,
.
.
. (x, y) r
. , , ..
:
H J AV A
47
//
private double r;
//
//
public double circumference() {return 2* 3.1416*r;}
public double area () {return 3.1416*r*r;}
}
(struct Circle) C.
3.1
3.2.2
Circle C . .
new :
Circle c;
// c
c = new Circle(); // c
//
//
:
Circle c = new Circle( );
c
new Circle( ).
3: K A
48
3.2.3
. , . ,
, .
,
. ,
.
, , , ,
.
(dynamic binding), .
[1]:
< >.< >([ ]).
c area. c.area().
. .
0, 0.0.
c.area()
area ( c ) . c,
Circle, area(struct Circle).
, ,
H J AV A
49
. area() r
,
.
, c1 1 c2 2
area()
c1.area()
3.1416*1*1
c2.area()
3.1416*2*2
!!! .
area().
c.move(),
move, Circle move.
c, . :
c.r = 2.0;
// private
// r
Java .
private
.
; .
, ,
(encapsulated)
.
. 3.3.2, .
.
3.2.4
public[2]
[2] O Smalltalk,
, .
3: K A
50
:
public class Circle{
//
public double x,y;
public double r;
//
}
public
. 3.3
c :
Circle c = new Circle ( );
// c
// (10,20)
// 2.0.
c.x = 10.0;
3.3
c.y = 20.0;
c.r = 2.0;
3.2.5
,
(information hiding).
,
. ,
.
data abstraction modular programming Modula2 (modules) Ada (packages),
Pascal C. Java,
C++, (encapsulation)
.
H J AV A
51
3.1
(2.0, 2.0) (5.0, 2.0) 3.0 2.0
.
, 3.4
, .
, . ,
.
run . run
main CircleTest1, ( 3)
public. main
( 6 11), ( 79 12)
,
area circumference ( 15, 16 19, 20).
main ( 23) run .
3.1. .
3.2
3: K A
52
// Java
// , , ,
//
System.out.println("Example 2 running");
// a
a.x = 2.0;
//
// a
a.y = 2.0;
a.r = 3.0;
10
11
12
// b
13
14
System.out.println("1 ");
//
// a
15
System.out.println(":" +a.circumference());
16
17
18
System.out.println("2 ");
19
System.out.println(":" +b.circumference());
20
System.out.println(":" +b.area());
21
22
//
// b
23
24 class Circle {
25
26
27
28
3.4
3.1
3.3
3.3.1
:
Circle c = new Circle ( );
new
Circle . ,
( c.area()), , Circle
. ;
. H Java
, .
, , new. ,
, (constructor).
,
, , .
, c
,
Circle , , double.
,
,
. , .
3.3.2
. 3.5 , .
53
3: K A
54
//
//
this.x=x;
this.y=y;
this.r=r;
}
public double circumference( ) { return 2*3.1416*r; }
public double area( ) { return 3.1416*r*r; }
3.5
this
.
,
. ,
,
..
public double area( ) { return 3.1416*this.r*this.r; }
,
3.3 :
Circle c = new Circle( 10.0, 20.0, 2.0);
,
,
private. :
1. .
2.
. . ,
, ,
.
55
3. this, .
this , .
4. ,
void, return.
5. this.
.
,
.
pixels . , area
circumference. .
x1
x2
3.3
+x
y1
y2
+y
(20, 20), (60, 80), pixels.
, .
3.3.3
,
. ,
3.4
3: K A
56
( ), ..
,
, .
3.6 Circle:
public class Circle {
public double x, y, r;
public Circle(double x, double y, double r) {
//
//
//
//
// (0.0,0.0)
// 1.0
3.3.4
, , ,
procedural , C, .
. , Java,
. ,
. (method overloading) -
57
strong typing. ,
,
,
, .
, Java
,
, .
,
,
.
3.3 x, y,
w h Rectangle(int x, int y, int w, int h).
x1,y1 x2,y2,
Rectangle(int x1, int y1, int
x2, int y2). ; .
A
3.2
3.3.5
3.7
this.
this (0.0,0.0,1.0); }
public Circle() {t
.
,
. this, , ,
, .
3: K A
58
.
. ,
,
.
,
.
3.5
6 Kernighan [Kernighan
88], ,
point rect .
x y, . , :
(makepoint), (addpoint),
(ptinrect)
(canonrect). , (,
) .
.
3.4 M
. Circle x ,y r,
,
area() circumference().
. ,
.
. !
count ,
,
.
!
int count;.
Circle, .
Java
C. , , Circle.
. . H :
public class Circle{
private double x,y,r;
private count;
//
}
T ; , , , . : .
count,
. . .
, ,
,
. . .
. .
.
(class variable) static ,
59
3: K A
60
,
. . ,
static.
, (class method) static ,
.
3.4.1
,
, . 3.8 Circle,
.:
public class Circle {
static int count = 0;
public double x,y,r;
public Circle (double x, double y, double r) {
this.x=x; this.y=y; this.r=r;
count++;
}
public double circumference ( ) { return 2*3.14159*r; }
3.8
.
,
, .
, (
) x, y r
count .
. (
public[3]) , , :
System.out.println(" : "+ Circle.count);
3.8, count,
. .
, : , ;.
. Java
C++ delete C
, free(). Java
(garbage collector)
. . ,
. Java .
[3] A private,
.
61
3: K A
62
;
Java (global) . Java
. Java
static
.
,
, public, , ,
. , count ,
.
3.4.2
3.4 3.1416
. (redundancy) C #define
(
const). H Java ,
. , static
final, 3.9. static
( ), final ,
.
.. Circle.PI=3;
X C
public class Circle{
public static final double PI = 3.1415926535; //instance variables
public double x, y, z:
3.9
final
Circle.PI = 2;
(2.0, 2.0) (5.0, 2.0) 3.0 2.0
.
.
3.4.3
,
,
bigger(Circle c), -
63
A
3.3
3: K A
64
c .
bigger.
Circle a = new Circle(2.0);// a
// 2
Circle b = new Circle(5.0); // a
// 3
Circle c = a.bigger(b); //
// a b
H a. Circle =
b.bigger(a). b
c 5.0.
2 :
, , .
Bigger , 3.10.
public class Circle {
public double x,y,r;
public static Circle Bigger(Circle a, Circle b)
{return(a.r>b.r?a:b);}
3.10
:
}
Bigger(Circle a,
Circle b), . Bigger.
Circle a = new Circle(2.0
Circle b = new Circle(5.0);
Circle c = Circle.Bigger(a,b); // c
// a b
65
H
. b
c 5.0.
3.11 .
,
.
.
//
3.11
. , sqrt
Math. Math
sqrt.
A
3.4
3: K A
66
A
3.5
3.6
System.out.println(HelloWorld).
.
System
out
println
Java
rect1((10,20),(50,60)), rect2((30,40),(80,80)) : p1(60,60), p2(20,30), p3(20,70)
p4(40,50).
5.
.
, .
.
,
. ,
,
.
. new
YNOH
. (constructors), ,
, .
(class static variables) , . , (instance variables) . (metaclass), .
.
,
, , .
, Java
.
. ,
, run
.
public main, , , , . ,
, .
67
3: K A
68
B
[1] [IEEE 1983]
H Java C
, Java. Java
C,
.
:
(4)
Java,
, ,
Java ,
(8) , Java,
10 Java C.
stronglytyped
String StringBuffer
byte
instanceof
boolean
exception handling
multithreading
null
native
, 3,
,
.
,
4 : H J AV A C
70
. Java,
, C.
, ,
,
.
C.
,
3.4. , , E
(3.4).
4.1 T
Java ,
. Java stronglytyped . Java
.
Java
,
interface . Java
C :
byte boolean,
,
,
,
, , .
4.1.1 O Boolean
O boolean : .
boolean
.
b=(i!=0);
i boolean b,
b i 0.
i=(b)?1:0;
boolean b i,
i 1 b 0 b.
4.1.2
Java
,
.
Java
, .
, C.
,
, C [1].
4.1.3
Java
(reference types) . , ,
, .
.
c1 c2
clone :
Vector c1 = new Vector;
c2 = c1.clone();
[1] O native methods.
[Gosling 96].
71
4 : H J AV A C
72
c2 () c1.
Cloneable
interface[2].
c1 == c2.
== ,
c1 c2
. , , ,
bigger 3.
null
null Java , C,
. 0 .
, , null.
4.1
, .
. .
double y, x = 1.0;
Circle c1,c2 ;
c1= new Circle(2.0,2.0,x) ;
c2 = c1;
y = x;
System.out.println("c2.x:"+ c2.x + " c2.y:"+ c2.y + "
c2.r:"+ c2.r);
c1.r = x = 4.0;
System.out.println("c2.x:"+ c2.x + " c2.y:"+ c2.y + "
c2.r:"+ c2.r);
System.out.println("x:" + x +" y:" + y);
[2] interface .
73
4.1.4
, ,
.
int [ ] x;
//
//
int [ ] x = new int [10];
: , ,
.
C. Java
, length,
, . < >.length ..
, Java ,
.
.
.
boolean [] [] [] threeDim = new boolean[2][4][6];
boolean 2 4 6.
threeDim.length
threeDim[1].lenthg
4 : H J AV A C
74
A
4.1
A
4.2
Java x 10 0
. .
4.1.5
H C . Java String.
String , . H String , .
StringBuffer. ,
String,
StringBuffer,
String
StringBuffer. ,
String, , + String.
String StringBuffer
.
Java.
String
Hello. ,
String Hello world. . Java String, StringBuffer,
.
.
4.2 T
Java C
. C,
,
:
while ((v = stream.next()) != null)
processValue(v);
while ,
(v = (stream.next() !=
null))
.
v boolean.
Java, C, [ ] . . :
(,) ,
* and &, ,
sizeof.
:
+: +, +, . , -
75
4.2
4 : H J AV A C
76
( 1). ,
toString(). += , .
instanceof: O instanceof ,
interface . false. False null. instanceof
<,<= >=.
4.3
4.3
Java C. , .
4.3.1 if/else, while do/while
int i= 10;
77
int i= 10;
while(i ) {
//
while(i >0) {
if (c1) {
if (c1 != null) {
//
int j;
int j;
do {
do {
} while (j != null) ;
}
}
}
}
()
()
4.1
4.3.2 H switch
H switch C.
case byte,
char, short, int long. ,
switch .
, switch.
4.3.3 for
4 : H J AV A C
78
String str;
, str = "testing"; (i<10)&&(str.length()>=1); i++,
,
for(i=0,
str =str.substring(1)) {
System.out.printin(str);
}
(,)
C.
.
2) H Java ,
for.:
int i=0; i<arr.length; i++)
for(i
System.out.println("a[" + i + "] = " + arr[i]);
,
, .
.
4.3.4 break continue
break continue
C , continue
break. C
.
, . break test;
while, ,
test if(i < len/2). , while if:
for(int i = 0; i < len ; i++) {
test: if(i < len/2) {
J AV A C
79
while (e1) {
if(e2)
break test;
// o
//
//
:
}
:
}
.
4.3.5 goto
goto .
C break continue
try/catch/finally.
6.10 [Gosling 96], goto .
4.4 Java C
, Java. Java:
,
,
( ),
typedef,
struct, union bitfields,
(enum) ,
,
4 : H J AV A C
80
,
void ,
,
(exception handling),
(multiple threads).
2 [Flanagan 96],
Java C.
,
Java. Java
C. ,
C,
, boolean byte,
. . Java ,
String, .
, Java
C, C. :
(&), (*), (,) sizeof,
+, ,
instanceof AND OR
.
Java .
, C goto, break continue
. , , ,
, (threads) .
B I B I O PA I A
B
[1] [Flanagan 96]
David Flanagan, Java in a Nutshell, OReilly and Associates Inc.,
1996.
API Java
.
Java.
[2] [Gosling 96]
Ken Arnold, James Gosling, The Java Programming Language,
Addison Wesley, 1996.
. , James Gosling
Sun.
81
H K
.
:
6 ,
,
,
Java C++ ,
Java,
,
,
5 Java,
.
K
link
aggregation
association
actor
inheritance
server
using
agent
instantiation
public
metaclass
private
5 : H K
84
protected
3
. , , . ,
.
(problem space classes). ,
, (solution space classes) , ,
.
, ,
, . , , .
. ,
, . ,
,
.
5.2 5.3,
. 5.1 8
, -
85
.
, :
area circumference .
.
5.1 O
5.1.1
.
, . Seidewitz Stark
seniority parent/child
. , Booch [Booch 94] Links Aggregation.
Links
link, MT ,
.
.
, (client)
(supplier).
, Booch.
Actor: ,
. active
object.
Server: T ,
.
Agent: T , ,
5.1
5 : H K
86
. agent
actor agent.
, .
,
. :
T .
,
, .
T
.
T .
T
.
, .
A
5.1
Java . .
T ,
;. , .
message passing
method call. , , (threads of control)
,
. ,
, (concurrent) .
Aggregation
, ,
,
. , ,
.
.
,
.
,
.
5.1.2
, , , ,
, , ferryboat, :
T .
T ferryboat .
O .
H .
.
, . , (class diagram),
.
. ,
:
Association
Inheritance
Aggregation
87
5 : H K
88
Using
Instantiation
Metaclass
(inheritance)
. /,
ferryboat.
6,
.
3.4 [Booch 94],
.
Association
Association,
, . ,
, , using.
Using
association .
using (refinement)
association
. H using
link .
Aggregation
(aggregation)
.
,
(members) ,
.
. , ,
(containment by value),
. ,
. [1] .
,
(containment by reference),
. . aggregate
, , , .
, aggregate .
4.1 [Rumbaugh 91].
Instantiation
instantiation, Ada, C++
Eiffel, , inheritance, .
(parameterized generic) CLU (template) .
(concrete) ,
. , Meyer,
, , . Queue
Booch 131 3.4 [Booch 94]. Queue intQueue itemQueue, item [1] E (cardinality) .
89
5 : H K
90
,
(typesafe).
Metaclass Association
metaclass Smalltalk, CLOS ObjectiveC, ( ).
,
. Metaclass metaclass .
.
. ,
. Java,
C++ ,
.
, C++
.
, . , , new
. Smalltalk78
, Smalltalk80
. (advanced)
, .
CLOS .
,
. 3.4 [Booch 94] .
5.2
, . , , , ,
(inconsistent state). ,
.
, . , , .
. public, protected private,
,
,
. . , Smalltalk
C++ Java,
, .
,
.
, , , .
Java :
public: ,
.
91
5 : H K
92
private: E
.
private
. , private
.
protected:
.
:
,
, . ! ,
.
protected private protected ,
.
A
5.2
C1, v1 m1
P1 P2,
v2 m2 C1.
m3
C1, C2 C3, m4 C1, C2 C21, m5
C1, C2, C3 C21. H
extends 6, ,
, ,
. 2.2
package P1;
public class C1 {
.v1;
.m1() {
93
.v2;
.m2() {
}
class C2 extends C1 {
class C3 {
package P2;
import P1;
public class C21 { }
class C22 extends C1 {
class C23 { }
5.3 K
.
( ) :
,
,
.
(scope), ,
, , , interface
. , .
, .
O Winder [Winder 98] :
()
5 : H K
94
()
: O
. .
: .
, . , .
:
(file). ,
C.
.
: O
,
public protected. public
. , public, protected
private .
: K
. ,
.
(nested) .
. , 4.10 [Deitel 98].
5.3.1
, , (toplevel class)
(nested) ,
.
:
public: H
.
import. Java
public interface .
abstract: .
.
.
final: .
, final , (static binding)
, .
5.3.2
(nested) (inner)
, ,
.
. K
,
. static
.
static, . , private, .
static, (member class).
. ,
Java, .
95
5 : H K
96
. 5.1 func C
this .
class A {
class B {
class C {
.
int c;
void func() {
A.this.a
//
//
B.this.b
// b
//
this.c
// c C
}
}
.
int b;
}
int a;
5.1
(local class). :
) ,
public, protected, private static.
) E this, new super[2].
YNOH
,
.
, .
new,
interface.
13.3 13.4 [Winder 98], .
,
( interfaces Java
6). ,
. , ,
,
/ . private. , (debugging). /
private . (modifiability) .
.
.
[2] H super , 6,
.
97
5 : H K
98
B
[1] [Booch 94]
Grady Booch, ObjectOriented Analysis and Design with
applications, second edition, Benjamin/Cummings Publishing
Company Inc, 1994.
[2] [Rumbaugh 91]
J. Rumbaugh, et.al ObjectOriented Modeling and Design, Prentice
Hall International, 1991.
, , , .
OMT (Object Modeling Technique) , UML,
.
, functional model,
links, associations, aggregation, inheritance,
dynamic modeling, ...
[3] [Winder 98]
Rusel Winder, Graham Roberts, Developing Java Software, John
Wiley & Sons, 1998.
, . Java, (robust)
(reliable) . , Java,
.
, ,
.
:
,
,
,
,
,
,
,
,
Java .
inheritance tree
Object based
(superclass)
(polymorphism)
(subclass)
(baseclass)
(early binding)
6 : K
100
(late binding)
.
.
. , Java
,
,
.
( ), ( ).
.
.
.
,
.
, ,
, . ,
,
,
. ,
. , ,
101
. , Java .
6.1
6.1.1
(problem space),
(solution space),
. /
( , single
inheritance) ( , multiple
inheritance) . (superclass), (subclass).
6.1
(inheritance tree).
(base class). , . ,
, .
,
( Object), .
Shape
Rectangle
Ellipse
Triangle
6.1
Circle
6 : K
102
H isa
. , (is not a)
, . ,
(Object Oriented)
(Object based) .
.
,
. , ,
,
. Java.
Smalltalk, Eiffel, ObjectiveC C++.
6.1.2
, .
Ivar Jacobson [Jacobson 93] :
1. O .
2. E
[1] .
3. A ,
.
4. E
.
,
[1] M .
103
.
. 6.2()
. ,
.
6.2().
+ BC
=(ABC)
+ D
=(A D)
+ BC
=(ABC)
+ D
=(A D)
+ E
=(ABCE)
+ F
=(ABCF)
+ E
=(ABCE)
+ F
=(ABCF)
+ K
=(A K)
6.2
()
()
, . ,
6.3.
,
, .
(overriding), /
. , , , ,
. ,
. ,
3.5.1 [Jacobson 93], .
+ B
=(A B)
+ D
=(A D)
+ C
=(ABC)
+ K
=(ABK)
+ E
=(ABCE)
+ F
=(ABCF)
6.3
6 : K
104
spaghetti inheritance
,
.
6.2
,
. (multiple
inheritance), . ,
, ,
6.4.
XYZ
6.4
. .
KM
YM
+ XZ
=(XYZ)
=(YM)
+ K
=(KM)
()
()
, ,
.
4.4 [Rumbaugh 91],
.
6.3
. , 6.5.
-
105
.
,
Bring to Front.
draw. , . .
, ,
draw . ,
, .
draw
.
, (polymorphism). ,
. , draw , ... shape 6.1 draw
.
draw
draw
Bring to Front
draw
6.5
draw
draw
6 : K
106
6.1
,
,
.
.
, (, ) . , C func
func. , Smalltalk, C++, Java ..,
,
. (early binding), ,
.
(late binding). , , .
6.1,
Shape center setCenter draw.
setCenter Shape Shape.
, draw Shape.
:
Shape[] shapes = new Shape[10];
shapes[0] = new Circle(); //
shapes[1] = new ..
// Circle,
Rectangle, Triangle
()
107
()
shapes ,
.
shapes,
draw. for o
for(int i=0; i<10;i++)
shapes[i].draw();
, draw
. , , ,
.
. Smalltalk, C++
CLOS 3 119 [Booch 94].
. , draw, , , .
, draw,
. , setCenter,
, . , ,
, Object Java.
, : ,
.
setCircle
, . , .
()
6 : K
108
()
, , (overhead)
. , .
6.4 H Java
Java .
:
,
,
,
,
,
Java
,
Java ,
interfaces .
H J AV A
109
extends
abstract
abstract
final
super
interface
implements
,
, . . ,
, .
, , ,
7,
. ,
, 7.18 [Deitel 98]
7.19 7.18 interface
abstract .
6.4.1
Circle 3. ,
,
.
draw, , ,
, .. -
6 : K
110
Circle. GraphicCircle
:
public class GraphicCircle extends Circle {
Color outline, fill; // Color
//
//
public void draw() {
//
// x,y r
}
}
extends GraphicCircle
Circle
x, y r draw
. area
circumference , gc
GraphicCircle,
double area = gc.area();
gc area.
. , gc Circle,
Circle c = gc;
6.4.2
,
.
, ' ,
.
H J AV A
111
abstract . , abstract abstract . abstract abstract,
abstract .
6.4.3
Java .
extends, -
6.1
6.2
6 : K
112
, extends. ,
Object . Object,
, ,
Java . 6.6
Java,
Circle GraphicCircle
Object
6.6
Circle
GraphicCircle.
6.3
Circle:
Numder:
System:
String:
Math:
Component
:
GraphicCircle
Double:
Float:
Integer:
Long:
Container:
Button:
List
Panel
Applet
GraphicCircle ,
.
6.4.4
3 Circle
. ,
Circle .
, Java
super . , 6.7.
H J AV A
113
6.7
this.fill = fill;
GraphicCircle.
super . ,
,
.
this () super ()
.
6.4
, Java .
. , super, Java
super ().
, ,
.
super ()
this (). , .
GraphicCircle
6.7.
6.5
6 : K
114
6.4.5
, GraphicCircle
(resolution)
. r,
GraphicCircle :
public class GraphicCircle extends Circle {
Color outline, fill; // Color
//
//
float r;
// resolution in dotperinch.
setResolution
r . r
(shadows) r Circle. super.
super.r
r Circle . H .
((Circle)this).r
H J AV A
115
(cast) this , .
M L, K, x,
K L, , :
this.x
x K
x L
super.x
x M
((L)this).x
super.super.x
((K)this).x
6.4.6
. ,
,
,
(overriding). ,
. ,
Circle area Ellipse c Circle
((Ellipse)c).area()
area Ellipse,
Circle.
.
6.6
6 : K
116
6.7
:
class K {
int x = 1;
int res() {return x;}
}
class M extends K {
int x = 2;
int res() {return x;}
}
public class override_test {
public static void main(String args[]) {
M m = new M();
System.out.println(m.x);
System.out.println(m.res());
K k = (K)m;
System.out.println(k.x);
System.out.println(k.res());
}
}
.
.
;
. Java static , .
private , .
final , final
. ,
(inlining) .
H J AV A
117
:
class K {
int x = 1;
int func() {return x;}
}
class L extends K {
int x;
// x
//
int func() {
//
// func K
x = 2 * super.x;
// x
//
return super.func() + x;
// func K
[2] H .
6 : K
118
6.8
bold
. , .
class K {
// func K
int i = 1;
int func() {return i;}
// i
}
class extends K {
int i=3;
// func
// func
int func() {
i = 2 * super.i;
// i
;
return super.func() + i;
}
void func2() {
super.func());
;
System.out.println(s
((K)this).func());
;
System.out.println((
}
}
public class Test1 {
public static void main(String[] args){
M m = new M();
m.i);
System.out.println(m
((K)m).i);
System.out.println((
System.out.println( ((K)m).func() );
System.out.println( m.func() );
m.func2();
}
}
// i
H J AV A
6.4.7 Interfaces
, ,
.
,
.
abstract .
.
, , .
components (component based development).
, [3]
components .
() components , .
, Java
interface
(implement) .
interfaces abstract interfaces
.
interface ,
6.7. interface
interface
interface.
(conforms) interface
.
, .
119
6 : K
120
object
inherits
6.7
implements
interfaces.
, .
.
, .
,
.
interfaces
. H Java
C++ CLOS Smalltalk,
interfaces Java ,
.
Interface
interface , abstract . interface
Drawable:
public interface Drawable {
public void setColor(Color c);
public void setPosition(double x, double y);
H J AV A
interface abstract
. static
final, . interfaces , . public,
,
public .
public, .
.
, :
interface D extends A, B, C
{
//
}
, C
D, .
, .< >
.
, ,
static, ,
.
Interface
implements
interfaces. .
interfaces static . interface, abstract.
, Drawable
Circle, (extends) Circle -
121
6 : K
122
interfaces
. interface
,
interface . ,
DrawableCircle Circle
Drawable
6.8.
shapes[1] = dr;
shapes[2] = dc2;
drawable[0] = dc1;
drawable[1] = dr;
drawable[2] = dc2;
double area = 0;
for(int i=0;i<shapes.length;i++) {
area += shapes[i].area();
drawables[i].draw(dw);
}
6.8
Y N O H E N O T H TA
(package) P .
, ;
Circle Rectangle
area
circumference.
.
.
, , .
Java:
extends: X
.
interfaces.
abstract: X
.
super: X
.
interface: X .
implements: X
.
, interfaces implements.
123
6.9
6.10
6 : K
124
,
,
, interface
Java
.
.
(objectbased) .
. ,
, (extends)
.
.
.
. Java
interface.
,
.
, .
.
B I B I O PA I A
B
[1] [Booch 94]
Gr.ady Booch, ObjectOriented Analysis and Design with
applications second edition, Benjamin/Cummings Publishing
Company Inc. 1994.
[2] [Jacobson 92]
Ivar Jacobson, ObjectOriented Software Engineering A use case
Driven Approach, AddisonWesley 1992.
[3] [Rumbaugh 91]
J. Rumbaugh, et.al ObjectOriented Modeling and Design Prentice
Hall International 1991.
[4] [Deitel 98]
Deitel & Deitel Java: How to program, Second edition, Prentice Hall
International 1998.
Java,
.
, C C++
Java. ,
.
125
A A E
.
:
, ,
.
incremental development
object collaboration
(coupling)
CRC
problem space
Responsibilities
solution space
Java
.
3.4. , 3.4.
(incremental
development).
7: A A E
128
,
.
.
. ,
calculator MS Windows.
, ,
. . ,
(coupling)
(problem space). ,
(solution space),
.
,
.
. ,
.
http://seg.clab.ee.upatras.gr/calc.htm
. .
7.1
3.4
1. .
129
7.1
.
, 4.2 Identifying
Classes and Objects 4 Classification [Booch 94,]
,
.
1.2 .
, . .
.
.
UML .
7.2
, (responsibilities)
. CRC (Class
Responsibility Collaborations) [Wilkinson 95]. ,
Software Engineering.
Stack
3.
Java Stack. H
7.2
7.1
7: A A E
130
util import
import java.util.*;
Stack, JavaTM
Platform 1.2 API Specification[1], :
Stack( )
boolean empty()
Object peek()
//Looks at the object at the top of this stack
//without removing it from the stack.
Object pop()
// Removes the object at the top of this stack and
//returns that object as the value of
// this function.
Object push(Object item)
Object. ,
t Java,
Object .
, . , Java.
:
Java
. .
Character, Byte, Int, Double,
char, byte, int double.
double d = 12.0;
131
d, ,
. :
Double d = new Double(12.0);
d,
Double, , 12.0.
Object .
Adder
,
operate.
.
,
Operator abstract
. , operate,
().
operate Adder.
.
Operand
Operand ,
,
.
7.2
7.3
7: A A E
132
7.4
Java Operand
double. .
7.3
.
.
12.0 4.0,
.
(object
collaboration) . ;
12.0,
4.0,
Adder,
ResultPresenter.
; main
Calculator, Calc; Calc:
public class Calc {
public static Stack s;
public static Adder add;
public static ResultPresenter rp;
public static Operand op;
public static void main(String[] argv){
.
}
}
OPERAND
main Calc
7.3.
.
. .
operate . .
.
, .
7.4 Operand
addDigit deleteLast
Digit. Operand , StringBuffer.
, buf, 14
bytes. , Operand
(double). H Operand :
class Operand {
private StringBuffer buf;
public Operand() {
buf = new StringBuffer(14);
}
public Operand(double val) {
this();
buf.append(Double.toString(val));
}
133
7.5
7.3
7.4
7: A A E
134
complete. :
public void complete() {
Calc.s.push(new Double(buf.toString()));
reset();
}
7.5
7.6
Operand.
.
Operand.
String StringBuffer Java.
Operand 5
.
( G U I )
135
7.5 (GUI)
.
. ,
(engine)
. , (GUI) calculator
Accessories MS Windows 7.1.
:
Display
DigitButton
ControlButton
OperatorButton
MemoryButton
7.1
(GUI)
calculator
Accessories MS
Windows
7.1
7: A A E
136
7.6
(engine).
DigitButton
(instance variable) .
.
mouse addDigit
Operand .
.
operand
DigitButton. 5
.
.
.
.
,
. Enter . Enter, , complete
Operand Calc.
OperatorButton
AddButton, SubButton, ..
mouse operate Adder, Subtracter, ..
. OperatorButton, operate.
137
7.2
. .
CButton
Onclik clear Calc.
CEButton
Onclik reset Operand Calc.
BackButton
Onclik deleteLastDigit
Operand Calc.
7.3
MCButton
MRButton
MplusButton
MSButton
(integration)
, .
.
13 6 + 8 6 *
O 1. addDigit(1)
Operand Calc.
()
7.4
7: A A E
138
()
O 3. addDigit(3)
Operand Calc.
O Enter. complete
Operand Calc,
push , 13.
O 6. addDigit(6)
Operand Calc.
O Enter. complete
Operand Calc,
push , 6.
O +. operate
Adder Calc. O Adder pop
.
pop .
push 19.
O 8.
O Enter.
O 6.
O Enter.
O .
O *. operate
Multiplier Calc. O Multiplier pop
(2).
pop (19).
push
38.
O Enter. operate
ResultPresenter Calc. O ResultPresenter
pop (38). Display
.
Y N O H / B I B I O PA I A
.
(incremental) , , .
.
, .
.
B
[1] [Wilkinson 95]
Nancy Wilkinson Using CRC cards: An informal Approach to
ObjectOriented Development SIGS Books, AT&T Bell
Laboratories,1995.
139
X E
, Java
.
:
,
,
C ,
,
Java,
,
,
,
Java
,
,
try/catch/finally throws.
(exception)
(exception handling)
resumption model of exception
handling
8: X E
142
try/catch/finally
Throwable
throws
Exception
throw
, ,
.
. ,
pop 3.1
3. pop . , , , ; pop , . , pop
, .
, ;
;
;
.
; C, ,
. ,
( ) .
(exception handling) .
.
, Java
.
,
143
. ,
.
Java 12 [Deitel 98].
fastfood o
fastfood,
1,
. ,
, .
.
. , ,
. ,
, . .
; :
) , ,
, , , ..
, .
.
) .
, ,
( ), .
,
()
8: X E
144
()
(throws an exception)
(throws) . (catch) .
;
;
; ..
.
8.1 H
. ,
,
. ,
.
, bytes, , , ..
(exception handling) (call chain)
. pop .
. C, ,
,
,
(global) .
145
.
,
. ( ) (
) [Gosling 96].
8.1 readFile , 8.2
,
, ,
, , .. . 29 .
read_file {
openTheFile;
determine its size;
allocate that much memory;
read the file into memory;
8.1
closeTheFile;
}
PL/1
, ,
(faulttolerant) .
.
, .
, (throw an exception).
:
; ;.
8: X E
146
ErrorCodeType read_file {
ErrorCode = 0;
OpenTheFile;
If (theFileIsOpen) {
determine_file_length();
if (gotTheFileLength) {
allocate_that_memory();
if (gotEnoughMemory) {
read_file_to_memory();
if (readFailed) errorCode = 1;
}
else errorCode = 2;
}
else errorCode = 3;
closeTheFile;
8.2
X J AV A
8.2 X Java
8.2.1
Java :
,
,
, .
. .
Java API .
throws. throws
.
Java
java.lang.Throwable . H
Throwable : java.lang.Error
java.lang.Exception. Error
(dynamic loading) (virtual machine),
. , RuntimeExceptions, Exceptions,
. , Exception . .
147
8: X E
148
java.io.EOFException
java.lang.ArrayAccessOutOfBound
java.lang.NumberFormatException
java.lang.ArithmeticException
modulo
, .
. 8.3 .
ExceptionTest
BufferedReader , . ,
:
Exception java.io.IOException must be caught, or it must be declared in
the throws clause of this method.
line = br.readline( );
^
IOException throws
.
X J AV A
149
import java.io.*;
public class ExceptionTest {
public static void main(String [] args) {
int num;
num = getNumber();
System.out.println("To :"
+ 2 * num);
}
public static int getNumber(){
String line;
BufferedReader br = new
InputStreamReader (System.in));
BufferedReader
(new
System.out.println(" :");
line = br.readLine();
return Integer.parseInt(line);
}
8.2.2 try/catch/finally
try . try ,
, catch . catch
. catch finally
. finally
, (cleanup code).
8.4 try/catch/finally.
8.3
8: X E
150
try {
//
}
catch (SomeException e1) {
// e1
// SomeException
// .
}
catch (AnotherException e2) {
// e2
// AnotherException
// .
}
finally {
//
//
//
//
//
//
8.4
,
try
: ,
break, continue return
catch ,
try/catch/finally.
try ,
catch . catch
, .
catch .
try catch
finally , ,
catch. To finally , , .
try
finally ,
X J AV A
151
try
break, continue return.
finally 7.4.1 [Gosling 96].
Java
(resumption model of exception handling),
termination model .
Java .
(command line) ,
(GUI). , ,
(default) . , java applets,
. (inconsistent
state), .
catch
Exception;
AException
BException , BException
AException.
class BadCatch {
public void myMethod() {
try{
throw new BException(); //
// BException
}
catch (AException ae) {
.
()
8.1
8.2
8: X E
152
()
}
catch(BException be) {
.
}
}
}
8.2.3
8.3
readLine parseInt :
public class Integer extends Number {
.
public static int parseInt(String s) throws
NumberFormatException
}
public class BufferedReader extends Reader {
8.5,
, ,
8.6.
X J AV A
153
import java.io.*;
public class ExceptionTest1 {
public static void main(String [] args) {
int num = getNumber();
System.out.println("To
:" + 2 * num);
}
//readLine throws
//IOException
return 0;
8.5
8: X E
154
import java.io.*;
public class ExceptionTest2 {
public static void main(String [] args) {
try{
int num = getNumber();
// throws Exception
System.out.println("To :" + 2 * num);
}
catch(Exception e) {
System.out.println(e);
}
catch(NumberFormatException e) {
System.out.println(e);
}
}
public static int getNumber() throws Exception {
String line;
BufferedReader br = new BufferedReader (new InputStreamReader
(System.in));
System.out.println(" :");
line = br.readLine();// readLine throws IOException
return Integer.parseInt(line); //parseInt throws NumberFormatException
}
}
8.6
,
.
main. , main
Exception IOException NumberFormat
Exception.
8.2.4
Java .
Exception ,
Throwable. Throwable
, .
, Exception
X J AV A
155
, .
(attribute) .
.
, Object
.
Throwable. 8.7.
public class NoSuchAttributeException extends Exception
{
public String attrName;
public Object newValue;
NosuchAttributeException(String name, Object value) {
super(" \" + "name");
attrName = name;
newValue = value;
}
}
super, Exception .
,
, . .
8.2.5
, ,
.
throw. throw
8.7
8: X E
156
. H
throw new NosuchAttributeException(name, value);
NosuchAttributeException.
. Java
,
catch , throws .
.
[1] . , stream
stream . 7.5 [Gosling 96]
.
8.3
8.4
8.5
Java;
pop Stack.
.
.
1. java
pop .
[1]
.
X J AV A
157
myMethod pop,
;
8.6
myMethod .
8.7
8: X E
158
8.8
8.1
myMethod 8.7.
, C
Pascal,
.
, , .
,
.
Java
try/catch/finally. To try
, catch . finally
. throws .
catch
.
,
try .
Java (root)
Throwable. , Throwable, Exception.
Error RuntimeException Throwable .
B I B I O PA I A
B
[1] [Anuff 96]
Anuuf Ed, The Java Sourcebook John Wiley and Sons Inc. 1996.
[2] [[Deitel 98]
Deitel & Deitel Java: How to program, Second edition, Prentice Hall
International 1998.
[3] [[Gosling 96]
Ken Arnold, James Gosling, The Java Programming Language,
Addison Wesley, 1996.
[4] [[Horowitz 84]
1993.
Ellis Horowitz Fundamentals of Programming Languages, Computer
Science Press, 1984. , ,
,
.
.
.
[5] [[Sethi 97]
Ravi Sethi, Programming Languages: Concepts and Constructs 2nd
Edition, Addison Wesley 1996. Reprinted with corrections April 1997.
159
.
:
(thread of control),
,
Java ,
,
,
Java .
(thread)
(multithreaded)
(semaphore)
(monitor)
(monitors condition variable)
Runnable interface
, ,
, .
162
9 : T
,
. thread[1] (thread of control).
, ,
.
, , ,
. , (multithreaded).
, , , .
.
.
. . Thread,
Java .
. , ,
, Java .
, .
, .
.
[1] O thread .
.
163
fastfood o
Fastfood .
, fastfood, ,
. 7.
, (collaborate) .
. fastfood
.
fastfood
.
.
. .
,
.
,
.
, .
.
. video clip
.
video .
A
.
1 2
push pop , timeslicing , pop push (interleaving). H ,
()
9 : T
164
()
, .
, .
.
. Adder, , , . (), , Adder .
9.1 B
(concurrent programming)
.
. (memory arbiters),
(semaphores), (monitors)
(randezvous)
.
. ,
, (timeslicing).
.
,
. , ,
.
.
(mutual exclusion)
. ,
( ),
. .
,
Dekker, 3 [Ben 97].
165
9 : T
166
(critical section) ,
,
.
(monitors) , . ,
monitor.
monitor, (shared)
, monitor . ,
[Sethi 97], 5 [Ben 97].
Y J AV A
9.2 Y Java
Java, .
:
Thread,
,
, ,
,
synchronized Java.
Thread
wait
sleep
yield
notify
synchronized
notifyAll
Java ,
. Thread
. , .
,
.
, , .
, 9.3.
167
9 : T
168
. . .
9.2.1 Thread
H Thread .
public Thread(String threadName)
public Thread()
Thread
threadName, "Thread" , .. Thread1, Thread2, ...
Thread ping = new Thread(new String("ping"));
Y J AV A
169
interrupt, .
interrupted true,
false.
class PingPong extends Thread {
String word;
//
int delay;
//
// thread
}
}
}
H suspend ,
resume .
.
stop .
isAlive true, start .
stop run .
9.1
PingPong
9 : T
170
9.1
"PING" "pong"
30 60 .
9.2.2
, ,
(life cycle) ,
9.2. , born.
start, runnable.
start
runnable
sleeping
yield
suspended
ep
end
p
sus
sle
9.2
(life cycle)
.
/O
waiting
wa
it
res
ify
not y All
if
not
dispatch
um
born
running
blocked
/O
Stop complete
dead
Y J AV A
, , .
(relinquishes control),
. running.
running,
. .
:
1. (yield),
,
runnable.
2. (sleep)
, .
3. wait,
, . To notify notifyAll
, .
4. blocked ,
.
runnable, .
5. (suspend),
runnable, resume .
, , stop
.
.
, sleeping. , (yield control), .
171
9 : T
172
Thread.
MIN_PRIORITY ( 1) Thread.M_PRIORITY
( 10). , .
Thread.NORM_PRIORITY ( 5).
9.2.3
Java ,
.
,
. .
.
,
.
.
. Java
.
,
synchronized.
O
.
Y J AV A
synchronized. synchronized .
.
(critical sections). keyword
synchronized synchronized
:
synchronized () ,
,
{ }.
synchronized , (exclusive lock)
. , . . . ,
, , .
H Java, (monitors) C.A.R. Hoare [Hoare 74],
synchronized . , synchronized .
synchronized ,
.
synchronized .
synchronized , , .
9.2.4 wait notify
synchronized ,
-
173
9 : T
174
, synchronized
, .
synchronized , ,
wait,
. notify
synchronized . notifyAll,
.
Java
. Thread
, ,
, .
. ,
.
9.3
. ,
. ( )
, . .
, ,
. .
buffer -
. , , .
. ,
.
4.3 [Ben 97].
. (producer)
(consumer).
.
.
9.3.1
.
,
,
2/.9.
,
int .
,
, sleep, .
Thread.sleep((int)(Math.random() * 3000));
0 3 .
175
9 : T
176
9.2
9.3
9.1
9.2
int, .
int 0
3 . .
2/.9.
Java
, A 3.
9.3.2 A
, .
put get Buffer synchronized
, .
boolean empty,
.
(monitors condition variable). empty put
YNOH
177
condition variable
9.4
Buffer,
synchronized put get.
9.5
1 .
.
.
.
13.8 [Deitel 98]
applet.
Java, . .
9.3
9 : T
178
Java
Thread . interface
Runnable
Thread , Java ,
Runnable interface.
, wait notify .
, syncronized .
B
[1] [Ben 98]
M.BenAri Prentice Hall International,
1998. .
[2] [Hoare 74]
C.A.R. Hoare Monitors:An operating System Structuring concept
Communications of the ACM, vol.17, No.10, Oct. 1974 pp 549557.
[3] [Deitel 98]
Deitel & Deitel Java: How to program, Second edition, Prentice Hall
International 1998.
[4] [Dijkstra 68]
Dijkstra, E.W.: Cooperating sequential processes In F.Genuys (Ed.),
Programming Languages: NATO Advanced Stydy Institute. Academic
Press 1968, London, 43112.
[5] [Sethi 97]
Ravi Sethi, Programming Languages: Concepts and Constructs 2nd
Edition, Addison Wesley 1996. Reprinted with corrections April 1997.
A A A
1.1
,
. UML
[1]:
, , , ,
fast food ().
Fast food
[1]
(class associations).
I I
180
, . ,
. ,
.
, . , .
. 1.2.
, .
2.1
import
extends
applet
Applet
awt
paint
public
Graphics
class
HelloWorld
drawstring
, . . 2.2.3.
.
, .
.
3.1
,
.
3.2
Rectangle(int x1, int y1, int x2, int y2) Rectangle(int x, int y, int w,
int h).
181
! . :
, ,
. , .
, .
, . ,
3.3.3 3.3.4, .
, . .
3.3
:
// 3
public class CircleTest3{
public static void main(String[] args){
Circle a = new Circle(2.0,2.0,3.0);// a
//
Circle b = new Circle(5.0,2.0,2.0);// b
//
// a b 1
System.out.println(" :" + Circle.count);
}
}
class Circle {
static int count = 0;
public static final double PI=3.1415926;
public double x,y,r;
public Circle(double x, double y, double r) {
this.x=x; this.y=y; this.r=r; count++; }
I I
182
.
2*PI*r 2*PI,
.
C,
,
Java. main
count Circle.
, . . .
, . . 1
3 ( 3.3),
( 3.4.1 3.4.2).
.
3.4
public Point(Point p1,Point p2) { x = p1.x + p2.x; y
= p1.x + p2.x; }
, p1 p2,
Point p3 = Point(p1,p2);
,
p1 p2.
:
System.out.println("p3.x:" + p3.x + "\tp3.y:" + p3.y);
Point.
183
,
. .
,
.
.
,
, , .
3.5
System.out.println()
System
out
printIn
, ,
, .
.
. System .
static
. , out ,
(standard output) ,
PrintStream io. PrintStream
println. .
println. .
, .
.
I I
184
4.1
,
x[i] = i + x[i1],
i<x.length,
:
int x[ ] = new int[10];
x[0] = 0;
for(int i=1; i<x.length; i++)
x[i] = i + x[i1];
for (int i = 0; i < x.length; i++)
System.out.println(i + ": " + x[i]);
, ,
for,
3.4, , .
4.2
Circle[] circles = new Circle[12];
for (int i = 0; i < circles.length; i++)
circles[i] = new Circle(point[i], radius[i]);
circles
12 Circle.
(new) (=) for.
, ,
4.1.4.
, Java.
5.1
Client Supplier
:
1) T ,
2) T
, :
class Supplier { }
class Client {
public m(Supplier s) { .. }
}
3) T
:
class Supplier { }
class Client {
Supplier s;
:
}
4)
:
class Supplier { }
class Client {
public m() {
Supplier s;
:
}
}
, ,
. .
[5] Y , .
185
I I
186
.
.
5.2
C1 :
public class C1 {
public v1;
private v2;
public m1() {
private m2() {
m3() {
, ,
,
.
.
6.1
abstract class Shape { }
abstract class TwoDimentionalShape extends Shape { }
abstract class ThreeDimentionalShape extends Shape { }
class Circle extends TwoDimentionalShape { }
class Rectangle extends TwoDimentionalShape { }
class Cube extends ThreeDimentionalShape { }
class Sphere extends ThreeDimentionalShape { }
.
. ,
.
,
.
.
6.2
Circle Rectangle, Shape area circumference,
.
public class Circle extends Shape {
double r;
static final double PI = 3.14159;
public double area() {return PI*r*r;}
public double circumference() {return 2*PI*r;}
}
public class Rectangle extends Shape {
double w,h;
public double area() {return w*h;}
public double circumference() {return 2 * (w+h);}
}
, .
2.4 3,
Circle.
. .
6.3
:
public GraphicCircle(double x, double y, double r, Color
outline, Color fill) {
this.x = x; this.y = y; this.r = r;
this.outline = outline;
this.fill = fill;
}
187
I I
188
GraphicCircle, Circle.
,
.
. 3.3 .
, ! ,
,
Circle.
6.4
super () , this ()
.
, . . , 3.3. .
6.5
public GraphicCircle(double x, double y, double r, Color
outline, Color fill) {
super( x, y, r );
this.outline = outline;
this.fill = fill;
}
6.6
this.x
x K
x L
super.x
x M
((L)this).x
super.super.x
((K)this).x
. ,
6.4.4 .
6.7
2
2
1
2
m.x x
System.out.println(m.x); 2.
m.res() res
System.out.println(m.res()); 2.
k.x x K
System.out.println(k.x); 1.
k.res() res
System.out.println(k.res()); 2.
189
I I
190
, .
. .
. .
; . .
6.8
class K {
int i = 1;
int func() {return i;}
}
class extends K {
int i=3;
int func() {
i = 2 * super.i;
;
return super.func() + i;
// func K
}
void func2() {
super.func());
;
System.out.println(s
((K)this).func());
;
System.out.println((
}
// i
// func
// func
// i
public class Test1 {
public static void main(String[] args){
M m = new M();
m.i);
System.out.println(m
((K)m).i);
System.out.println((
System.out.println( ((K)m).func() );
System.out.println( m.func() );
m.func2();
}
}
// i
, .
.
, ,
, , early
late binding.
.
, :
, . .
6.9
.
.. Const,
. Const.NUMBER_OF_ITEMS.
,
. , , .
, ! .
6.10
Shape area circumference
:
interface Shape {
double area();
double circumference();
}
Circle Rectangle
Shape.:
191
I I
192
Circle
Rectangle , Shape.
:
Shapes[] shapes = new Shapes[10];
Circle c1 = new Circle(2,3,1);
Rectangle r1 = new Rectangle(2.0,4.0);
Rectangle r2 = new Rectangle(6.0,8.0);
Circle c2 = new Circle(2,3,1);
shapes[0] = c1;
shapes[1] = r1;
shapes[2] = r2;
shapes[3] = c2;
double area = 0;
double circumference = 0;
for(int i=0;i<shapes.length;i++) {
area += shapes[i].area();
circumference += shapes[i].circumference ();
}
193
, .
.
.
7.1
:
Calculator
Operand
Operator
Stack
Memory
Adder
Subtracter
Multiplier
Divider
Operator
5 .
,
Calculator . .
,
, . .
, .
, .
7.2
:
,
,
I I
194
,
.
7.3
pop Object.
Double.
Double
. double (
), Double, Object .
Adder,
Operator:
class Adder extends Operator {
public void operate(){
Double d1 = (Double)Calc.s.pop();
Double d2 = (Double)Calc.s.pop();
Double d3 = new Double(d1.doubleValue() +
d2.doubleValue());
Calc.s.push(d3);
}
}
class Adder extends Operator {
public void operate(){
Double d = new Double(((Double)Calc.s.pop()).
doubleValue() +
((Double)Calc.s.pop()).doubleValue());
Calc.s.push(d);
}
}
Calc.s.pop() Object. To
(cast) Double
doubleValue. H ((Double)Calc.s.pop()).double
Value() double +. , double, Double ,
.
Operator:
abstract class Operator {
public abstract void operate();
}
, .
, , .
, , ,.
7.4
:
class Operand {
private double value;
public Operand(double val) { value = val;}
public void add_digit(){};
public void deleteLastDigit(){};
public void reset(){};
public void set(double val) {value = val;}
}
double
, . .
.
set.
set(12.5).
195
I I
196
, complete.
Double ,
. Operand :
class Operand {
//
public void complete() {
Double d = new Double(value);
Calc.s.push(d);
reset();
}
}
H complete
public void complete() {
Calc.s.push(new
Double(value)); reset();
}
, , flush. .
Operand,
3,
.
, ,
.
7.5
:
public static void main(String[] args){
Calc.s = new Stack();
//
// Adder
//
ResultPresenter rp = new ResultPresenter();
197
op = new Operand(12.0);
//
// 12.0
op.complete();
//
//
op.set(4.0);
// 4.0
// op
op.complete();
//
//
ad.operate();
//
// Adder
rp.operate();
//
// ResultPresenter
main.
.
8.1
catch
.
8.2
To catch xception
xception.
xception
catch
.
catch .
8.2.1 8.2.2.
.
, .
I I
198
8.3
Java :
1. A throws,
2. ,
3.
RuntimeExceptions Java. ,
,
(unchecked), .
x/y, y 0,
ArithmeticException, arr[y] ArrayIndexOutOf
BoundException, y arr.
.
.
8.4
pop
.
.
:
1. StackException, Exception,
. .
2. pop . StackException.
3. throws
pop .
,
.
, . .
8.5
.
class Stack {
.
public void pop() throws StackException {
if(isEmpty()) {
throw new StackException(" ");
}
. //
}
}
.
8.6
H myMethod pop,
StackException. :
. myMethod , :
public void myMethod() {
try {
// pop
}
catch (StackException se) {
//
// StackException
}
}
199
I I
200
. myMethod , , ,
.
:
public void myMethod() throws StackException {
// try catch
}
,
. , , throws .
.
, 8.2.3, .
, .
.
8.7
Java , NullPointerException, RuntimeException.
, .
:
. try NullPointerException. H
:
myMethod Begin
NullPointerException
NullPointerException
myMethod End
B. try NullPointerException.
H :
myMethod Begin
myMethod End
. try . H :
myMethod Begin
myMethod End
throws catch .
catch , (recover)
(.. ) , myMethod .
, . 8.2.
, .
Java.
8.8
myMethod, ,
NullPointerException,
( throw e). throws. :
static void myMethod(String myString) throws Exception
{
9.1
TreadTest, PingPong
start.
201
I I
202
, for
main.
O :
class ThreadTest {
public static void main(String [] args) {
PingPong ping = new PingPong("PING",30);
PingPong pong = new PingPong("pong",60);
ping.start();
pong.start();
for(int i=0;i<400;i++)
System.out.print(i + " ");
ping.stop();
pong.stop();
}
}
, for loop,
.
. ,
9.2.1.
[6]
! .
9.2
int ,
(put) (get). O
:
class Buffer {
private int value;
public void put(int val) {value = val;}
public int get() {return value;}
}
[6]
.
value private
.
, .
.
9.3
Thread.
run. sleep,
InterruptedException .
Producer :
class Producer extends Thread {
private Buffer b;
public Producer(Buffer b){this.b = b;}
public void run() {
for(int i=0;i<12;i++) {
try {
Thread.sleep((int)(Math.random() * 3000));
}
catch(InterruptedException e) {
System.err.println(e.toString());
}
b.put(i);
System.out.println("Producer set buffer to" + i);
}
}
}
A Consumer -
203
I I
204
Buffer.
.
, . .
.
9.4
put
get:
condition variable
, .
. . !
, , .
9.5
Buffer value condition
variable , boolean . wait,
InterruptedException, .
notify. Buffer :
class Buffer {
private int value;
private boolean empty = true;
205
206
I I
,
. .
, .
1.1
fast food
fast food
, , , ,
, , , ...
.
.
, .
, ,
. . ,
.
, .
,
.
.
, , .
I I
208
1.2
,
.
K
Fast food
Fast food
.
.
,
, . , .
fast food .
, ,
fast food () fast food.
Unified Modeling Language (UML)[2] [UML
97], ,
. ,
:
209
,
,
fast food:
Fast food
, .
.
,
.
, . , ,
.
, . .
2.1
2.
drawString
.
pixels . 12
15 pixels .
:
// 2 applet
import java.applet.Applet; // import
// Applet
import java.awt.Graphics; // import
// Graphics
public class SecondApplet extends Applet {
// applet.
I I
210
// Graphics
Java.
public void paint(Graphics g) {
g.drawString("", 50, 50);
g.drawString("", 50, 65);
g.drawString(" /", 50, 80);
}
}
+x
Hello
(x, y)
+y
, SecondApplet, .
, .
Java ,
pixels. H drawString
.
2.5
Graphics 1.
drawShapes.java[3]
// 3 .
import java.applet.Applet; // import
// Applet
import java.awt.Graphics; // import
// Graphics
public class drawShapes extends Applet {
public void paint(Graphics g) {
g.drawLine(10,20,110,170);
g.drawRect(10,20,100,150);
g.drawOval(10,20,100,150);
}
}
[3] ,
.
211
,
,
Graphics,
Java. .
, Java. .
, . Java .
3.2
:
main
//
// run
// s.push()
//
// s.pop()
//
// s.clear()
//
I I
212
main
,
. main;
. ,
fast food. .
.
! , .
,
.
.
.
, 3.1
3.2 .
3.3
:
class Rectangle {
private int x1,y1;
//
//
213
private
Rectangle. this.
, , , .
:
class Rectangle {
private int x,y;
//
//
//
//
x, y, w h.
, x1, y1, x2,
y2, :
Rectangle r = new Rectangle(x1, y1, x2x1, y2y1);
, pixels. ,
cm pixels, cm.
, , ,
. , .
I I
214
3.4
:
1 // 4
//
2 public class RectTest{
3 public static void main(String[] args){
4
5
6
System.out.println(":
"+r.circumference());
System.out.println(":" +r.area() +
"\n");
9 }
10
11 class Rectangle {
12
13
14
15
//
}
, RectTest
Rectangle . main,
run,
public. , ,
( 4) (System.out.println) ,
r circumference area
( 6,7).
static. .
3
pixels pixels .
, ,
.
, , ,
3.2 3.3.1 3.3.2, 3.
, ,
.
3.5
makepoint.:
class Point {
public int x,y;
public Point(int x, int y) { this.x = x; this.y = y;}
public void addpoint(Point p) {//
// p
//
//
x += p.x;
y += p.y;
}
}
,
: )
) .
:
class Rect {
215
I I
216
. . Rect
Point. p1 p2
.
,
.
,
:
public Rect(int x1, int y1, int x2, int y2) {
p1.x = x1; p1.y = y1; p2.x = x2; p2.y = y2;}
. ,
. ,
. . .
, , .
p1 p2 , ,
,
. , ,
Point, -
. Rect
Point.
, p1
p2, (
p1 p2 ), :
public Rect(Point p1, Point p2) {
this.p1 = new Point(p1.x,p1.y);
this.p2 = new Point(p2.x,p2.y);}
,
( ), .
1 3.2,
.
ptinrect point, ,
,
public boolean ptinrect(Rect r) {
return(x>r.p1.x && x<r.p2.x &&
y>r.p1.y && y < r.p2.y);}
, ,
.
,
Rect, , . Rect,
! ; .
3.6
. ,
Point.
System.out.println("p1.x:" + p1.x + "\tp1.y:" + p1.y);
217
I I
218
Rect
. :
System.out.println("r1.p1.x:" + r1.p1.x + "\tr1.p1.y:"
+ r1.p1.y + "\tr1.p2.x:" + r1.p2.x
+ "\tr1.p2.y:" + r1.p2.y );
, ptinrect
.
:
// 6
public class RectTest6{
public static void main(String[] argv) {
Point p1 = new Point(60,60);
Point p2 = new Point(20,30);
Point p3 = new Point(20,70);
Point p4 = new Point(40,50);
Rect r1 = new Rect(10,20,50,60);
Rect r2 = new Rect(30,40,80,80);
System.out.println("p1 in:" +(r1.ptinrect(p1)?
"r1":" ")+(r2.ptinrect(p1)?"\tr2":" "));
System.out.println("p2 in:" +(r1.ptinrect(p2)?
"r1":" ")+(r2.ptinrect(p2)?"\tr2":" "));
System.out.println("p3 in:" +(r1.ptinrect(p3)?
"r1":" ")+(r2.ptinrect(p3)?"\tr2":" "));
System.out.println("p4 in:" +(r1.ptinrect(p4)?
"r1":" ")+(r2.ptinrect(p4)?"\tr2":" "));
}
}
class Point {
public int x,y;
public Point(int x, int y) { this.x = x; this.y = y;}
,
3. ,
1 2 .
.
, , . 1 2 .
.
[4] O canonrect(),
.
219
I I
220
4.1
x, y
r Circle public
Circle(double x, double y, double r).
c2 = c1; c2
c1. c1.r = 4.0;
c1, c2.
System.out.println("c2.x:"+c2.x + " c2.y:"+ c2.y + "
c2.r:"+ c2.r);
,
c2.x: 2.0 c2.y:2.0 c2.r:4.0.
x =
4.0; x, y,
System.out.println("x:" + x + " y:" + y);
x:4.0 y:1.0.
4.2
s1 Hello,
Hello World s1
World. ,
String .
StringBuffer s1,
(append) World
StringBuffer. , s1 . s1 ; , . (garbage
collector). :
public class unit4Test1 {
public static void main(String[] args){
221
System.out.println("s1:"+ s1);
StringBuffer buf1 = new StringBuffer(s1);
// StringBuffer
buf1.append(" World");
//
//"world"
s1 = new String(buf1);
System.out.println("s1:"+ s1);
}
}
, .
Java . .
, . ,
. .
4.3
& |
AND OR ,
boolean, AND OR .
(short
circuit evaluation).
&& || AND
OR .
Java. ,
Java.
I I
222
5.1
3. ,
. 3 , ,
.
, !
.
6.1
, . ,
.
, , .
, .
7.2
:
(Calculator)
(Adder)
(Subtracter)
(Multiplier)
(Divider)
(ResultPresenter)
(Operator)
(Memory)
223
(Stack), .
(Expression),
.
A
. ,
.
.
, . .
, .
7.3
:
import java.util.*;
import java.lang.*;
public class Calc {
public static Stack s;
public static Adder add;
public static ResultPresenter rp;
public static Operand op;
public static void main(String[] argv){
s = new Stack();
//
ad = new Adder();
// Adder
//
rp = new ResultPresenter();
op = new Operand(12.0);
//
// 12.0
I I
224
op.complete();
//
//
op.set(4.0);
// 4.0
op.complete();
//
//
ad.operate();
// Adder
rp.operate();
//
// ResultPresenter
}
}
abstract class Operator {
public abstract void operate();
}
class Adder extends Operator {
}
class ResultPresenter extends Operator {
public void operate(){System.out.println(Calc.s. pop());}
}
class Operand {
..
}
import Double.
H
12.0 4.0.
.
,
. .
7.4
:
import java.util.*;
import java.lang.*;
public class Calc {
public static Stack s;
public static Adder add;
public static Subtracter sub;
public static Multiplier mul;
public static Divider div;
public static Equals eq;
public static Operand op;
public static void main(String[] argv){
s = new Stack();
add = new Adder();
sub = new Subtracter();
mul = new Multiplier();
div = new Divider();
eq = new Equals();
op = new Operand();
// Make some calculations
double d1 = 12.0;
double d2 = 4.0;
// addition
op.set(d1);
add.operate();
System.out.print(d1 + " + " + d2 + " = ");
eq.operate();
225
I I
226
// Sub
op.set(d1);
sub.operate();
System.out.print(d1 + " " + d2 + " = ");
eq.operate();
// mul
op.set(3.0); op.flush(); op.set(7.0);
op.flush();
mul.operate();
eq.operate();
// Division
op.set(d1);
div.operate();
System.out.print(d1 + " / " + d2 + " = ");
eq.operate();
}
}
class Operand {
private double value;
public void add_digit(){};
public void set(double val) {value = val;}
public void flush() {Double d = new Double(value);
Calc.s.push(d);}
}
abstract class Operator {
public abstract void operate();
}
class Adder extends Operator {
public void operate(){
Double d = new Double(((Double)Calc.s.pop()).double
Value() + ((Double)Calc.s.pop()).doubleValue());
Calc.s.push(d);
}
}
class Subtracter extends Operator {
public void operate(){
Double d2 = (Double)Calc.s.pop();
Double d = new Double(((Double)Calc.s.pop()).
doubleValue()
d2.doubleValue());
Calc.s.push(d);
}
}
class Multiplier extends Operator {
public void operate(){
Double d = new
Double(((Double)Calc.s.pop()).doubleValue()
* ((Double)Calc.s.pop()).doubleValue());
Calc.s.push(d);
}
}
class Divider extends Operator {
public void operate(){
Double d2 = (Double)Calc.s.pop();
Double d = new
Double(((Double)Calc.s.pop()).doubleValue()
/d2.doubleValue());
Calc.s.push(d);
}
}
class Equals extends Operator {
public void operate(){
System.out.println(Calc.s.pop());}
}
227
I I
228
, . E .
. .
, .
7.5
Operand :
import java.util.*;
import java.lang.*;
public class Calc {
public static Operand op;
public static Stack s;
public static ResultPresenter rp;
public static void main(String[] args){
Calc.s = new Stack();
Operand op = new Operand();
ResultPresenter rp = new ResultPresenter();
op.addDigit('1');
op.addDigit('8');
op.addDigit('2');
op.addDigit('1');
op.complete();
rp.operate();
}
}
class Operand {
// 7.4
}
// ResultPresenter
7.6
:
class Operand {
private StringBuffer buf;
private double value;
public Operand() {
buf = new StringBuffer(14);
}
public Operand(double val) {
this();
buf.append(Double.toString(val));
}
public void addDigit(char ch){buf.append(ch);};
public void deleteLastDigit(){
String s =new String(buf);
buf = new StringBuffer(s.substring(0,s.length()1));
};
public void reset(){buf = new StringBuffer(14);}
public void set(double val) {
reset();
buf.append(val);
}
229
I I
230
StringBuffer
.
8.1
:
import java.io.*;
public class ExceptionTest14 {
public static void main(String [] args) {
try{
System.out.println("\nmyMethod(klea)");
myMethod("klea");
System.out.println("\nmyMethod(kleanthis)");
myMethod("kleanthis");
System.out.println("\nmyMethod(null)");
myMethod(null);
}
catch(Exception e){
System.out.println("main Exception handler");
}
}
static void myMethod(String myString) throws Exception {
//
}
A myMethod main
try . myMethod null
NullPointerException. .
9.1
ProducerConsumer
main :
Buffer,
,
,
.
,
. Java ProducerConsumer, .
,
:
public class ProducerConsumer {
public static void main(string()argv){
Buffer b = new Buffer();
Producer p = new Producer(b);
Consumer c = new Consumer(b);
p.start();
c.start();
}
}
, . 9.2.
.
, .
.
231
I I
232
9.2
1 :
Producer set buffer to 0
Producer set buffer to 1
Consumer retrieved 1
Consumer retrieved 1
Consumer retrieved 1
Producer set buffer to 2
Consumer retrieved 2
Producer set buffer to 3
Consumer retrieved 3
Consumer retrieved 3
Consumer retrieved 3
Producer set buffer to 4
Producer set buffer to 5
, ,
:
1. O (Producer set buffer to
1) (0).
2. O
(Consumer retrieved 1, Consumer retrieved 1)
9.3
1 Buffer
5.
:
233
B
[1] [Ben 98]
M.BenAri Prentice Hall International,
1998. .
[2] [Horowitz 84]
1993.
Fundamentals of Programming Languages Ellis Horowitz,
Computer Science Press, 1984.
.
. .
B
[1] [Deitel 98]
Deitel & Deitel Java: How to program, Second edition, Prentice
Hall International 1998.
Java,
, .
, C
C++ Java. ,
.
[2] [Anuff 96]
Anuuf Ed, The Java Sourcebook John Wiley and Sons Inc. 1996.
B /
235
I I
236
B /
Eiffel.
[13] [Rumbaugh 91]
J. Rumbaugh, et.al ObjectOriented Modeling and Design Prentice
Hall International, 1991.
, ,
. OMT
(Object Modeling Technique) , UML,
.
, functional model, links, associations, aggregation, inheritance, dynamic
modeling, ..
[14] [Sethi 97]
Ravi Sethi, Programming Languages: Concepts and Constructs 2nd
Edition, Addison Wesley 1996. Reprinted with corrections April 1997.
[15] [Sun 95]
James Gosling, Henr McGilton, The Java Language Environment A
White Paper SUN Microsystems 1995.
[16] [Sutherland 95]
Jeff Sutherland Smalltalk, C++ and OO COBOL: The good the bad
and the ugly, OBJECT MAGAZINE May 95.
[17] [UML 97]
Unified Modeling Language: UML semantics version 1.1, Rational
Software, September 97.
[18] [Wilkinson 95]
Nancy Wilkinson Using CRC cards: An informal Approach to
ObjectOriented Development SIGS Books 1995, AT&T Bell
Laboratories
[19] [Winder 98]
Rusel Winder, Graham Roberts, Developing Java Software, John
237
I I
238
abstract
Java .
abstraction
A.
actor
A ,
.
Ada
(embedded) .
Software Engineering reliability, portability,
modularity, reusability, efficiency, maintainability,
information hiding, abstract data types, concurrent
programming, objectoriented programming.
agent
A , .
aggregation
applet
Java
,
Web browser
.
association
base class
boolean
Java :
true false. (cast)
.
byte
Java. 128
127. (cast)
.
bytecode
H
Java. Java Java.
I I
240
C++
Bjarne Stroustrup Bell Labs
80.
C
.
class
K.
class diagram
class method
M .
class variable
M .
CLOS
compatibility
constructor
correctness
O.
coupling
E .
CRC
Class/Responsibility/Collaboration.
derived class
. : .
dynamic binding
early binding
Eiffel
. , .
encapsulation
E.
exception
E.
exception handling
X .
extendibility
E.
extends
final
241
Java
, . final
. final . final .
garbage collector
HotJava
Java applets . (lightweight)
.
HTML
identifier
A.
implements
interface interfaces
.
information hiding
A .
inheritance
K.
instance
instanceof
T Java ,
interface
.
instantiation
interface
Java
.
interpreter
T Java . bytecode, ,
I I
242
, CPU
.
Java API
JDK
late binding
link
maintainability
message
M.
metaclass
M.
method
M.
method overloading
Y .
Microsoft J++
T Java
Microsoft.
monitor
E.
multithreaded
native
.
, C
.
null
E
.
(cast)
0 C.
object
A.
object implementation
Y .
243
object interface
ObjectiveC
.
ANSI C Smalltalk.
C.
package
1. M .
2. Java
package .
polymorphism
private
Java
.
private protected
K .
protected
Java
,
.
protected
.
public
Java
, interfaces,
. Public interfaces
. Public .
reference types
T .
I I
244
resumption model of
exception handling
reusability
E.
robustness
E.
SDE
Software Development
Environment.
semaphore
server
A ,
.
Simula
Smalltalk
K
.
static
Java -
245
K Java .
StringBuffer
K Java .
stronglytyped
M stronglytyped,
,
.
subclass
Y.
super
Java
this,
. , this , super
.
superclass
Y.
synchronized
.
threadsafe.
.
termination model of
exception handling
this
Java. 1)
)
, .. this.x. ) .. this(x,y);. 2)
I I
246
.
thread
N.
throw
Throwable
K Java
.
throws
Java
.
try/catch/finally
Java .
UML
using
M
.
Visual Caf
Java
Symantec.
web browser
T .
O
(state), (behaviour) (identity).
247
K .
, .
M .
B
(, ,
)
.
A -
(abstraction)
(abstract class)
M .
(abstract method)
(base class)
H .
M
. .
M
.
I I
248
H ,
, , ..
T
.
H
.
(scope)
A .
, , , interface .
.
H
.
.
A .
H , ,
.
O
.
O ,
249
H (abnormal)
.
H
. (type).
(member class)
O . .
T ,
(critical section)
(method)
M
.
(operation)
, .
(class method)
(class variable)
.
.
K .
.
I I
250
M
.
.
M
.
E
.
H .
E
. Netscapes
Communicator Microsofts Internet Explorer
.
H .
. .
251
,
.
H .
(containment by reference) .
M -
(containment by value)
.
.
H (composite) (components)
.
M
.
(local class) K .
Java
(reference types)
,
.
T
()
( ).
(superclass)
I I
252
T Java ,
,
.
.
(subclass)
E
.
H
(abnormal) .