오버로딩, 참조형 매개변수
- 같은 이름의 함수 (메서드)를 여러개 정의하고,
- 메서드 명은 같고
- 매개변수의 타입과 개수를 다르게 정의하는것이다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public static void main(String[] args) {
Sample1 s = new Sample1();
int result = s.add(3,5); // 값을 던져준다.
System.out.println(result);
int[] result2 = {0}; //배열명, 즉 배열의 시작주소를 가지고있다.
s.add(3, 5, result2);
System.out.println(result2[0]);
}
//오버로딩
int add(int a, int b)
{
return a + b; // 매개변수에 값을 던져줬기때문에 return을 지정해줘야한다.
}
void add(int a, int b, int[] result)
{
result[0] = a + b; // 같은 주소를 바라보고있기때문에 return을 지정해주지 않아도 된다.
}
}
1
2
8
8
오버로딩으로 메서드명은 같고, 매개변수는 다르다.
매개변수로 배열 주소를 받기때문에 리던값에 지정해주지않아도 배열안에있는 [0]번째 행의 값을 바꿀수있었다.
참조형 반환타입
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Data
{
int x;
}
public class Sample1 {
public static void main(String[] args)
{
Data d = new Data(); //d의 1번째 집, 주소 :100층
d.x = 10;
Data d2 = copy(d); // 클래스 매소드라 copy로 바로선언이 가능하다.
System.out.println("d.x =" + d.x);
System.out.println("d2.x = " + d2.x);
}
static Data copy(Data d) //d의 2번째 집, 주소 : 100층
{
Data tmp = new Data(); // tmp의 3번째 집
tmp.x = d.x; // 10 = 10
return tmp; // tmp의 시작주소값 return
}
}
이그림으로 보면
- Data 클래스의 x변수
- Data 클래스의 d변수
- Data 클래스의 tmp변수
- 이 세변수를 참조형 매개변수로 넘겨줘 객체지향적인 변수를 통해
주소값으로 변수에 값들을 넣어주는 소스이다.
- 그림을 보고 잘이해한다면 참조형 변수에대한 객체 생성, 매개변수넘겨주는것을 이해할수있을것이다.
static 붙인 메소드, 인스턴스 메소드의 차이
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package sample;
class calculator
{
int add(int a, int b)
{
return a + b;
}
int multiply(int a, int b)
{
return a * b;
}
int devide(int a, int b)
{
return a / b;
}
static long add(long a, long b)
{
return a + b;
}
static long multiply(long a, long b)
{
return a * b;
}
static long devide(long a, long b)
{
return a / b;
}
}
public class Test {
public static void main(String[] args) {
int result1, result2, result3;
long result5, result6, result7;
calculator cal = new calculator();
result1 = cal.add(2, 3);
result2 = cal.multiply(4, 3);
result3 = cal.devide(2, 4);
System.out.println(result1+ " " + result2 + " " + result3);
result5 = calculator.add(3l, 4l);
result6 = calculator.multiply(3l, 4l);
result7 = calculator.devide(3l, 4l);
System.out.println(result5+ " " + result6 + " " + result7);
}
}
- 이것을 보면 static이붙은 메소드는 클래스.메소드로 쓸수있으며
- static이 붙지않은 메소드는 객체를 생성하여 객체.메소드로 쓸수있는 차이점이 있다.
클래스 멤버와 인스턴스 멤버와 참조와 호출
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
33
34
35
36
37
class MemberCall {
int iv = 10;
static int cv = 20;
int iv2 = cv;
static int cv2 = new MemberCall().iv;
static void staticMethod1() // 클래스 메소드
{
System.out.println(cv); //클래스 변수를호출할수있다.
MemberCall c = new MemberCall(); //인스턴스 변수는 메모리할당이,즉 객체가 필요하다
System.out.println(c.iv); //그다음 호출할수있다.
}
void instanceMethod1() // 인스턴스 메소드
{
System.out.println(cv); //클래스 변수선언할수있다.
System.out.println(iv); //인스턴스 변수를 바로사용가능하다.
}
static void staticMethod2()
{
staticMethod1 ();
MemberCall c = new MemberCall();
c.instanceMethod1();
}
void instanceMethod2()
{
staticMethod1();
instanceMethod1();
}
}
- 클래스메소드,변수는
- 클래스메소드,변수 호출시 바로 호출가능
인스턴스메소드,변수 호출시 인스턴스 생성하고 호출가능
- 인스턴스메소드, 변수는
- 클래스메소드,변수 호출할때 바로 호출가능
인스턴스메소드,변수 호출할때 바로 호출가능하다
- 즉! 클래스 메소드,변수에서 , 다른인스턴스메소드,변수 호출할때 인스턴스생성을 떠올리면된다.
오버로딩
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package sample;
public class OverLoaingTest {
public static void main(String args[])
{
MyMath mm = new MyMath();
System.out.println("mm.add(4,5)의 결과:" + mm.add(4,5));
System.out.println("mm.add(4,5L)의 결과:" + mm.add(4,5L));
System.out.println("mm.add(4L,5)의 결과:" + mm.add(4L,5));
System.out.println("mm.add(4L,5L)의 결과:" + mm.add(4L,5L));
int[] a = {100, 200, 300};
System.out.println("int[] a의 결과" +mm.add(a));
}
}
//오버로딩
class MyMath
{
int add(int a, int b)
{
System.out.print("int add(int a, int b) - " );
return a + b;
}
long add(int a, long b)
{
System.out.print("int add(int a, long b) - " );
return a + b;
}
long add(long a, int b)
{
System.out.print("int add(long a, int b) - " );
return a + b;
}
long add(long a, long b)
{
System.out.print("int add(long a, long b) - " );
return a + b;
}
int add(int[] a) //배열의 시작주소, 길이도 같이 들어온다.
{
System.out.print("int add(int[] a) - " );
int i, result= 0;
for(i=0; i<a.length; i++)
{
result += a[i];
}
return result;
}
}
클래스 초기화 블럭
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
package sample;
public class Sample3 {
//클래스 초기화 블럭
static int[] arr = new int[10];
static int imsi;
static
{
System.out.println("static Start");
System.out.println("imsi [1]: " + imsi);
int i;
for(i=0; i<arr.length; i++)
{
arr[i] = i+1;
}
imsi = 10;
}
public static void main(String[] args)
{
System.out.println("main Start");
System.out.println("imsi [2]: " + imsi);
int i;
for(i=0; i<arr.length; i++)
{
System.out.println("arr[" + i + "] :" + arr[i]);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
static Start
imsi [1]: 0
main Start
imsi [2]: 10
arr[0] :1
arr[1] :2
arr[2] :3
arr[3] :4
arr[4] :5
arr[5] :6
arr[6] :7
arr[7] :8
arr[8] :9
arr[9] :10
클래스초기화블럭 : 앞에 static이붙는다., 메모리에올라갈때 자동으로 생성 인스턴스초기화블럭 : 인스턴스를 새로만든다. -> 메모리를할당 -> 인스턴스 초기화블럭 -> 생성자 만들어진다.