๋ฌธ์ฅ๋ค์ ๋ฌถ์ด ๋์ ๊ฒ { } โ ์ด๋ ๊ฒ
์์
๋จ์๋ก ๋ฌธ์ฅ๋ค์ ๋ฌถ์ด์ ์ด๋ฆ ๋ถ์ธ ๊ฒ
๊ฐ(์
๋ ฅ)์ ๋ฐ์์ ์ฒ๋ฆฌํ๊ณ , ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ(์ถ๋ ฅ)
ํ๋ก๊ทธ๋๋ฐ์์ ์ฝ๋์ ์ค๋ณต์ ์ข์ง์๊ฒ ๋ณธ๋ค๐ต
๋ฉ์๋ == ํจ์ ๊ฑฐ์ ๊ฐ์ ๋ง
๋ฉ์๋๋ ํด๋์ค์์ ๋ฐ๋์ ๋ค์ด๊ฐ์ผ ํจ, ํจ์๋ ๋
๋ฆฝ์ ์
๋ฉ์๋๋ ๋ธ๋๋ฐ์ค๋ผ๊ณ ๋ ํจ
int add(int x, int y) { // int๋ ๋ฐํํ์
(์ถ๋ ฅ) add๋ ๋ฉ์๋์ด๋ฆ int x, int y๋ ๋งค๊ฐ๋ณ์์ ์ธ(์
๋ ฅ)
int result = x + y;
return result; // ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ
}
๋ฉ์๋์ ์ฅ์
๋ฉ์๋์ ์์ฑ
๋ฉ์๋ = ์ ์ธ๋ถ + ๊ตฌํ๋ถ
// ๋งค๊ฐ๋ณ์ ๋ชฉ๋ก ์ ๋ ฅ, ์์ ํ์ํ ๊ฐ๋ค ๋ฃ๊ธฐ (0~n๊ฐ) - ์ ์ธ๋ถ
๋ฐํํ์ (์์ ๊ฒฐ๊ณผ ํ์ ) ๋ฉ์๋์ด๋ฆ (ํ์ ๋ณ์๋ช , ํ์ ๋ณ์๋ช ,...)
{
// ๋ฉ์๋ ํธ์ถ์ ์ํ๋ ์ฝ๋
} - ๊ตฌํ๋ถ
int add(int a, int b) {
int result = a + b;
return result; // ํธ์ถํ ๋ฉ์๋๋ก ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ค
}
//์ถ๋ ฅ ๊ฐ์ 0~1๊ฐ , 2๊ฐ๋ ์๋จ
๋ฐํํ ๊ฐ์ด ์์๋ ๋ฐํํ์ ์ void๋ฅผ ๋ฃ์ผ๋ฉด ๋จ
์ง์ญ๋ณ์(lv) : ๋ฉ์๋ ๋ด์ ์ ์ธ๋ ๋ณ์ (๋ฉ์๋ ์์ญ)
int add(int x, int y) { // ๋งค๊ฐ๋ณ์ ์
๋ ฅ
int result = x + y;
return result;
}
int multiply(int x, int y) {
int result = x * y;
return result;
}
๊ฐ๊ฐ์ ๋ฉ์๋๋ก ๋ณ์์ด๋ฆ์ด ๊ฐ์๋ ๊ฒน์น์ง ์์
๋งค๊ฐ๋ณ์๋ ์ง์ญ๋ณ์~
๋ฉ์๋๋ฅผ ๋ถ๋ฌ์ ์์
์ ์ํด
ํธ์ถ ๋ฐฉ๋ฒ์?
๋ฉ์๋์ด๋ฆ(๊ฐ1, ๊ฐ2, ...); // ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ๋ฐฉ๋ฒ, ์์
์ ํ์ํ ๊ฐ๋ค ๋ฃ์ผ๋ฉด ๋จ
print99danAll(); // void print99danAll() ํธ์ถ, ์
๋ ฅ๊ฐ ๋ฑํ ๋ฃ์ ํ์ ์์
int result = add(3,5); // int add(int x, int y)๋ฅผ ํธ์ถํ๊ณ , ๊ฒฐ๊ณผ๋ฅผ result์ ์ ์ฅ
class MyMath { // 1. MyMath ํด๋์ค ์์ฑ(๋ฉ์๋ ์์ฑ)
long add(long a, long b) {
long result = a + b;
return result;
//return a + b; //์์ ๋ ์ค์ ์ด์ ๊ฐ์ด ํ ์ค๋ก ๊ฐ๋จํ ํ ์ ์๋ค.
}
// ๋ ๊ฐ์ ๋ฐ์์ ๋ ์ค์ ํฐ ๊ฐ์ ๋ฐํํ๋ ๋ฉ์๋๋ฅผ ์์ฑํ์์ค
long max(long a, long b) {
long result = 0;
if(a>b) {
result = a;
} else {
result = b;
}
return result;
}
long subtract(long a, long b) {
return a - b;
}
long multiply(long a, long b) {
return a * b;
}
double divide(double a, double b)
return a / b;
}
}
class Ex6_4 {
public static void main(String[] args) {
MyMath mm = new MyMath(); // MyMath ๊ฐ์ฒด ์์ฑ
long result1 = mm.add(5L, 3L); // MyMath ๊ฐ์ฒด ์ฌ์ฉ (๊ฐ์ฒด์ ๋ฉ์๋ ํธ์ถ) , add๋ฉ์๋ ํธ์ถ
long result2 = mm.subtract(5L, 3L);
long result3 = mm.multplay(5L, 3L);
double result4 = mm.divide(5L, 3L);
System.out.println("add(5L, 3L)= " + result1);
System.out.println("subtract(5L, 3L)= " + result2);
System.out.println("multiply(5L, 3L)= " + result3);
System.out.println("divide(5L, 3L)= " + result4);
}
}
์์ ์ ๋ง์น๋ฉด ํธ์ถํ ๊ณณ์ผ๋ก ๋์์ด!
๋ฉ์๋๋ ํด๋์ค์์ญ์๋ง ์ ์ ๊ฐ๋ฅ!
์คํ ์ค์ธ ๋ฉ์๋๋ฅผ ์ข
๋ฃํ๊ณ ํธ์ถํ ๊ณณ์ผ๋ก ๋๋์๊ฐ๋ค
๋ฐํํ์
์ด void ์ผ ๊ฒฝ์ฐ ์๋ต๊ฐ๋ฅ, ์ปดํ์ผ๋ฌ๊ฐ ์๋ ์ถ๊ฐ
๋ฐํํ์ ์ด void๊ฐ ์๋ ๊ฒฝ์ฐ, ๋ฐ๋์ return๋ฌธ์ด ํ์ํจ!
int multiply(int x, int y) {
int result = x * y;
return result; // ๋ฐํ ํ์
์ด void๊ฐ ์๋๋ฏ๋ก ์๋ต๋ถ๊ฐ
}
int max(int a, int b) {
if(a > b)
return a; // ์กฐ๊ฑด์์ด ์ฐธ์ผ๋๋ง ์คํ๋จ
} // ์๋ฌ๋ธ : return๋ฌธ์ด ์์์ด๋ผ๊ณ ๋ธ , ์กฐ๊ฑด์์ด ๊ฑฐ์ง์ผ ๋ ๋ฆฌํด๋ฌธ์ด ์์ด์ ์๋ฌ๋๋๊ฑฐ์!
int max(int a, int b) {
if(a > b)
return a; // ์กฐ๊ฑด์์ด ์ฐธ์ผ๋๋ง ์คํ๋จ
else
return b; // ์กฐ๊ฑด์์ด ๊ฑฐ์ง์ผ ๋ ์คํ๋จ
} // ์ด๋ ๊ฒ ๋ง๋๋ฉด ์๋ฌ ์๋น๋๋ค~~
๋ฉ์๋์ ๊ตฌ๋ถ
ํด๋์ค์ ๋ฉ์๋(method)๋ ์ด๋ ํ ์์ ์ ์ํํ๊ธฐ ์ํ ๋ช ๋ น๋ฌธ์ ์งํฉ์ด๋ผ ํ ์ ์์ต๋๋ค.
ํด๋์ค ๋ด์์ ๋ฉ์๋๋ static ํค์๋์ ์ฌ๋ถ์ ๋ฐ๋ผ ๋ค์๊ณผ ๊ฐ์ด ๊ตฌ๋ถ๋ฉ๋๋ค.
์์
class Car {
boolean door; // ์ธ์คํด์ค ๋ณ์
void openDoor() { // ์ธ์คํด์ค ๋ฉ์๋
door = true;
}
static void toggleDoor(boolean d) { // ํด๋์ค ๋ฉ์๋
return !d;
}
}
ํด๋์ค ๋ฉ์๋์ ์ธ์คํด์ค ๋ฉ์๋
์์ ๊ฐ์ด static ํค์๋๋ฅผ ๊ฐ์ง๋ ๋ฉ์๋๋ฅผํด๋์ค ๋ฉ์๋(static method)
๋ผ๊ณ ๋ถ๋ฆ!
๋ํ, static ํค์๋๋ฅผ ๊ฐ์ง์ง ์๋ ๋ฉ์๋๋ ์ธ์คํด์ค ๋ฉ์๋(instance method)๋ผ๊ณ ํฉ๋๋ค.
ํด๋์ค ๋ฉ์๋๋ ํด๋์ค ๋ณ์์ ๋ง์ฐฌ๊ฐ์ง๋ก ์ธ์คํด์ค๋ฅผ ์์ฑํ์ง ์๊ณ ๋ ๋ฐ๋ก ์ฌ์ฉ ๊ฐ๋ฅ!
๋ฐ๋ผ์ ํด๋์ค ๋ฉ์๋๋ ๋ฉ์๋ ๋ด๋ถ์์ ์ธ์คํด์ค ๋ณ์๋ฅผ ์ฌ์ฉํ ์ ์๋ค
๊ทธ๋ฌ๋ฏ๋ก ๋ฉ์๋ ๋ด๋ถ์์ ์ธ์คํด์ค ๋ณ์๋ ์ธ์คํด์ค ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ง ์๋ ๋ฉ์๋๋ฅผ
ํด๋์ค ๋ฉ์๋๋ก ์ ์ํ๋ ๊ฒ์ด ์ผ๋ฐ์ ์
๋ค์ ์์ ๋ ํด๋์ค์ ๋ฉ์๋๋ฅผ ์ข ๋ฅ๋ณ๋ก ์์ฑํ๊ณ ์ ๊ทผํ๋ ์์ ์ ๋๋ค.
class Method {
int a = 10, b = 20; // ์ธ์คํด์ค ๋ณ์
int add() { // ์ธ์คํด์ค ๋ฉ์๋
return a + b;
}
static int add(int x, int y) { // ํด๋์ค ๋ฉ์๋
return x + y;
}
}
public class Member02 {
public static void main(String[] args) {
System.out.println(Method.add(20, 30)); // ํด๋์ค ๋ฉ์๋์ ํธ์ถ
Method myMethod = new Method(); // ์ธ์คํด์ค ์์ฑ
System.out.println(myMethod.add()); // ์ธ์คํด์ค ๋ฉ์๋์ ํธ์ถ
}
}
์คํ ๊ฒฐ๊ณผ
50
30
์์ ์์ ์ฒ๋ผ ํด๋์ค ๋ฉ์๋๋ ์ธ์คํด์ค ๋ณ์๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ,
์ ๋ฌ๋ ๋งค๊ฐ๋ณ์๋ง์ผ๋ก ๋์ํ๋ ๋ฉ์๋์
์ฐธ์กฐ
์๋ฐ์ ์ ์ ๊ธฐ์ดํธ codechobo.com
http://www.tcpschool.com/java/java_member_method