본문 바로가기
Java 배우기

Providing Constructors for Your Classes 클래스에 생성자 제공하기

by 노화방지 Anti-aging Hairstyle 2016. 1. 5.
반응형

클래스에 생성자 제공하기 Providing Constructors for Your Classes


클래스에는, 객체를 생성하기 위하여 클래스 설계도로부터 invoke되는, 생성자(constructors)가 포함되어 있습니다.
생성자 선언은, 클래스 이름을 사용하고 리턴 타입이 없다는 점을 제외하고는 메소드 선언과 같습니다.
예를 들어, Bicycle은 1개의 생성자를 갖고 있습니다:

A class contains constructors that are invoked to create objects from the class blueprint. Constructor declarations look like method declarations—except that they use the name of the class and have no return type. For example, Bicycle has one constructor:

public Bicycle(int startCadence, int startSpeed, int startGear) {
    gear = startGear;
    cadence = startCadence;
    speed = startSpeed;
}

myBike란 이름의 새 Bicycle객체를 생성하기 위하여, 운영자(operator) new가 생성자를 호출합니다:

To create a new Bicycle object called myBike, a constructor is called by the new operator:

Bicycle myBike = new Bicycle(30, 0, 8);

new Bicycle(30, 0, 8)는 메모리 안에 객체를 위한 공간을 생성하고 그 필드들을 초기화합니다.

Bicycle은 생성자를 1개만 갖고 있지만, 아규먼트가 없는 생성자 등 다른 생성자도 가질 수 있습니다:

new Bicycle(30, 0, 8) creates space in memory for the object and initializes its fields.

Although Bicycle only has one constructor, it could have others, including a no-argument constructor:

public Bicycle() {
    gear = 1;
    cadence = 10;
    speed = 0;
}


Bicycle yourBike = new Bicycle(); yourBike라는 new Bicycle 객체를 생성하기 위하여 아규먼트 없는 생성자를 invoke 합니다.

Bicycle yourBike = new Bicycle(); invokes the no-argument constructor to create a new Bicycle object called yourBike.


2개의 생성자가 Bicycle로 선언되었는데, 그 이유는 생성자들이 각자 다른 아규먼트 목록을 갖고 있기 때문입니다.
메소드를 구분하는 처럼, 자바 플랫폼은 목록에 있는 아규먼트의 갯수와 타입을 근거로 이들 생성자들을 구분합니다.
같은 클래스에서 동일한 갯수와 타입의 아규먼트를 가진 2개의 생성자를 작성할 수 없는데, 그 이유는 플랫폼이 그들을 구분할 수 없기 때문입니다.
그렇게 하면 컴파일 시점에 오류가 발생됩니다.

Both constructors could have been declared in Bicycle because they have different argument lists. As with methods, the Java platform differentiates constructors on the basis of the number of arguments in the list and their types. You cannot write two constructors that have the same number and type of arguments for the same class, because the platform would not be able to tell them apart. Doing so causes a compile-time error.


클래스에 어떤 생성자들을 제공할 필요는 없지만, 제공하지 않을 때는 신중해야 합니다.
컴파일러는 생성자가 없는 모든 클래스에게 아규먼트가 없는, 기본 생성자를 자동으로 제공합니다.
이 기본 생성자는 수퍼클래스의 아규먼트 없는 생성자를 호출(call) 할 것입니다.
이 상황에서, superclass가 아규먼트 없는 생성자를 갖고 있지 않다면, 그래서 수퍼클래스가 생성자를 갖고 있는지 여부를 검증해야 한다면 컴파일러는 불평할 것입니다.
클래스가 명시적 수퍼클래스를 갖고 있지 않다면,
묵시적 수퍼클래스인 Object를 갖고 있는데, Object는 아규먼트 없는 생성자를 갖고 있습니다.

You don't have to provide any constructors for your class, but you must be careful when doing this. The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does. If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor.


수퍼클래스 생성자를 스스로 사용할 수 있습니다.
이 단원이 시작될 때의 클래스 MountainBike가 그렇게 했습니다.
이것은 나중에 인터페이스와 상속 단원에서 배울 것입니다.
어떤 다른 클래스들이 생성자를 호출할 수 있는지를 제어하기 위하여 생성자 선언에 있는 접근 제한자를 사용할 수 있습니다.
You can use a superclass constructor yourself. The MountainBike class at the beginning of this lesson did just that. This will be discussed later, in the lesson on interfaces and inheritance.

You can use access modifiers in a constructor's declaration to control which other classes can call the constructor.


참고: 다른 클래스가 MyClass 생성자를 호출할 수 없다면 객체 MyClass를 만들 수 없습니다. 
If another class cannot call a MyClass constructor, it cannot directly create MyClass objects.




반응형

댓글