chapter 06. 클래스.


6.5 클래스의 구성 멤버

6.5.1 필드

6.5.2 생성자

6.5.3 메소드



6.6 필드 (클래스의 멤버 변수)

// 필드는 객체의 고유 데이터, 부품 객체, 상태 정보를 저장하는 곳..

// 선언 형태는 변수와 비슷하지만, 필드는 변수가 아니다.

// 변수는 생성자와 메소드 내에서만 사용되고, 생성자와 메소드가 실행 종료되면 자동 소멸된다.

// 하지만 필드는 생성자와 메소드 전체에서 사용되며 객체가 소멸되지 않는 한 객체와 함께 존재한다.


6.6.1 필드 선언



6.6.2 필드 사용


// 클래스 내부의 생성자나 메소드에서 사용할 경우 단순히 필드 이름으로 읽고 변경하면 되지만,

// 클래스 외부에서 사용할 경우 우선적으로 클래스로부터 객체를 생성한 뒤 사용해야 한다.



 

1
2
3
4
5
6
7
8
9
10
package sec06.exam01_field_declaration;
 
public class Car {
    //필드
    String company = "현대자동차";
    String model = "그랜저";
    String color = "검정";
    int maxSpeed = 350;
    int speed;
}
cs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package sec06.exam01_field_declaration;
 
public class CarExample {
    public static void main(String[] args) {
        //객체 생성
        Car myCar = new Car();
        
        //필드 값 읽기
        System.out.println("제작회사: " + myCar.company);
        System.out.println("모델명: " + myCar.model);
        System.out.println("색깔: " + myCar.color);
        System.out.println("최고속도: " + myCar.maxSpeed);
        System.out.println("현재속도: " + myCar.speed);
        
        //필드 값 변경
        myCar.speed = 60;
        System.out.println("수정된 속도: " + myCar.speed);
    }
}
cs




6.7 생성자

6.7.1 기본 생성자

// 생성자를 실행시키지 않고는 클래스로부터 객체를 만들 수 없다. (생성자(O) -> 객체생성(O))


6.7.2 생성자 선언


Car.java // 생성자 선언

1
2
3
4
5
6
public class Car 
{
    //생성자
    Car(String color, int cc) // 검정, 3000
    {}
}
cs

CarExample.java // 생성자를 호출해서 객체 생성
1
2
3
4
5
6
7
8
9
10
package sec07.exam01_constructor_declaration;
 
public class CarExample {
    public static void main(String[] args) 
    {
        Car myCar = new Car("검정"3000);
        //Car myCar = new Car();  (X) // 기본 생성자를 호출할 수 없다.
    }
}
 
cs



6.7.3 필드 초기화


Korean.java // 생성자에서 필드 초기화

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Korean 
{
    //필드
    String nation = "대한민국"// 1. 필드를 선언할때 초기값을 설정
    String name;
    String ssn;
 
    //생성자
    public Korean(String n, String s) 
    {
      name = n;
      ssn = s;
    }
    
    //생성자 (this로 바꿔쓰기)
    public Korean(String name, String ssn)// 매개변수 // 2. 생성자에서 매개값으로 초기화를 설정 
    {
      this.name(필드) = name(매개변수);
      this.ssn(필드) = ssn(매개변수);
    }
}
cs


1. 필드를 선언할 때 초기값을 설정 // 동일한 클래스로부터 생성되는 객체들은 모두 같은 데이터를 갖게 된다.

1
2
Korean k1 = new Korean(); // 대한민국
Korean k2 = new Korean(); // 대한민국
cs

KoreanExapmle.java // 객체 생성 후 필드값 출력
1
2
3
4
5
6
7
8
9
10
11
public class KoreanExample {
    public static void main(String[] args) {
        Korean k1 = new Korean("박자바""011225-1234567");
        System.out.println("k1.name : " + k1.name);
        System.out.println("k1.ssn : " + k1.ssn);
        
        Korean k2 = new Korean("김자바""930525-0654321");
        System.out.println("k2.name : " + k2.name);
        System.out.println("k2.ssn : " + k2.ssn);
    }
}
cs



