์๋
! ์ค๋ ์ฐ๋ฆฌ๋ ๊ฐ์ฒด์ ๊ด๋ จ๋ ๋งค์ฐ ์ค์ํ ์ฃผ์ ๋ฅผ ๊ณ ๋ คํ ๊ฒ์
๋๋ค. ๊ณผ์ฅ ์์ด ์ค์ํ์์ ์ด ์ฃผ์ ๋ฅผ ๋งค์ผ ์ฌ์ฉํ๊ฒ ๋ ๊ฒ์ด๋ผ๊ณ ๋งํ ์ ์์ต๋๋ค! ์ฐ๋ฆฌ๋ Java ์์ฑ์์ ๋ํด ์ด์ผ๊ธฐํ๊ณ ์์ต๋๋ค. ์ด ์ฉ์ด๋ฅผ ์ฒ์ ๋ฃ๋ ๊ฒ์ผ ์ ์์ง๋ง ์ค์ ๋ก๋ ์ด๋ฏธ ์์ฑ์๋ฅผ ์ฌ์ฉํ์ต๋๋ค. ๋น์ ์ ๊ทธ๊ฒ์ ๊นจ๋ซ์ง ๋ชปํ์ต๋๋ค :) ์ฐ๋ฆฌ๋ ๋์ค์ ์ด๊ฒ์ ํ์ ํฉ๋๋ค.
์์
์์ ๋ถ๋ถ์์ ๋น์ ์ด ๊นจ๋ซ์ง ๋ชปํ ์ฑ ์ด๋ฏธ ์์ฑ์๋ฅผ ์ฌ์ฉํ๋ค๊ณ ๋งํ๋ ๊ฒ์ ๊ธฐ์ตํ์ญ๋๊น? ์ฐ๋ฆฌ๋ ์ฐ๋ฆฌ๊ฐ ๋งํ ๊ฒ์ ์๋ฏธํ์ต๋๋ค. ์ฌ์ค Java์ ๋ชจ๋ ํด๋์ค์๋ ๊ธฐ๋ณธ ์์ฑ์๋ผ๋ ๊ฒ์ด ์์ต๋๋ค. ์ธ์๋ฅผ ์ฌ์ฉํ์ง ์์ง๋ง ๋ชจ๋ ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ๋ง๋ค ๋๋ง๋ค ํธ์ถ๋ฉ๋๋ค.
(CarFactory.java:15) at CarFactory.main(CarFactory.java:23) ํ๋ก์ธ์ค๊ฐ ์ข
๋ฃ ์ฝ๋ 1๋ก ์ข
๋ฃ๋จ
ํ! ํ๋ก๊ทธ๋จ์ ์ด๋ค ์ข
๋ฅ์ ์ดํดํ ์ ์๋ ์ค๋ฅ์ ํจ๊ป ๋๋ฉ๋๋ค. ์์ธ์ ์ถ์ธกํ ์ ์์ต๋๊น? ๋ฌธ์ ๋ ์ฐ๋ฆฌ๊ฐ ์์ฑ์์ ๋ฃ์ ๋
ผ๋ฆฌ์ ์์ต๋๋ค. ๋ณด๋ค ๊ตฌ์ฒด์ ์ผ๋ก ๋ค์ ์ค์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
์ธ๊ณ์์ ์์ฑ์๋ ๋ฌด์์ด๋ฉฐ ์ ํ์ํ๊ฐ์?
๋ ๊ฐ์ง ์๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
public class Car {
String model;
int maxSpeed;
public static void main(String[] args) {
Car bugatti = new Car();
bugatti.model = "Bugatti Veyron";
bugatti.maxSpeed = 378;
}
}
์ฐ๋ฆฌ๋ ์๋์ฐจ๋ฅผ ๋ง๋ค๊ณ ๋ชจ๋ธ๊ณผ ์ต๋ ์๋๋ฅผ ์ค์ ํ์ต๋๋ค. ๊ทธ๋ฌ๋ Car ๊ฐ์ฒด๋ ๋ถ๋ช
ํ ์ค์ ํ๋ก์ ํธ์์ 2๊ฐ์ ํ๋๋ฅผ ๊ฐ์ง ์์ต๋๋ค. ์๋ฅผ ๋ค์ด 16๊ฐ์ ํ๋๊ฐ ์์ ์ ์์ต๋๋ค!
public class Car {
String model;// model
int maxSpeed;// maximum speed
int wheels;// wheel width
double engineVolume;// engine volume
String color;// color
int productionYear;// production year
String ownerFirstName;// first name of owner
String ownerLastName;// last name of owner
long price;// price
boolean isNew;// flag indicating whether car is new
int seatsInTheCar;// number of seats in the car
String cabinMaterial;// interior material
boolean insurance;// flag indicating whether car is insured
String manufacturerCountry;// manufacturer country
int trunkVolume;// size of the trunk
int accelerationTo100km;// how long it takes to accelerate to 100 km/h (in seconds)
public static void main(String[] args) {
Car bugatti = new Car();
bugatti.color = "blue";
bugatti.accelerationTo100km = 3;
bugatti.engineVolume = 6.3;
bugatti.manufacturerCountry = "Italy";
bugatti.ownerFirstName = "Amigo";
bugatti.productionYear = 2016;
bugatti.insurance = true;
bugatti.price = 2000000;
bugatti.isNew = false;
bugatti.seatsInTheCar = 2;
bugatti.maxSpeed = 378;
bugatti.model = "Bugatti Veyron";
}
}
์๋ก์ด Car ๊ฐ์ฒด๋ฅผ ๋ง๋ค์์ต๋๋ค. ํ ๊ฐ์ง ๋ฌธ์ ๊ฐ ์์ต๋๋ค. 16๊ฐ์ ํ๋๊ฐ ์์ง๋ง 12๊ฐ๋ง ์ด๊ธฐํํ์ต๋๋ค ! ์ง๊ธ ์ฝ๋๋ฅผ ๋ณด๊ณ ์์ด๋ฒ๋ฆฐ ํ๋๋ฅผ ์ฐพ์ผ์ญ์์ค! ์ฝ์ง ์์ฃ ? ์ด ์ํฉ์์ ํ๋ก๊ทธ๋๋จธ๋ ์ฝ๊ฒ ์ค์ํ ์ ์๊ณ ์ผ๋ถ ํ๋๋ฅผ ์ด๊ธฐํํ์ง ๋ชปํ ์ ์์ต๋๋ค. ๊ฒฐ๊ณผ์ ์ผ๋ก ํ๋ก๊ทธ๋จ์ด ์๋ชป ์๋ํฉ๋๋ค.
public class Car {
String model;// model
int maxSpeed;// maximum speed
int wheels;// wheel width
double engineVolume;// engine volume
String color;// color
int productionYear;// production year
String ownerFirstName;// first name of owner
String ownerLastName;// last name of owner
long price;// price
boolean isNew;// flag indicating whether car is new
int seatsInTheCar;// number of seats in the car
String cabinMaterial;// interior material
boolean insurance;// flag indicating whether car is insured
String manufacturerCountry;// manufacturer country
int trunkVolume;// size of the trunk
int accelerationTo100km;// how long it takes to accelerate to 100 km/h (in seconds)
public static void main(String[] args) {
Car bugatti = new Car();
bugatti.color = "blue";
bugatti.accelerationTo100km = 3;
bugatti.engineVolume = 6.3;
bugatti.manufacturerCountry = "Italy";
bugatti.ownerFirstName = "Amigo";
bugatti.productionYear = 2016;
bugatti.insurance = true;
bugatti.price = 2000000;
bugatti.isNew = false;
bugatti.seatsInTheCar = 2;
bugatti.maxSpeed = 378;
bugatti.model = "Bugatti Veyron";
System.out.println("Model: Bugatti Veyron. Engine volume: " + bugatti.engineVolume + ". Trunk volume: " + bugatti.trunkVolume + ". Cabin material: " + bugatti.cabinMaterial +
". Wheel width: " + bugatti.wheels + ". Purchased in 2018 by Mr. " + bugatti.ownerLastName);
}
}
์ฝ์ ์ถ๋ ฅ: ๋ชจ๋ธ: Bugatti Veyron. ์์ง ๋ณผ๋ฅจ: 6.3. ํธ๋ ํฌ ๋ณผ๋ฅจ: 0. ์บ๋น ์ฌ์ง: null. ๋ฐํด ๋๋น: 0. 2018๋
์ Mr. null์ด ๊ตฌ์
ํ์ต๋๋ค . ์ฐจ๋ฅผ ์ฌ๊ธฐ ์ํด 200๋ง ๋ฌ๋ฌ๋ฅผ ํฌ๊ธฐํ ๊ทํ์ ๊ตฌ๋งค์๋ ๋ถ๋ช
ํ " Mr. null "์ด๋ผ๊ณ ๋ถ๋ฆฌ๋ ๊ฒ์ ์ข์ํ์ง ์์ ๊ฒ์
๋๋ค! ๊ทธ๋ฌ๋ ์ฌ๊ฐํ๊ฒ ํต์ฌ์ ์ฐ๋ฆฌ ํ๋ก๊ทธ๋จ์ด ๊ฐ์ฒด๋ฅผ ์๋ชป ์์ฑํ๋ค๋ ๊ฒ์
๋๋ค. ํ ๋๋น๊ฐ 0์ธ ์๋์ฐจ(์ฆ, ๋ฐํด๊ฐ ์ ํ ์์), ํธ๋ ํฌ๊ฐ ๋๋ฝ๋จ, ์ ์ ์๋ ์ฌ๋ฃ๋ก ๋ง๋ค์ด์ง ์บ๋น, ๊ทธ๋ฆฌ๊ณ ๋ฌด์๋ณด๋ค๋ ์ ์๋์ง ์์ ์์ ์ . ํ๋ก๊ทธ๋จ์ด ์คํ ์ค์ผ ๋ ๊ทธ๋ฌํ ์ค์๊ฐ ์ด๋ป๊ฒ "์ฌ๋ผ์ง" ์ ์๋์ง ์์ํ ์ ์์ ๋ฟ์
๋๋ค! ์ฐ๋ฆฌ๋ ์ด๋ป๊ฒ๋ ๊ทธ๋ฐ ์ํฉ์ ํผํด์ผ ํฉ๋๋ค. ํ๋ก๊ทธ๋จ์ ์ ํํด์ผ ํฉ๋๋ค: ์ ์๋์ฐจ๋ฅผ ๋ง๋ค ๋๊ฐ์ฒด, ๋ชจ๋ธ ๋ฐ ์ต๋ ์๋์ ๊ฐ์ ํ๋๊ฐ ํญ์ ์ง์ ๋๊ธฐ๋ฅผ ์ํฉ๋๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด ๊ฐ์ฒด ์์ฑ์ ๋ฐฉ์งํ๋ ค๊ณ ํฉ๋๋ค. ์์ฑ์๋ ์ด ์์
์ ์ฝ๊ฒ ์ฒ๋ฆฌํฉ๋๋ค. ๊ทธ๋ค์ ์ด์ ๋๋ฌธ์ ์ด๋ฆ์ ์ป์์ต๋๋ค. ์์ฑ์๋ ๊ฐ๊ฐ์ ์ ๊ฐ์ฒด๊ฐ ์ผ์นํด์ผ ํ๋ ์ผ์ข
์ "๊ณจ๊ฒฉ" ํด๋์ค๋ฅผ ๋ง๋ญ๋๋ค. ํธ์๋ฅผ ์ํด ๋ ๊ฐ์ ํ๋๊ฐ ์๋ ๊ฐ๋จํ ๋ฒ์ ์ Car ํด๋์ค ๋ก ๋์๊ฐ ๋ณด๊ฒ ์ต๋๋ค . ์๊ตฌ ์ฌํญ์ ๊ณ ๋ คํ๋ฉด Car ํด๋์ค์ ์์ฑ์๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
public Car(String model, int maxSpeed) {
this.model = model;
this.maxSpeed = maxSpeed;
}
// And creating an object now looks like this:
public static void main(String[] args) {
Car bugatti = new Car("Bugatti Veyron", 378);
}
์์ฑ์๊ฐ ์ ์ธ๋๋ ๋ฐฉ์์ ์ ์ํ์ญ์์ค. ์ผ๋ฐ ๋ฉ์๋์ ๋น์ทํ์ง๋ง ๋ฐํ ์ ํ์ด ์์ต๋๋ค. ๋ํ ์์ฑ์๋ ๋๋ฌธ์๋ก ์์ํ๋ ํด๋์ค ์ด๋ฆ( Car )์ ์ง์ ํฉ๋๋ค. ๋ํ ์์ฑ์๋ ์๋ก์ด ํค์๋์ธ this ์ ํจ๊ป ์ฌ์ฉ๋ฉ๋๋ค . ํค์๋ this๋ ํน์ ๊ฐ์ฒด๋ฅผ ๋ํ๋ด๊ธฐ ์ํ ๊ฒ์
๋๋ค. ์์ฑ์์ ์ฝ๋
public Car(String model, int maxSpeed) {
this.model = model;
this.maxSpeed = maxSpeed;
}
" ์ด ์๋์ฐจ์ ๋ชจ๋ธ ( ์ง๊ธ ๋ง๋ค๊ณ ์๋ ๊ฒ) ์ ์์ฑ์์ ์ ๋ฌ๋ ๋ชจ๋ธ ์ธ์์
๋๋ค. ์ด ์๋์ฐจ(์ฐ๋ฆฌ๊ฐ ๋ง๋ค๊ณ ์๋ ๊ฒ)์ maxSpeed ๋ ๊ฑด์ค์." ๊ทธ๋ฆฌ๊ณ ๊ทธ๊ฒ์ด ๋ฐ๋ก ์ผ์ด๋๋ ์ผ์
๋๋ค.
public class Car {
String model;
int maxSpeed;
public Car(String model, int maxSpeed) {
this.model = model;
this.maxSpeed = maxSpeed;
}
public static void main(String[] args) {
Car bugatti = new Car("Bugatti Veyron", 378);
System.out.println(bugatti.model);
System.out.println(bugatti.maxSpeed);
}
}
์ฝ์ ์ถ๋ ฅ: Bugatti Veyron 378 ์์ฑ์๊ฐ ํ์ํ ๊ฐ์ ์ฌ๋ฐ๋ฅด๊ฒ ํ ๋นํ์ต๋๋ค. ์์ฑ์๊ฐ ์ผ๋ฐ ๋ฉ์๋์ ๋งค์ฐ ์ ์ฌํ๋ค๋ ๊ฒ์ ๋์น์ฑ์
จ์ ๊ฒ์
๋๋ค! ๊ทธ๋ ์ต๋๋ค. ์์ฑ์๋ ์ค์ ๋ก ๋ฉ์๋์ด์ง๋ง ํน์ ๊ธฐ๋ฅ์ด ์์ต๋๋ค :) ๋ฉ์๋์ ๋ง์ฐฌ๊ฐ์ง๋ก ์์ฑ์์ ์ธ์๋ฅผ ์ ๋ฌํ์ต๋๋ค. ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ๊ฒ๊ณผ ๋ง์ฐฌ๊ฐ์ง๋ก ์์ฑ์๋ฅผ ํธ์ถํ๋ฉด ์ง์ ํ์ง ์์ผ๋ฉด ์๋ํ์ง ์์ต๋๋ค.
public class Car {
String model;
int maxSpeed;
public Car(String model, int maxSpeed) {
this.model = model;
this.maxSpeed = maxSpeed;
}
public static void main(String[] args) {
Car bugatti = new Car(); // Error!
}
}
์์ฑ์๊ฐ ์ฐ๋ฆฌ๊ฐ ๋ฌ์ฑํ๋ ค๊ณ ํ๋ ๊ฒ์ ๋ฌ์ฑํ๋ ๊ฒ์ ๋ณผ ์ ์์ต๋๋ค. ์ด์ ์๋๋ ๋ชจ๋ธ ์์ด๋ ์๋์ฐจ๋ฅผ ๋ง๋ค ์ ์์ต๋๋ค! ์์ฑ์์ ๋ฉ์๋์ ์ ์ฌ์ฑ์ ์ฌ๊ธฐ์ ๋๋์ง ์์ต๋๋ค. ๋ฉ์๋์ ๋ง์ฐฌ๊ฐ์ง๋ก ์์ฑ์๋ ์ค๋ฒ๋ก๋๋ ์ ์์ต๋๋ค. ์ง์ ๊ณ ์์ด ๋ ๋ง๋ฆฌ๊ฐ ์๋ค๊ณ ์์ํด ๋ณด์ธ์. ๋น์ ์ ๊ทธ๋ค ์ค ํ๋๋ฅผ ์๋ผ ๊ณ ์์ด๋ก ์ป์์ต๋๋ค. ํ์ง๋ง ๋ ๋ฒ์งธ๋ ์ด๋ฏธ ๋ค ์๋ ์ํ์์ ๊ธธ๊ฑฐ๋ฆฌ์์ ๊ฐ์ ธ์จ ๊ฒ์ธ๋ฐ ์ ํํ ๋ช ์ด์ธ์ง ์ ์ ์์ต๋๋ค. ์ด ๊ฒฝ์ฐ ์ฐ๋ฆฌ๋ ํ๋ก๊ทธ๋จ์ด ์ด๋ฆ๊ณผ ๋์ด๊ฐ ์๋ ๊ณ ์์ด(์ฒซ ๋ฒ์งธ ๊ณ ์์ด)์ ์ด๋ฆ๋ง ์๋ ๊ณ ์์ด(๋ ๋ฒ์งธ ๊ณ ์์ด)์ ๋ ์ข
๋ฅ์ ๊ณ ์์ด๋ฅผ ๋ง๋ค ์ ์๊ธฐ๋ฅผ ์ํฉ๋๋ค. ์ด๋ฅผ ์ํด ์์ฑ์๋ฅผ ์ค๋ฒ๋ก๋ํฉ๋๋ค.
public class Cat {
String name;
int age;
// For the first cat
public Cat(String name, int age) {
this.name = name;
this.age = age;
}
// For the second cat
public Cat(String name) {
this.name = name;
}
public static void main(String[] args) {
Cat smudge = new Cat("Smudge", 5);
Cat streetCatNamedBob = new Cat("Bob");
}
}
"name" ๋ฐ "age" ๋งค๊ฐ๋ณ์๊ฐ ์๋ ์๋ ์์ฑ์ ์ธ์ ์ด๋ฆ ๋งค๊ฐ๋ณ์๋ง ์๋ ์์ฑ์๋ฅผ ํ๋ ๋ ์ถ๊ฐํ์ต๋๋ค. ์ด์ ์์
์์ ๋ฉ์๋๋ฅผ ์ค๋ฒ๋ก๋ํ ๊ฒ๊ณผ ์ ํํ ๊ฐ์ ๋ฐฉ์์
๋๋ค. ์ด์ ๋ ์ข
๋ฅ์ ๊ณ ์์ด๋ฅผ ๋ชจ๋ ๋ง๋ค ์ ์์ต๋๋ค :)

