Professional Documents
Culture Documents
CHAPTER
157
12
Class B
Class C
Attributes
Attributes
Attributes
Constructor
Constructor
Constructor
Methods
Methods
Methods
new
new
new
Object a
Object b
Object c
Attributes
Attributes
Attributes
Method-1
.
.
.
Method-1
.
.
.
Method-1
.
.
.
Method-N
Method-N
Method-N
(Package)
(Classes)
(Attributes)
(Constructors) (Methods)
(Instance of Class)
(Objects)
( )
2.
(Package)
()
(Class)
(Attribute)
(Constructor)
(Method)
(Variable)
(Statement)
(if, for, )
(Object)
( )
(Object-Oriented Programming)
2553 ( 7 2/2553) ()
158
1 [ ] (10 )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
2
3
4
5
6
7
8
9
10
1 Class Diagram
Num
obj: Num
RunNum
int x
double y
x = 5
y = 7.0
Num()
Num(int m, double n)
int addNX(int n)
double addNY(double n)
void showXY()
new
addNX(int n)
addNY(double n)
showXY()
obj.showXY();
int a = obj.addNX(4);
...
}
5)
6)
7)
8)
9)
10)
11)
12)
13)
14)
15)
159
2. (Classes)
1.
3
1)
[ ] class <> {
[ ( )]
[ ( )]
[ ( )]
}.
(Modifier)
public final
1
2
3
4
5
6
7
8
*.java
public
1
( )
public (
)
2) 2
[ ] <
> < >;
(Modifier)
+ public
- private
# protected
static
final
2553 ( 7 2/2553) ()
160
1
2
3
4
5
static
static
3)
(Constructor) ( )
[ ] < > ([ ]) {
[
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Return Type
public
4) 8
[ ] <
> <> ([ ]) {
[]
}
1
2
3
4
5
6
7
8
public, private, protected,
static final
8 ( )
2553 ( 7 2/2553) ()
161
3 [ ]
Course
(30 )
public class Course {
// 3 ( ) (3 )
// id private (Course ID)
// title public (Course Title)
// credit protected (Credit)
// 3
( int 0, String "" ) (3 )
// 3 3 (3 )
// setID() public
id (3 )
2553 ( 7 2/2553) ()
162
// getLevel() public
3 2110191 1
2 Undergraduate ( 1-4)
Graduated ( 5 ) (3 )
// getFaculty() public
2110191 21
(3 )
2553 ( 7 2/2553) ()
163
// getDepartment() public
3-4 2110191 10
(3 )
// toString() public
} //End of class
2.
1) (Body)
(Local Variable)
2) (Head)
3)
(Global Variable)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
x y
getX() x
public class Test {
private static int x = 10;
getX(int x)
private static int y = 1;
public static int getX() { return x; }
public static int getX(int x) { return x; }
public static int incX(int y) { return x + y; } x x
public static void main(String[] args) {
y y
int x = 5;
System.out.println(x);
5
System.out.println(y);
1
x x
System.out.println(getX());
10
System.out.println(getX(2));
2
y y
System.out.println(incX(x));
15
}
}
x main() x getX(int x)
2553 ( 7 2/2553) ()
164
4)
public class TestClass {
Attribute
Parameter
) {
Variable
return ;
} //End of method
} //End of class
4 [ ] (10 )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
1) y 10
y y
2) y 14
y y
3. (Objects)
1.
1)
( )
2553 ( 7 2/2553) ()
2)
Class A
Object a
New Object
Constructor
Method-1
.
.
.
Methods
Method-N
2.
1)
Student std;
Car car;
a.attr1 = 0
a.method1()
a.method2()
Attributes
Attributes
165
Account obj;
2) ( new)
< > = new <>();
3)
1 2 ( )
<> < > = new <>();
<> < > = new < >;
<>()
(Constructor)
1
2
3
4
5
6
7
8
9
10
+ +
2553 ( 7 2/2553) ()
166
5 [ ] (8 )
1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
6 [ ] TestNumber
Number
Number (10 )
1
2
3
4
5
6
7
8
9
10
11
x
x
x
x
x
x
=
+
*
/
%
a;
y;
y;
y;
y;
y;
y = b; }
}
}
}
}
}
// TestNumber
import java.util.Scanner;
2553 ( 7 2/2553) ()
167
// 2 no2 Number
//
no2
} //End of main
} //End of class
4.
1.
1) public, private
protected
(Where)
Package java.test1
1
Methods in B call
modifier me()
Class A
2
3
4
Class D
Class B
modifier me()
Package java.test2
Class C Extends A
modifier
modifier
modifier
modifier
me() ( A)
me()
me()
me()
2553 ( 7 2/2553) ()
168
( )
public me()
9
9
9
9
private me()
9
8
8
8
protected me()
9
9
9
8
me()
9
9
8
8
private public
2) static
(How)
( static)
9
9
( static)
9
8
7 [ ] (18 )
1) data
2) var 10
3) check
4) stdName 351
2553 ( 7 2/2553) ()
169
5) m 8 x 5
6) show
show
7) search x
num
8) mulMatrix
9) union
2
2.
1) (
8-11 ) static static
static static
static static
static static
9
8
static static
9
9
2) ( static)
(1) (Class Variable)
<>.< >;
Math.PI;
Color.RED;
2553 ( 7 2/2553) ()
170
Math.pow(2,3);
Time.start();
3) ( static)
(1) (Object Variable)
(Instance Variable)
< >.< >;
std.get(5230121);
c.stop();
static
static
..
8 [ ] (20 )
(A) Class Variable (B) Class Method
(C) Object Variable (D) Object Method
1. Math.sqrt(x)
11. std.grad
2.
p.colorCode(s)
12.
Sqt.borders(a,b)
3.
Array.equals(a, b)
13.
in.readLine()
4.
v.x
14.
System.in
5.
System.getProperties()
15.
Math.random()
6.
Integer.MAX_VALUE
16.
in.hasNext()
7.
kb.nextInt()
17.
i.id_code
8.
rectangle.setSize(w, h)
18.
out.close()
9.
Math.PI
19.
String.format("%4d", x)
10.
a.appendArrays(x,y)
20.
o.name
2553 ( 7 2/2553) ()
171
9 [] (15 )
1
2
3
4
5
6
7
8
9
10
11
12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
static
Global
10 [] Dice
n
6
12 TestDice Dice (20 )
public class Dice {
// public face
// public value
//
2
2553 ( 7 2/2553) ()
172
//
//
// roll
1
value
// setValue
value
// getFace
2553 ( 7 2/2553) ()
173
// getValue
// show
} //End of class
public class TestDice {
/*
d1, d2 d3
3
d1
d2
13
d3
30 9
3
3
20
3 */
} //End of class
2553 ( 7 2/2553) ()
174
// public num
// num Default
// num
// plus
num
// diff
num
} //End of class
2553 ( 7 2/2553) ()
175
/* 1
r 70 RealNumber
15 50
*/
} //End of class
12 [] Account
(10 )
1) balance Object Variable
2) 2
0
3) deposit() Object Method
4) withdraw() Object Method
5) getbalance() Object Method
2553 ( 7 2/2553) ()
176
TestAccount Account 1
2000 1500 800
( ) (10 )
2553 ( 7 2/2553) ()
177
13 [] PiggyBank
1 2 5 10 (20 )
public class PiggyBank {
(3) 2
1) size 100 0
2) size size
0
(4) clear()
( 0)
2553 ( 7 2/2553) ()
178
(5) getTotal()
(7) addOne() 1
(8) addTwo() 2
2553 ( 7 2/2553) ()
179
(9) addFive() 5
(10) addTen() 10
}//End of class
2553 ( 7 2/2553) ()
180
TestPiggyBank PiggyBank
500 34 13
public class TestPiggyBank {
public static void main(String[] args) {
}//End of main
}//End of class
2553 ( 7 2/2553) ()