| English | 의미 |
|---|---|
| default constructor/dɪˈfɒlt kənˈstrʌktə/ | 기본 constructor |
매개변수가 있는 constructor는無-arg constructor를 제공하지 않습니다
- Student는 오직 Student(String n)만을 선언합니다. new Student("Ada")는 작동하지만, new Student()에는 일치하는 constructor가 없습니다.
- Java는 declared되지 않은 경우에만 암시적 default constructor를 제공합니다. 두 호출을 허용하려면, 명시적으로 Student()와 Student(String n)을 작성하십시오.
새로운 객체를 설정하는 방법
- A constructor는 object가 생성될 때
new로 실행되어 초기 상태를 설정합니다. class와 동일한 이름을 가지며 반환 유형이 없습니다(void도 포함하여). public Student(String n) { name = n; score = 0; }그 역할: 새 객체에 합리적인 시작 값을 부여하는 것입니다.
Constructor parameters
- 파라미터는 각 새 객체가 자체 값으로 시작하게 합니다.
new Student("Ada")은"Ada"을 생성자의n파라미터에 전달합니다. - 내부에서,
name = n;는 인수를 instance variable에 복사합니다. 각 성공적인new표현식은 동일한 인수를 가져오더라도 별개의 객체를 생성합니다. 다른 인수들이 constructor에 의해 무시되거나 정렬될 경우, 반드시 다른 필드 값을 생성하지는 않을 수 있습니다.
Two executions of new Student("Ada") refer to the same object because the arguments are equal.
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
Each successful new expression constructs a distinct object; equal arguments do not make the references equal.
Overloading constructors
- A class는 서로 다른 parameter lists를 가진 여러 constructors를 가질 수 있습니다 — overloading.
Student(String n)와Student(String n, int s)는 모두 유효합니다. - Java는 your
newarguments와 일치하는 one을 선택합니다. 이는 caller에게 객체를 구축하는 유연한 방법을 제공합니다.
Constructors set the starting state
Each new object is configured by the constructor's arguments.
A constructor has...
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
Same name as the class, no return type — not even void.
When does a constructor run?
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
new invokes the constructor to build the object.
If you write no constructor, Java provides a default no-argument constructor.
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
But writing any constructor removes that free default.
Once you define a constructor with parameters, new Student() still works automatically.
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
The free default is gone; write a no-arg constructor if you need one.
Writing two constructors with different parameter lists is an example of...
난이도 1/5 · 100 코인 + 오늘 첫 완료 시 소액 보너스
Same name, different parameters = overloading.
The default constructor
- A constructor을 아무것도 쓰지 않으면, Java는 parameters가 없는 숨겨진 default constructor를 제공합니다. fields는 먼저 default values(
0,false,null)를 가지며, 이후 명시적 field initializers가 다른 초기 값을 설정할 수 있습니다. - 그러나 어떤 constructor를 작성하면, 그 무료 default는 사라집니다. 따라서 무-arg constructor 및 parameterized one이 필요하면, 두 개를 모두 작성하십시오.
A constructor는 class의 이름을 가지고 반환 유형이 없습니다 — void를 쓰면寻常 method가 되어 constructor가 아닙니다. 그리고 어떤 constructor를 정의하면, Java는 무료 무-argument default를 더 이상 제공하지 않습니다; 여전히 new Student()이 필요하다면, 그 constructor를 직접 작성해야 합니다.
Order the steps used to evaluate new Student("Ada").
난이도 3/5 · 200 코인 + 오늘 첫 완료 시 소액 보너스
The supplied argument selects a matching constructor; declaring it does not also create a no-argument constructor.
A Counter has private int value = 7; and an explicit constructor that then assigns value = 9;. What is value immediately after construction?
난이도 2/5 · 140 코인 + 오늘 첫 완료 시 소액 보너스
The field initializer assigns 7 before the constructor body assigns 9.
Two constructors for Student:
public Student(String n) { name = n; score = 0; }public Student(String n, int s) { name = n; score = s; }new Student("Ada")→ score0;new Student("Bo", 75)→ score75.
추론을 새로운 사례로 확장하기
class Counter { private int value = 7; }과 명시적 생성자가value = 9;을 할당하는 다른 동일한 클래스를 비교하십시오. 첫 번째는 필드 초기화기 후 7에서 시작하고, 두 번째는 9에서 konstruion을 끝냅니다.- fields는 field initializers와 constructor body가 실행되기 전에 default 값을 받습니다. programmer가 쓴 무-argument constructor는 compiler가 제공하는 default constructor와 다르지만, 둘 다 arguments를 취하지는 않습니다.
생성자는 클래스와 동일한 이름을 가지며, 반환 유형이 없으며, new에서 실행되어 객체의 초기 상태를 설정합니다. 파라미터를 사용하여 각 객체에 대한 값을 지정합니다. 하나의 클래스에는 여러 생성자를 오버로드할 수 있습니다. 생성자를 명시적으로 정의하지 않으면 Java가 기본 무인 생성자를 자동으로 제공합니다. 그러나 임의의 생성자를 정의하면 이 무료 기본 생성자는 사라집니다.