public class Cat {
public static void main(String[] args) {
Cat smudge = new Cat(); // The default constructor is invoked here
}
}
์ธ๋ป ๋ณด๋ฉด ๋ณด์ด์ง ์์ต๋๋ค. ์ฐ๋ฆฌ๋ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์์ต๋๋ค. ๊ทธ๋์ ๋ฌด์์
๋๊น? ์ฌ๊ธฐ์์ ์์ฑ์๋ ์ด๋์์ ๋ฌด์์ ํ๊ณ ์์ต๋๊น? ์ด๋ฅผ ํ์ธํ๊ธฐ ์ํด Cat ํด๋์ค ์ ๋ํ ๋น ์์ฑ์๋ฅผ ๋ช
์์ ์ผ๋ก ์์ฑํด ๋ณด๊ฒ ์ต๋๋ค . ๊ทธ ์์ ๋ช ๊ฐ์ง ๋ฌธ๊ตฌ๋ฅผ ํ์ํฉ๋๋ค. ๊ตฌ๊ฐ ํ์๋๋ฉด ์์ฑ์๊ฐ ํธ์ถ๋ ๊ฒ์
๋๋ค.
public class Cat {
public Cat() {
System.out.println("A cat has been created!");
}
public static void main(String[] args) {
Cat smudge = new Cat(); // The default constructor is invoked here
}
}
์ฝ์ ์ถ๋ ฅ: ๊ณ ์์ด๊ฐ ์์ฑ๋์์ต๋๋ค! ํ์ธ์ด ์์ต๋๋ค! ๊ธฐ๋ณธ ์์ฑ์๋ ํญ์ ํด๋์ค์ ๋ณด์ด์ง ์๊ฒ ์กด์ฌํฉ๋๋ค. ๊ทธ๋ฌ๋ ๊ทธ๊ฒ์ ๋ํด ํ ๊ฐ์ง ๋ ์์์ผ ํฉ๋๋ค. ์ธ์๋ฅผ ์ฌ์ฉํ์ฌ ์์ฑ์๋ฅผ ์์ฑํ๋ฉด ๊ธฐ๋ณธ ์์ฑ์๊ฐ ํด๋์ค์์ ์ ๊ฑฐ๋ฉ๋๋ค. ์ฌ์ค, ์ฐ๋ฆฌ๋ ์์์ ์ด๊ฒ์ ๋ํ ์ฆ๊ฑฐ๋ฅผ ์ด๋ฏธ ๋ณด์์ต๋๋ค. ์ด ์ฝ๋์ ์์์ต๋๋ค.
public class Cat {
String name;
int age;
public Cat(String name, int age) {
this.name = name;
this.age = age;
}
public static void main(String[] args) {
Cat smudge = new Cat(); //Error!
}
}
๋ฌธ์์ด ๊ณผ int ๋งค๊ฐ๋ณ์๋ก Cat ์์ฑ์๋ฅผ ์ ์ธํ๊ธฐ ๋๋ฌธ์ ์ด๋ฆ๊ณผ ๋์ด ์์ด Cat์ ๋ง๋ค ์ ์์ต๋๋ค . ์ด๋ก ์ธํด ๊ธฐ๋ณธ ์์ฑ์๊ฐ ํด๋์ค์์ ์ฆ์ ์ฌ๋ผ์ก์ต๋๋ค. ๋ฐ๋ผ์ ์ธ์๊ฐ ์๋ ์์ฑ์๋ฅผ ํฌํจํ์ฌ ํด๋์ค์ ์ฌ๋ฌ ์์ฑ์๊ฐ ํ์ํ ๊ฒฝ์ฐ ๋ณ๋๋ก ์ ์ธํด์ผ ํฉ๋๋ค. ์๋ฅผ ๋ค์ด ๋๋ฌผ ๋ณ์์ ์ํ ํ๋ก๊ทธ๋จ์ ๋ง๋ ๋ค๊ณ ๊ฐ์ ํด ๋ณด๊ฒ ์ต๋๋ค. ์ ํฌ ๋ณ์์ ์ ํ์ ํ์ฌ ์ด๋ฆ๊ณผ ๋์ด๋ฅผ ์ ์ ์๋ ๋
ธ์์ ์๋ผ ๊ณ ์์ด๋ฅผ ๋๊ณ ์ ํฉ๋๋ค. ๊ทธ๋ฌ๋ฉด ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์์ผ ํฉ๋๋ค.
public class Cat {
String name;
int age;
// For cats with owners
public Cat(String name, int age) {
this.name = name;
this.age = age;
}
// For street cats
public Cat() {
}
public static void main(String[] args) {
Cat smudge = new Cat("Smudge", 5);
Cat streetCat = new Cat();
}
}
์ด์ ๋ช
์์ ์ธ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ์์ฑํ์ผ๋ฏ๋ก ๋ ๊ฐ์ง ์ ํ์ ๊ณ ์์ด๋ฅผ ๋ชจ๋ ๋ง๋ค ์ ์์ต๋๋ค. :) ๋ชจ๋ ๋ฉ์๋์ ๋ง์ฐฌ๊ฐ์ง๋ก ์์ฑ์์ ์ ๋ฌ๋๋ ์ธ์์ ์์๋ ๋งค์ฐ ์ค์ํฉ๋๋ค. ์์ฑ์์์ name ์ธ์ ์ age ์ธ์๋ฅผ ๋ฐ๊พธ๊ฒ ์ต๋๋ค .
public class Cat {
String name;
int age;
public Cat(int age, String name) {
this.name = name;
this.age = age;
}
public static void main(String[] args) {
Cat smudge = new Cat("Smudge", 10); // Error!
}
}
์ค๋ฅ! ์์ฑ์๋ Cat ๊ฐ์ฒด๊ฐ ์์ฑ๋ ๋ ์ซ์์ ๋ฌธ์์ด์ ์ด ์์๋๋ก ์ ๋ฌํด์ผ ํ๋ค๊ณ ๋ช
ํํ๊ฒ ๊ท์ ํฉ๋๋ค. ๋ฐ๋ผ์ ์ฐ๋ฆฌ ์ฝ๋๋ ์๋ํ์ง ์์ต๋๋ค. ์ด๊ฒ์ ๊ธฐ์ตํ๊ณ ์์ ์ ํด๋์ค๋ฅผ ์ ์ธํ ๋ ์ผ๋์ ๋์ญ์์ค.
public Cat(String name, int age) {
this.name = name;
this.age = age;
}
public Cat(int age, String name) {
this.age = age;
this.name = name;
}
์ด๋ค์ ์์ ํ ๋ค๋ฅธ ๋ ์์ฑ์์
๋๋ค! "์ ์์ฑ์๊ฐ ํ์ํ๊ฐ์?"๋ผ๋ ์ง๋ฌธ์ ๋ํ ๋ต์ ํ ๋ฌธ์ฅ์ผ๋ก ํํํ๋ค๋ฉด "๊ฐ์ฒด๊ฐ ํญ์ ์ ํจํ ์ํ๋ฅผ ๊ฐ๋๋ก ํ๊ธฐ ์ํด"๋ผ๊ณ ๋งํ ์ ์์ต๋๋ค. ์์ฑ์๋ฅผ ์ฌ์ฉํ๋ฉด ๋ชจ๋ ๋ณ์๊ฐ ์ฌ๋ฐ๋ฅด๊ฒ ์ด๊ธฐํ๋ฉ๋๋ค. ํ๋ก๊ทธ๋จ์๋ ์๋๊ฐ 0์ธ ์๋์ฐจ๋ ๊ธฐํ "์ ํจํ์ง ์์" ๊ฐ์ฒด๊ฐ ์์ต๋๋ค. ๊ทธ๋ค์ ์ฃผ์ ์ด์ ์ ํ๋ก๊ทธ๋๋จธ๋ฅผ ์ํ ๊ฒ์
๋๋ค. ํ๋๋ฅผ ์๋์ผ๋ก ์ด๊ธฐํํ๋ฉด(๊ฐ์ฒด๋ฅผ ๋ง๋ ํ) ๋ฌด์ธ๊ฐ๋ฅผ ๋์น๊ณ ๋ฒ๊ทธ๊ฐ ์๊ธธ ์ํ์ด ํฝ๋๋ค. ๊ทธ๋ฌ๋ ์ด๊ฒ์ ์์ฑ์์์๋ ๋ฐ์ํ์ง ์์ต๋๋ค. ํ์ํ ๋ชจ๋ ์ธ์๋ฅผ ์ ๋ฌํ์ง ๋ชปํ๊ฑฐ๋ ์๋ชป๋ ์ ํ์ ์ธ์๋ฅผ ์ ๋ฌํ๋ฉด ์ปดํ์ผ๋ฌ๋ ์ฆ์ ์ค๋ฅ๋ฅผ ๋ฑ๋กํฉ๋๋ค. ์ฐ๋ฆฌ๋ ๋ํ ๋น์ ์ด ๋น์ ์ ํ๋ก๊ทธ๋จ์ ๋ฃ์ง ๋ง์์ผ ํ๋ค๊ณ ๋ณ๋๋ก ๋งํด์ผ ํฉ๋๋ค.' ์์ฑ์ ๋ด๋ถ์ ๋
ผ๋ฆฌ. ์ด๊ฒ์ด ๋ฐ๋ก ๋ฐฉ๋ฒ์
๋๋ค. ๋ฉ์๋๋ ํ์ํ ๋ชจ๋ ๊ธฐ๋ฅ์ ์ ์ํด์ผ ํ๋ ๊ณณ์
๋๋ค. ์์ฑ์์ ๋
ผ๋ฆฌ๋ฅผ ์ถ๊ฐํ๋ ๊ฒ์ด ๋์ ์๊ฐ์ธ ์ด์ ๋ฅผ ์ดํด๋ณด๊ฒ ์ต๋๋ค.
public class CarFactory {
String name;
int age;
int carsCount;
public CarFactory(String name, int age, int carsCount) {
this.name = name;
this.age = age;
this.carsCount = carsCount;
System.out.println("Our car factory is called " + this.name);
System.out.println("It was founded " + this.age + " years ago" );
System.out.println("Since that time, it has produced " + this.carsCount + " cars");
System.out.println("On average, it produces " + (this.carsCount/this.age) + " cars per year");
}
public static void main(String[] args) {
CarFactory ford = new CarFactory("Ford", 115 , 50000000);
}
}
์๋์ฐจ ๊ณต์ฅ์ ์ค๋ช
ํ๋ CarFactory ํด๋์ค๊ฐ ์์ต๋๋ค . ์์ฑ์ ๋ด๋ถ์์ ๋ชจ๋ ํ๋๋ฅผ ์ด๊ธฐํํ๊ณ ์ผ๋ถ ๋
ผ๋ฆฌ๋ฅผ ํฌํจํฉ๋๋ค. ๊ณต์ฅ์ ๋ํ ์ผ๋ถ ์ ๋ณด๋ฅผ ํ์ํฉ๋๋ค. ์ด ์ ๋๋ฉด ๋์ ๊ฑด ์๋ ๊ฒ ๊ฐ์ต๋๋ค. ํ๋ก๊ทธ๋จ์ด ์ ์๋ํฉ๋๋ค. ์ฝ์ ์ถ๋ ฅ: ์ฐ๋ฆฌ ์๋์ฐจ ๊ณต์ฅ์ Ford๋ผ๊ณ ํฉ๋๋ค. ์ค๋ฆฝ๋ ์ง 115๋
์ด ๋ ์ด๋ ์ง๊ธ๊น์ง 5000๋ง ๋์ ์๋์ฐจ๋ฅผ ์์ฐํ์ต๋๋ค. ํ๊ท ์ ์ผ๋ก ์ฐ๊ฐ 434782๋์ ์๋์ฐจ๋ฅผ ์์ฐํฉ๋๋ค. ํ์ง๋ง ์ค์ ๋ก ์๊ฐ ์ง์ฐ ๊ด์ฐ์ ๊น์์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ด๋ฐ ์ข
๋ฅ์ ์ฝ๋๋ ๋งค์ฐ ์ฝ๊ฒ ์ค๋ฅ๋ก ์ด์ด์ง ์ ์์ต๋๋ค. ์ด์ ์ฐ๋ฆฌ๋ Ford๊ฐ ์๋๋ผ 1๋
๋ฏธ๋ง ๋์ ์กด์ฌํ๊ณ 1000๋์ ์๋์ฐจ๋ฅผ ์์ฐํ "Amigo Motors"๋ผ๋ ์๋ก์ด ๊ณต์ฅ์ ๋ํด ์ด์ผ๊ธฐํ๊ณ ์๋ค๊ณ ๊ฐ์ ํฉ๋๋ค.
public class CarFactory {
String name;
int age;
int carsCount;
public CarFactory(String name, int age, int carsCount) {
this.name = name;
this.age = age;
this.carsCount = carsCount;
System.out.println("Our car factor is called " + this.name);
System.out.println("It was founded " + this.age + " years ago" );
System.out.println("Since that time, it has produced " + this.carsCount + " cars");
System.out.println("On average, it produces " + (this.carsCount/this.age) + " cars per year");
}
public static void main(String[] args) {
CarFactory ford = new CarFactory("Amigo Motors", 0 , 1000);
}
}
์ฝ์ ์ถ๋ ฅ: ์ฐ๋ฆฌ ์๋์ฐจ ๊ณต์ฅ์ "main" ์ค๋ ๋์์ Amigo Motors ์์ธ๋ผ๊ณ ํฉ๋๋ค. java.lang.ArithmeticException: / by zero ์ค๋ฆฝ๋ ์ง 0๋
์ด ์ง๋ ์ง๊ธ๊น์ง CarFactory์์ 1000๋์ ์๋์ฐจ๋ฅผ ์์ฐํ์ต๋๋ค .
System.out.println("On average, it produces " + (this.carsCount/this.age) + " cars per year");
์ฌ๊ธฐ์์๋ ๊ณ์ฐ์ ์ํํ๊ณ ์์ฐ๋ ์๋์ฐจ ์๋ฅผ ๊ณต์ฅ ์ฐ์์ผ๋ก ๋๋๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ฐ๋ฆฌ ๊ณต์ฅ์ ์ ๊ณต์ฅ์ด๊ธฐ ๋๋ฌธ์(์ฆ, 0๋
์ด ๋์๊ธฐ ๋๋ฌธ์) ์ํ์์๋ ํ ์ ์๋ 0์ผ๋ก ๋๋๋๋ค. ๊ฒฐ๊ณผ์ ์ผ๋ก ํ๋ก๊ทธ๋จ์ ์ค๋ฅ์ ํจ๊ป ์ข
๋ฃ๋ฉ๋๋ค.
GO TO FULL VERSION