<Point class>
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
|
package ex02.oop_constructor;
//<<<<<<<<<<<<<<2.이클립스 기반 class 만들기>>>>>>>>>>>>>>
public class Point {
private int x, y;
/*<생성자 함수>
-멤버변수의 초기화 담당, 클래스명과 동일해야함,
-리턴타입이 없다(void 조차 사용하지 않음),
-중복정의 가능함(overload 가능-중복함수),
-default constructor(생성자) 갖고 있음
->단, 사용자가 생성자 함수를 재정의하면 디폴트 생성자 함수 기능 상실함*/
public Point() { //1.매개변수 없는 생성자 함수(=default constructor)
// x = y = 100; //멤버변수 초기화
System.out.println("default constructor");
x = 5;
y = 90;
}
public Point(int xx) { //2.매개변수 1개 생성자 함수
System.out.println("매개변수 1개 생성자 함수 constructor");
x = xx;
y = 99999;
}
public Point(int xx, int yy) { //3.매개변수 2개 생성자 함수
System.out.println("매개변수 2개 생성자함수 constructor");
x = xx;
y = yy;
}
//setter, getter 자동완(멤버변수는 만들어놔야 함)
//art + shift + s => 단축키
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
//츨력 method
public void disp() {
System.out.println("x = " + x + "\ty = " + y);
}
}
|
cs |
▶생성자 함수
-멤버변수의 초기화 담당
-클래스명과 동일해야함
-리턴타입이 없다(void 조차 사용하지 않음)
-중복정의 가능(overload 가능-중복함수)
-default contructor(생성자) 갖고 있음
->단, 사용자가 생성자 함수를 재정의하면 default 생성자 함수는 기능을 상실함
-생성자함수는 상속이 안된다 !!
▶getter, setter함수와 생성자 자동 생성방법
1. art + shift + s => 단축키
2. Generate Getters and Setters... -> getter, setter 함수 생성
Generate Constructor using Fields... -> 인자가 있는 생성자 생성
Generate Constructor from Superclass... -> default 생성자 생성
▶public void setX(int x) { this.x = x; } 에서
int x는 외부에서 가져온 값을 받아오는 변수!!
▶public int getX() { return x; } 를 보면,
get은 어떤 타입의 데이터를 return할건지 알려주고, 값을 보내야 하니까 return 필수!
<Circle class>
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
|
package ex02.oop_constructor;
public class Circle {
private int x, y, r;
public Circle() { //1.default 생성자
x = y = 20;
r = 5;
}
public Circle(int x) {
this.x = x;
}
public Circle(int x, int y, int r) {
this.x = x;
this.y = y;
this.r = r;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
public void disp() {
System.out.println("x = " + x + "\ty = " + y + "\tr = " + r);
}
}
|
cs |
<Rect class>
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
|
package ex02.oop_constructor;
public class Rect {
int x,y,x1,y1;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getX1() {
return x1;
}
public void setX1(int x1) {
this.x1 = x1;
}
public int getY1() {
return y1;
}
public void setY1(int y1) {
this.y1 = y1;
}
public void disp() {
System.out.println("x = " + x + "\ty = " + y);
System.out.println("x1 = " + x1 + "\ty1 = " + y1);
}
}
|
cs |
<main이 들어있는 MaintEntry class>
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
|
package ex02.oop_constructor;
//<<<<<<<<<<<<<<2.이클립스 기반 class 만들기>>>>>>>>>>>>>>
public class MainEntry {
public static void main(String[] args) {
System.out.println("<<Point class>>");
Point pt = new Point(); //생성자함수 자동호출
pt.disp(); //default값인 0,0으로 나옴 -> 초기값을 정하고 싶다면? -> 생성자 함수 이용
Point p2 = new Point(45);
p2.disp();
Point p3 = new Point(1,2);
p3.disp();
System.out.println("\n--------------------\n");
System.out.println("<<Circle class>>");
Circle c = new Circle();
c.disp();
System.out.println("\n--------------------\n");
System.out.println("<<Rect class>>");
Rect r = new Rect();
r.disp();
}
}
|
cs |
<<Point class>>
default constructor
x = 5 y = 90
매개변수 1개 생성자 함수 constructor
x = 45 y = 99999
매개변수 2개 생성자함수 constructor
x = 1 y = 2
--------------------
<<Circle class>>
x = 20 y = 20 r = 5
--------------------
<<Rect class>>
x = 0 y = 0
x1 = 0 y1 = 0
▶생성한 객체인 pt, c, r 이 다른 클래스로 들어갈 수 있는 열쇠(key)이다.
'JAVA > 11_oop-class' 카테고리의 다른 글
class, 멤버변수, 멤버함수 그림 설명 (0) | 2021.01.22 |
---|---|
this와 this() (0) | 2021.01.19 |
클래스 안에서 다른 클래스 접근 방법 (참고) (0) | 2021.01.19 |
<<1.코딩레벨로 class만들기>>-하나의 클래스 안에 여러개의 클래스 (0) | 2021.01.19 |
객체생성시(new,참조변수) 메모리의 모습 - heap영역과 stack영역 (0) | 2021.01.18 |