
new
, ๊ทธ๋ฆฌ๊ณ ๋ชจ๋ ๊ฒ์ด ์ค๋น๋์์ต๋๋ค :) ์ฌ๊ธฐ์์๋ ์ฐ๋ฆฌ๊ฐ ๊ธ์ ์ธ ๋ ์ปดํจํฐ์ Java ๋จธ์ ๋ด๋ถ์์ ์ผ์ด๋๋ ์ผ์ ๋ํด ์ด์ผ๊ธฐํ๊ฒ ์ต๋๋ค. ์๋ฅผ ๋ค๋ฉด ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
Cat cat = new Cat();
์ด์ ์ ์ด์ ๋ํด ์ด์ผ๊ธฐํ์ง๋ง ๋ง์ผ์ ๋๋นํ์ฌ ๋ค์์ ์๊ธฐ์์ผ ๋๋ฆฌ๊ฒ ์ต๋๋ค.
- ๋จผ์ ๊ฐ์ฒด๋ฅผ ์ ์ฅํ๊ธฐ ์ํ ๋ฉ๋ชจ๋ฆฌ๊ฐ ํ ๋น๋ฉ๋๋ค.
- ๋ค์์ผ๋ก Java ๋จธ์ ์ ๊ฐ์ฒด์ ๋ํ ์ฐธ์กฐ๋ฅผ ์์ฑํฉ๋๋ค(์ด ๊ฒฝ์ฐ ์ฐธ์กฐ๋ Cat cat์ ๋๋ค).
- ๋ง์ง๋ง์ผ๋ก ๋ณ์๊ฐ ์ด๊ธฐํ๋๊ณ ์์ฑ์๊ฐ ํธ์ถ๋ฉ๋๋ค(์ด ํ๋ก์ธ์ค๋ฅผ ์์ธํ ์ดํด๋ณด๊ฒ ์ต๋๋ค).