6.7.4 생성자 오버로딩(Overloading)


Car.java // 생성자의 오버로딩

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
public class Car {
    //필드
    String company = "현대자동차";
    String model;
    String color;
    int maxSpeed;
    
    //생성자
    Car() { // 기본생성자이지만, 다른 생성자들도 만들었으므로, 자동으로 생성X, 그래서 이 예제에서 명시적으로 만듬
    }
    
    Car(String model) {
        this.model = model;
    }
    
    Car(String model, String color) {
        this.model = model;
        this.color = color;
    }
    
    Car(String model, String color, int maxSpeed) {
        this.model = model;
        this.color = color;
        this.maxSpeed = maxSpeed;
    }
}
 
cs


CarExample.java // 객체 생성 시 생성자 선택
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
public class CarExample {
    public static void main(String[] args) {
        Car car1 = new Car();
        System.out.println("car1.company : " + car1.company);
        System.out.println();
        
        Car car2 = new Car("자가용"); // String model
        System.out.println("car2.company : " + car2.company);
        System.out.println("car2.model : " + car2.model);
        System.out.println();
        
        Car car3 = new Car("자가용""빨강"); // String model, String color 
        System.out.println("car3.company : " + car3.company);
        System.out.println("car3.model : " + car3.model);
        System.out.println("car3.color : " + car3.color);
        System.out.println();
        
        Car car4 = new Car("택시""검정"200); // String model, String color, int maxSpeed
        System.out.println("car4.company : " + car4.company);
        System.out.println("car4.model : " + car4.model);
        System.out.println("car4.color : " + car4.color);
        System.out.println("car4.maxSpeed : " + car4.maxSpeed);
    }
}
 
 
cs



6.7.5 다른 생성자 호출(this())


Car.java // 다른 생성자를 호출해서 중복 코드 줄이기

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
package sec07.exam04_other_constructor_call;
 
public class Car {
    //필드
    String company = "현대자동차";
    String model;
    String color;
    int maxSpeed;
    
    //생성자
    Car() 
    {
        this("자가용""은색"250); // this는 생성자의 첫줄에만 있어야한다 아니면 오류가 난다.
    }
    
    Car(String model) 
    {
        this(model, "은색"250); 
    }
    
    Car(String model, String color) 
    {
        this(model, color, 250);
    }
    
    Car(String model, String color, int maxSpeed) // 공통 실행 코드, 초기화 내용을 한 생성자에 몰아서 작성한다
    {
        this.model = model;
        this.color = color;
        this.maxSpeed = maxSpeed;
    }
}
cs


CarExample.java // 객체 생성 시 생성자 선택
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
package sec07.exam04_other_constructor_call;
 
public class CarExample {
    public static void main(String[] args) {
        Car car1 = new Car();
        System.out.println("car1.company : " + car1.company);
        System.out.println("car1.model : " + car1.model);
        System.out.println("car1.color : " + car1.color);
        System.out.println("car1.maxSpeed : " + car1.maxSpeed);
        System.out.println();
        
        Car car2 = new Car("자가용");
        System.out.println("car2.company : " + car2.company);
        System.out.println("car2.model : " + car2.model);
        System.out.println("car2.color : " + car2.color);
        System.out.println("car2.maxSpeed : " + car2.maxSpeed);
        System.out.println();
        
        Car car3 = new Car("자가용""빨강");
        System.out.println("car3.company : " + car3.company);
        System.out.println("car3.model : " + car3.model);
        System.out.println("car3.color : " + car3.color);
        System.out.println("car3.maxSpeed : " + car3.maxSpeed);
        
        System.out.println();
        
        Car car4 = new Car("택시""검정"200);
        System.out.println("car4.company : " + car4.company);
        System.out.println("car4.model : " + car4.model);
        System.out.println("car4.color : " + car4.color);
        System.out.println("car4.maxSpeed : " + car4.maxSpeed);
    }
}
cs


Posted by 너래쟁이
: