<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)이다.

 

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
package ex01.oop;
//=============클래스 안에서 다른 클래스 접근 방법->참고만===============
class Circle {
    private int x, y, r;
    private Rect re; //선언 -> 메모리에 할당이 필요!
 
    public void display() {
        System.out.println("Circle disp: " + x + " , " + y + " , " + r);
        
        //메로리할당, 객체 생성
        re = new Rect();
        re.display();
    }
 
}
 
class Rect {
    private int x, y, x2, y2;
 
    public void display() {
        System.out.println("Rect disp: " + " , " + y + " , " + x2 + " , " + y2);
    }
}
 
public class Ex02_OOP2 {
    public static void main(String[] args) {
 
        Circle c = new Circle();
        c.display();
        
        System.out.println("--------------------------");
        
        Rect r = new Rect();
        r.display();
    }
}
 
cs

Circle disp: 0 , 0 , 0
Rect disp:  , 0 , 0 , 0
--------------------------
Rect disp:  , 0 , 0 , 0


Circle 클래스안에서 Rect 클래스에 접근하기 

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package ex01.oop;
//<<<<<<<<<<<<<<1.코딩레벨로 class만들기>>>>>>>>>>>
class Point {
    private int x, y; // 멤버변수(클래스변수) - int: 0, String: null, double/float: 0.0
    private char ch;
    String name;
    double height;
    
    public void setX(int xx) {
        x=xx;
    }
    
    public int getX() {
        return x;
    }
    
    public void setY(int yy) {
        y=yy;
    }
    
    public int getY() {
        return y;
    }
    
    public void setCh(char chch) {
        ch=chch;
    }
    
    public char getCh() {
        return ch;
    }
    
    public void setName(String name22) {
        name = name22;
    }
    
    public String getName() {
        return name;
    }
    
    public void setHeight(double height22) {
        height = height22;
    }
    
    public double getHeight() {
        return height;
    }
    
    
    
    // 멤버함수
    public void display() {
        System.out.println(x + " , " + y);
        
        //멤버변수의 default 값을 알수있다
        System.out.println("ch = " + ch);  
        System.out.println("name = " + name);
        System.out.println("height = " + height);
    }
}
 
 
public class Ex01_MainEntry {
    public static void main(String[] args) {
        Point pt = new Point(); //객체생성, 메모리에할당, 생성자함수 자동호출
        pt.display(); //멤버변수의 초기값 출력
        
        System.out.println("-----------------------");
        
        //cf)지역변수는 초기화 필수 
        int x=123
        System.out.println(x);
        
        System.out.println("-----------------------");
        
        //멤버변수 셋팅(setXXX사용)
        pt.setHeight(173.5);
        pt.setName("rara");
        pt.setX(80);
//        pt.display(); //멤버변수의 셋팅된 값 출력
        
        //getXXX사용해서 값 가져오기 
        System.out.println("name = " + pt.getName());
        System.out.println("x = " + pt.getX());
    }
// class MainEntry end
 
cs

0 , 0
ch = 

name = null
height = 0.0
-----------------------
123
-----------------------
name = rara
x = 80


멤버변수(클래스변수) - 초기화하지 않을 경우, 자동으로 default값이 들어감

                                                         (int: 0, String: null, double/float: 0.0)

지역변수 - 초기화가 필수

 

+ Recent posts