public class Vehicle {
public static int vehicleCounter = 0;
private String description = "Vehicle";
public Vehicle() {
}
public String getDescription() {
return description;
}
}
public class Truck extends Vehicle {
private static int truckCounter = 0;
private int yearOfManufacture;
private String model;
private int maxSpeed;
public Truck(int yearOfManufacture, String model, int maxSpeed) {
this.yearOfManufacture = yearOfManufacture;
this.model = model;
this.maxSpeed = maxSpeed;
Vehicle.vehicleCounter++;
truckCounter++;
}
}
์ด Truck
ํด๋์ค๋ ์ฐ๋, ๋ชจ๋ธ ๋ฐ ์ต๋ ์๋๋ฅผ ๋ํ๋ด๋ ํ๋๊ฐ ์๋ ํธ๋ญ์ ๊ตฌํํ ๊ฒ์
๋๋ค. ์ด์ ์ฐ๋ฆฌ๋ ๊ทธ๋ฌํ ๊ฐ์ฒด๋ฅผ ํ๋ ๋ง๋ค๊ณ ์ถ์ต๋๋ค.
public class Main {
public static void main(String[] args) throws IOException {
Truck truck = new Truck(2017, "Scania S 500 4x2", 220);
}
}
Java ์์คํ
์์ ํ๋ก์ธ์ค๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
-
๊ฐ์ฅ ๋จผ์ ์ผ์ด๋๋ ์ผ์ ํด๋์ค ์ ์ ์ ๋ณ์๊ฐ
Vehicle
์ด๊ธฐํ๋๋ ๊ฒ ์ ๋๋ค . ๋ค, ์์Vehicle
์ด ์๋๋ผTruck
. ์ ์ ๋ณ์๋ ์์ฑ์๊ฐ ํธ์ถ๋๊ธฐ ์ ์ ์ด๊ธฐํ๋๋ฉฐ ์ด๋ ๋ถ๋ชจ ํด๋์ค์์ ์์๋ฉ๋๋ค. ์ด๊ฒ์ ํ์ธํด๋ณด์.vehicleCounter
ํด๋์ค ์ ํ๋๋ฅผVehicle
10์ผ๋ก ์ค์ ํ๊ณVehicle
๋ฐTruck
์์ฑ์ ๋ชจ๋์ ํ์ํ๋ ค๊ณ ํฉ๋๋ค.public class Vehicle { public static int vehicleCounter = 10; private String description = "Vehicle"; public Vehicle() { System.out.println(vehicleCounter); } public String getDescription() { return description; } } public class Truck extends Vehicle { private static int truckCount = 0; private int yearOfManufacture; private String model; private int maxSpeed; public Truck(int yearOfManufacture, String model, int maxSpeed) { System.out.println(vehicleCounter); this.yearOfManufacture = yearOfManufacture; this.model = model; this.maxSpeed = maxSpeed; Vehicle.vehicleCounter++; truckCount++; } }
๊ฐ ํ์๋
Truck
๋ ํธ๋ญ์ ํ๋๊ฐ ์์ง ์ด๊ธฐํ๋์ง ์์์์ ํ์ธํ๊ธฐ ์ํด ์๋์ ์ผ๋ก ์์ฑ์์ ๋งจ ์ฒ์์ println ๋ฌธ์ ๋ฃ์ต๋๋ค .vehicleCounter
๊ฒฐ๊ณผ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
10 10
-
๋ถ๋ชจ ํด๋์ค์ ์ ์ ๋ณ์๊ฐ ์ด๊ธฐํ๋ ํ ์์ ํด๋์ค์ ์ ์ ๋ณ์๊ฐ ์ด๊ธฐํ๋ฉ๋๋ค. ์ฐ๋ฆฌ์ ๊ฒฝ์ฐ ์ด๊ฒ์ ํด๋์ค
truckCounter
์ ํ๋ ์ ๋๋คTruck
.๋ค๋ฅธ ํ๋๊ฐ ์ด๊ธฐํ๋๊ธฐ ์ ์ ์์ฑ์
truckCounter
๋ด๋ถ ์ ๊ฐ์ ํ์ํ๋ ๋ ๋ค๋ฅธ ์คํ์ ํด๋ด ์๋ค .Truck
public class Truck extends Vehicle { private static int truckCounter = 10; private int yearOfManufacture; private String model; private int maxSpeed; public Truck(int yearOfManufacture, String model, int maxSpeed) { System.out.println(truckCounter); this.yearOfManufacture = yearOfManufacture; this.model = model; this.maxSpeed = maxSpeed; Vehicle.vehicleCounter++; truckCounter++; } }
๋ณด์ ๋ค์ํผ ์์ฑ์๊ฐ ์์๋ ๋ ๊ฐ 10์ด ์ด๋ฏธ ์ ์ ๋ณ์์ ํ ๋น๋์์ต๋๋ค
Truck
. -
์์ง ์์ฑ์๋ฅผ ์ํ ์๊ฐ์ด ์๋๋๋ค! ๋ณ์ ์ด๊ธฐํ๊ฐ ๊ณ์๋ฉ๋๋ค.โฏ๋ถ๋ชจ ํด๋์ค์ ๋น์ ์ ๋ณ์๋ ์ธ ๋ฒ์งธ๋ก ์ด๊ธฐํ๋ฉ๋๋ค.โฏ๋ณด์๋ค์ํผ ์์์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๊ณผ์ ์ ์๋นํ ๋ณต์กํ๊ฒ ๋ง๋ค์ง๋ง ์ด์ ๋ํด ํ ์ ์๋ ์ผ์ ์์ต๋๋ค. ํ๋ก๊ทธ๋๋ฐ์์ ๋ช ๊ฐ์ง๋ง ๊ธฐ์ตํ๋ฉด ๋ฉ๋๋ค. :)
์คํ์ผ๋ก ํด๋์ค
description
์ ๋ณ์ ์ ์ด๊ธฐ ๊ฐ์ ํ ๋นํVehicle
๋ค์ ์์ฑ์์์ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.public class Vehicle { public static int vehicleCounter = 10; private String description = "Initial value of the description field"; public Vehicle() { System.out.println(description); description = "Vehicle"; System.out.println(description); } public String getDescription() { return description; } }
main()
ํธ๋ญ์ ์์ฑํ๋ ๋ฉ์๋๋ฅผ ์คํํด ๋ณด๊ฒ ์ต๋๋ค .public class Main { public static void main(String[] args) throws IOException { Truck truck = new Truck(2017, "Scania S 500 4x2", 220); } }
๋ค์ ๊ฒฐ๊ณผ๋ฅผ ์ป์ต๋๋ค.
Initial value of the description field Vehicle
์ด๋
Vehicle
์์ฑ์๊ฐ ์์ํ ๋description
ํ๋์ ์ด๋ฏธ ๊ฐ์ด ํ ๋น๋์์์ ์ฆ๋ช ํฉ๋๋ค. -
๋ง์ง๋ง์ผ๋ก ์์ฑ์๋ฅผ ์ํ ์๊ฐ์ ๋๋ค!โฏ๋ณด๋ค ์ ํํ๊ฒ๋ ๊ธฐ๋ณธ ํด๋์ค ์์ฑ์๋ฅผ ์ํ ์๊ฐ์ ๋๋ค. ๊ฐ์ฒด ์์ฑ ํ๋ก์ธ์ค์ ๋ค ๋ฒ์งธ ๋จ๊ณ์์ ํธ์ถ๋ฉ๋๋ค.
์ด๊ฒ์ ๋ํ ํ์ธํ๊ธฐ๊ฐ ๋งค์ฐ ์ฝ์ต๋๋ค. ์ฝ์์ ๋ ์ค์ ์ถ๋ ฅํด ๋ด ์๋ค. ํ๋๋
Vehicle
๊ธฐ๋ณธ ํด๋์ค ์์ฑ์ ๋ด๋ถ์ด๊ณ ๋ค๋ฅธ ํ๋๋Truck
์์ฑ์ ๋ด๋ถ์ ๋๋ค.Vehicle
๋ด๋ถ ์ค์ด ๋จผ์ ํ์๋๋์ง ํ์ธํด์ผ ํฉ๋๋ค .public Vehicle() { System.out.println("Hello from the Vehicle constructor!"); } public Truck(int yearOfManufacture, String model, int maxSpeed) { System.out.println("Hello from the Truck constructor!"); this.yearOfManufacture = yearOfManufacture; this.model = model; this.maxSpeed = maxSpeed; Vehicle.vehicleCounter++; truckCounter++; }
์ฐ๋ฆฌ๋ ์ฐ๋ฆฌ์ ๋ฐฉ๋ฒ์ ์คํ
main()
ํ๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ๊ฒ์ ๋๋ค:Hello from the Vehicle constructor! Hello from the Truck constructor!
ํ๋ฅญํ. ๊ทธ๊ฒ์ ์ฐ๋ฆฌ๊ฐ ํ๋ฆฌ์ง ์์๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค :) ๊ณ์ ์งํํ๊ฒ ์ต๋๋ค.
-
์ด์ ํ์ ํด๋์ค, ์ฆ ์ฐ๋ฆฌ ํด๋์ค์ ๋น์ ์ ํ๋๋ฅผ ์ด๊ธฐํํ ์๊ฐ์ ๋๋ค
Truck
. ์ธ์คํด์คํ๋๋ ํด๋์ค ๋ด์ ํ๋๋ ๋ค์ฏ ๋ฒ์งธ ๋จ๊ณ๊น์ง ์ด๊ธฐํ๋์ง ์์ต๋๋ค! ๋๋์ง๋ง ์ฌ์ค์ ๋๋ค :) ๋ค์ ํ ๋ฒ ๊ฐ๋จํ ๊ฒ์ฌ๋ฅผ ์ํํฉ๋๋ค. ๋ถ๋ชจ ํด๋์ค์ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ณ์์ ์ด๊ธฐ ๊ฐ์ ์ง์ ํ๊ณ ์์ฑ์maxSpeed
์์Truck
์์ฑ์๊ฐ ์์๋๊ธฐ ์ ์ ๊ฐ์ด ํ ๋น๋์๋์ง ํ์ธํฉ๋๋ค.public class Truck extends Vehicle { private static int truckCounter = 10; private int yearOfManufacture; private String model; private int maxSpeed = 150; public Truck(int yearOfManufacture, String model, int maxSpeed) { System.out.println("Initial value of maxSpeed = " + this.maxSpeed); this.yearOfManufacture = yearOfManufacture; this.model = model; this.maxSpeed = maxSpeed; Vehicle.vehicleCounter++; truckCounter++; } }
์ฝ์ ์ถ๋ ฅ:
Initial value of maxSpeed = 150
๋ณด์โฏ โฏ๋ค์ํผ ์์ฑ์๊ฐ ์์๋๋ฉด ์ด๋ฏธ โฏ150์ ๋๋ค!
Truck
maxSpeed
-
์์ ํด๋์ค ์ ์์ฑ์
Truck
๊ฐ ํธ์ถ๋ฉ๋๋ค.๊ทธ๋ฆฌ๊ณ ์ด ์์ ์์ ๋ง์ง๋ง์ผ๋ก ์ธ์คํด์คํํ๋ ํด๋์ค์ ์์ฑ์๊ฐ ํธ์ถ๋ฉ๋๋ค!
์ฌ์ฏ ๋ฒ์งธ ๋จ๊ณ์์๋ง ํธ๋ญ์ ์ธ์๋ก ์ ๋ฌํ๋ ๊ฐ์ด ํ๋์ ํ ๋น๋ฉ๋๋ค.
๋ณด์๋ค์ํผ ํธ๋ญ์ "๊ตฌ์ถ"ํ๋ ๊ฒ, ์ฆ ๊ฐ์ฒด ์์ฑ ํ๋ก์ธ์ค๋ ๊ฐ๋จํ์ง ์์ต๋๋ค. ๊ทธ๋ฌ๋ ์ฐ๋ฆฌ๋ ๊ทธ๊ฒ์ ๊ฐ์ฅ ์์ ๋ถ๋ถ์ผ๋ก ๋๋ ๊ฒ ๊ฐ์ต๋๋ค :)

GO TO FULL VERSION