CodeGym /Java Blog /๋ฌด์ž‘์œ„์˜ /๊ฐœ์ฒด ์ƒ์„ฑ ์ค‘ ์ผ๋ จ์˜ ์ž‘์—…
John Squirrels
๋ ˆ๋ฒจ 41
San Francisco

๊ฐœ์ฒด ์ƒ์„ฑ ์ค‘ ์ผ๋ จ์˜ ์ž‘์—…

๋ฌด์ž‘์œ„์˜ ๊ทธ๋ฃน์— ๊ฒŒ์‹œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค
์•ˆ๋…•! ์˜ค๋Š˜์˜ ์ˆ˜์—…์€ ์ƒ๋‹นํžˆ... ์–ด... ๋‹ค๋ฉด์ ์ผ ๊ฒƒ์ž…๋‹ˆ๋‹ค :) ๊ด‘๋ฒ”์œ„ํ•œ ์ฃผ์ œ๋ฅผ ๋‹ค๋ฃฐ ๊ฒƒ์ด๋ผ๋Š” ์˜๋ฏธ์—์„œ ๊ทธ๊ฒƒ๋“ค์€ ๋ชจ๋‘ ๊ฐ์ฒด ์ƒ์„ฑ ํ”„๋กœ์„ธ์Šค ์™€ ๊ด€๋ จ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค . ๊ฐœ์ฒด ์ƒ์„ฑ ์ค‘ ์ž‘์—… ์ˆœ์„œ - 1์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœ๋˜๋Š” ๋ฐฉ๋ฒ•, ํ•„๋“œ(์ •์  ํ•„๋“œ ํฌํ•จ)๊ฐ€ ์ดˆ๊ธฐํ™”๋˜๋Š” ๋ฐฉ๋ฒ• ๋ฐ ์ˆœ์„œ ๋“ฑ์„ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ๋ถ„์„ํ•ฉ๋‹ˆ๋‹ค. ์ด์ „์— ๊ธฐ์‚ฌ์—์„œ ๋…ผ์˜๋œ ๋ช‡ ๊ฐ€์ง€ ์‚ฌํ•ญ์„ ๋‹ค๋ฃจ์—ˆ์œผ๋ฏ€๋กœ ํ•œ๋ˆˆ์— ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ธฐ๋ณธ ํด๋ž˜์Šค ์ƒ์„ฑ์ž ์— ๋Œ€ํ•œ ์ž๋ฃŒ . ๋จผ์ € ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜๋Š” ๋ฐฉ์‹์„ ๊ธฐ์–ตํ•ด ๋ด…์‹œ๋‹ค. ๊ฐœ๋ฐœ์ž์˜ ๊ด€์ ์—์„œ ์ด ํ”„๋กœ์„ธ์Šค๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ณด์ด๋Š”์ง€ ์ž˜ ๊ธฐ์–ตํ•˜์‹ค ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Š” ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์”๋‹ˆ๋‹ค new, ๊ทธ๋ฆฌ๊ณ  ๋ชจ๋“  ๊ฒƒ์ด ์ค€๋น„๋˜์—ˆ์Šต๋‹ˆ๋‹ค :) ์—ฌ๊ธฐ์—์„œ๋Š” ์šฐ๋ฆฌ๊ฐ€ ๊ธ€์„ ์“ธ ๋•Œ ์ปดํ“จํ„ฐ์™€ Java ๋จธ์‹  ๋‚ด๋ถ€์—์„œ ์ผ์–ด๋‚˜๋Š” ์ผ์— ๋Œ€ํ•ด ์ด์•ผ๊ธฐํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
Cat cat = new Cat();
์ด์ „์— ์ด์— ๋Œ€ํ•ด ์ด์•ผ๊ธฐํ–ˆ์ง€๋งŒ ๋งŒ์ผ์„ ๋Œ€๋น„ํ•˜์—ฌ ๋‹ค์Œ์„ ์ƒ๊ธฐ์‹œ์ผœ ๋“œ๋ฆฌ๊ฒ ์Šต๋‹ˆ๋‹ค.
  • ๋จผ์ € ๊ฐ์ฒด๋ฅผ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค.
  • ๋‹ค์Œ์œผ๋กœ Java ๋จธ์‹ ์€ ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค(์ด ๊ฒฝ์šฐ ์ฐธ์กฐ๋Š” Cat cat์ž…๋‹ˆ๋‹ค).
  • ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ณ€์ˆ˜๊ฐ€ ์ดˆ๊ธฐํ™”๋˜๊ณ  ์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค(์ด ํ”„๋กœ์„ธ์Šค๋ฅผ ์ž์„ธํžˆ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค).
๋˜ํ•œ ๊ฐœ์ฒด ์ˆ˜๋ช… ์ฃผ๊ธฐ ์— ๋Œ€ํ•œ ๋‹จ์›์—์„œ ๊ฐœ์ฒด์— ๋Œ€ํ•œ ์ฐธ์กฐ๊ฐ€ ํ•˜๋‚˜ ์ด์ƒ ์žˆ๋Š” ํ•œ ๊ฐœ์ฒด๊ฐ€ ์ง€์†๋œ๋‹ค๋Š” ๊ฒƒ์„ ๊ธฐ์–ตํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์•„๋ฌด๊ฒƒ๋„ ๋‚จ์ง€ ์•Š์œผ๋ฉด ๊ฐœ์ฒด๋Š” ๊ฐ€๋น„์ง€ ์ˆ˜์ง‘๊ธฐ์˜ ๋จน์ž‡๊ฐ์ด ๋ฉ๋‹ˆ๋‹ค. ๊ฐœ์ฒด ์ƒ์„ฑ ์ค‘ ์ž‘์—… ์ˆœ์„œ - 2์ด ์ฒ˜์Œ ๋‘ ํ•ญ๋ชฉ์€ ํŠน๋ณ„ํ•œ ์งˆ๋ฌธ์„ ์ œ๊ธฐํ•ด์„œ๋Š” ์•ˆ ๋ฉ๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น์€ ๊ฐ„๋‹จํ•œ ํ”„๋กœ์„ธ์Šค์ด๋ฉฐ ๊ฐ€๋Šฅํ•œ ๊ฒฐ๊ณผ๋Š” ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์žˆ๊ฑฐ๋‚˜ ์—†๊ฑฐ๋‚˜ ๋‘ ๊ฐ€์ง€๋ฟ์ž…๋‹ˆ๋‹ค. :) ๊ทธ๋ฆฌ๊ณ  ๋งํฌ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์€ ๋“œ๋ฌธ ์ผ์ด ์•„๋‹™๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์„ธ ๋ฒˆ์งธ ํ•ญ๋ชฉ์€ ์—„๊ฒฉํ•œ ์ˆœ์„œ๋กœ ์‹คํ–‰๋˜๋Š” ์ „์ฒด ์ž‘์—… ์ง‘ํ•ฉ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ €๋Š” ๋ฒผ๋ฝ์น˜๊ธฐ ์‹œํ—˜์„ ์ข‹์•„ํ•˜์ง€ ์•Š์ง€๋งŒ ์ด ๊ณผ์ •์„ ์ž˜ ์ดํ•ดํ•˜๊ณ  ์ด ์ผ๋ จ์˜ ์ž‘์—…์„ ์™ธ์›Œ์•ผ ํ•ฉ๋‹ˆ๋‹ค.. ์ด์ „ ๊ฐ•์˜์—์„œ ๊ฐ์ฒด ์ƒ์„ฑ ๊ณผ์ •์— ๋Œ€ํ•ด ์ด์•ผ๊ธฐํ–ˆ์„ ๋•Œ, ์—ฌ๋Ÿฌ๋ถ„์€ ์•„์ง ์ƒ์†์— ๋Œ€ํ•ด ์ „ํ˜€ ์•Œ์ง€ ๋ชปํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋ช‡ ๊ฐ€์ง€๋ฅผ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ์ด ๋ฌธ์ œ๊ฐ€ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ์ด์ œ ๊ฝค ๋งŽ์€ ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๊ณ  ๋งˆ์นจ๋‚ด ์ด ์งˆ๋ฌธ์„ ์™„์ „ํžˆ ๊ณ ๋ คํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค :) ๊ทธ๋ž˜์„œ ์„ธ ๋ฒˆ์งธ ์š”์ ์€ " ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ณ€์ˆ˜๊ฐ€ ์ดˆ๊ธฐํ™”๋˜๊ณ  ์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค. "๋ผ๊ณ  ๋งํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์ด ๋ชจ๋“  ์ผ์ด ์–ด๋–ค ์ˆœ์„œ๋กœ ๋ฐœ์ƒํ•ฉ๋‹ˆ๊นŒ? ๋” ๋‚˜์€ ์ดํ•ด๋ฅผ ์œ„ํ•ด ๋ถ€๋ชจ์™€ ์ž์‹์ด๋ผ๋Š” ๋งค์šฐ ๊ฐ„๋‹จํ•œ ๋‘ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
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 ์‹œ์Šคํ…œ์—์„œ ํ”„๋กœ์„ธ์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
  1. ๊ฐ€์žฅ ๋จผ์ € ์ผ์–ด๋‚˜๋Š” ์ผ์€ ํด๋ž˜์Šค ์˜ ์ •์  ๋ณ€์ˆ˜๊ฐ€ Vehicle์ดˆ๊ธฐํ™”๋˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค . ๋„ค, ์ˆ˜์—… Vehicle์ด ์•„๋‹ˆ๋ผ Truck. ์ •์  ๋ณ€์ˆ˜๋Š” ์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœ๋˜๊ธฐ ์ „์— ์ดˆ๊ธฐํ™”๋˜๋ฉฐ ์ด๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค์—์„œ ์‹œ์ž‘๋ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ ํ™•์ธํ•ด๋ณด์ž. vehicleCounterํด๋ž˜์Šค ์˜ ํ•„๋“œ๋ฅผ Vehicle10์œผ๋กœ ์„ค์ •ํ•˜๊ณ  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
  2. ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ์ •์  ๋ณ€์ˆ˜๊ฐ€ ์ดˆ๊ธฐํ™”๋œ ํ›„ ์ž์‹ ํด๋ž˜์Šค์˜ ์ •์  ๋ณ€์ˆ˜๊ฐ€ ์ดˆ๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค. ์šฐ๋ฆฌ์˜ ๊ฒฝ์šฐ ์ด๊ฒƒ์€ ํด๋ž˜์Šค 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 .

  3. ์•„์ง ์ƒ์„ฑ์ž๋ฅผ ์œ„ํ•œ ์‹œ๊ฐ„์ด ์•„๋‹™๋‹ˆ๋‹ค! ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”๊ฐ€ ๊ณ„์†๋ฉ๋‹ˆ๋‹ค.โ€ฏ๋ถ€๋ชจ ํด๋ž˜์Šค์˜ ๋น„์ •์  ๋ณ€์ˆ˜๋Š” ์„ธ ๋ฒˆ์งธ๋กœ ์ดˆ๊ธฐํ™”๋ฉ๋‹ˆ๋‹ค.โ€ฏ๋ณด์‹œ๋‹ค์‹œํ”ผ ์ƒ์†์€ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ณผ์ •์„ ์ƒ๋‹นํžˆ ๋ณต์žกํ•˜๊ฒŒ ๋งŒ๋“ค์ง€๋งŒ ์ด์— ๋Œ€ํ•ด ํ•  ์ˆ˜ ์žˆ๋Š” ์ผ์€ ์—†์Šต๋‹ˆ๋‹ค. ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ๋ช‡ ๊ฐ€์ง€๋งŒ ๊ธฐ์–ตํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. :)

    ์‹คํ—˜์œผ๋กœ ํด๋ž˜์Šค 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ํ•„๋“œ์— ์ด๋ฏธ ๊ฐ’์ด ํ• ๋‹น๋˜์—ˆ์Œ์„ ์ฆ๋ช…ํ•ฉ๋‹ˆ๋‹ค.

  4. ๋งˆ์ง€๋ง‰์œผ๋กœ ์ƒ์„ฑ์ž๋ฅผ ์œ„ํ•œ ์‹œ๊ฐ„์ž…๋‹ˆ๋‹ค!โ€ฏ๋ณด๋‹ค ์ •ํ™•ํ•˜๊ฒŒ๋Š” ๊ธฐ๋ณธ ํด๋ž˜์Šค ์ƒ์„ฑ์ž๋ฅผ ์œ„ํ•œ ์‹œ๊ฐ„์ž…๋‹ˆ๋‹ค. ๊ฐœ์ฒด ์ƒ์„ฑ ํ”„๋กœ์„ธ์Šค์˜ ๋„ค ๋ฒˆ์งธ ๋‹จ๊ณ„์—์„œ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

    ์ด๊ฒƒ์€ ๋˜ํ•œ ํ™•์ธํ•˜๊ธฐ๊ฐ€ ๋งค์šฐ ์‰ฝ์Šต๋‹ˆ๋‹ค. ์ฝ˜์†”์— ๋‘ ์ค„์„ ์ถœ๋ ฅํ•ด ๋ด…์‹œ๋‹ค. ํ•˜๋‚˜๋Š” 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!

    ํ›Œ๋ฅญํ•œ. ๊ทธ๊ฒƒ์€ ์šฐ๋ฆฌ๊ฐ€ ํ‹€๋ฆฌ์ง€ ์•Š์•˜๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค :) ๊ณ„์† ์ง„ํ–‰ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

  5. ์ด์ œ ํ•˜์œ„ ํด๋ž˜์Šค, ์ฆ‰ ์šฐ๋ฆฌ ํด๋ž˜์Šค์˜ ๋น„์ •์  ํ•„๋“œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•  ์‹œ๊ฐ„์ž…๋‹ˆ๋‹ค 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์ž…๋‹ˆ๋‹ค!TruckmaxSpeed

  6. ์ž์‹ ํด๋ž˜์Šค ์˜ ์ƒ์„ฑ์ž Truck๊ฐ€ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

    ๊ทธ๋ฆฌ๊ณ  ์ด ์‹œ์ ์—์„œ ๋งˆ์ง€๋ง‰์œผ๋กœ ์ธ์Šคํ„ด์Šคํ™”ํ•˜๋Š” ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๊ฐ€ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค!

    ์—ฌ์„ฏ ๋ฒˆ์งธ ๋‹จ๊ณ„์—์„œ๋งŒ ํŠธ๋Ÿญ์— ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•˜๋Š” ๊ฐ’์ด ํ•„๋“œ์— ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค.

    ๋ณด์‹œ๋‹ค์‹œํ”ผ ํŠธ๋Ÿญ์„ "๊ตฌ์ถ•"ํ•˜๋Š” ๊ฒƒ, ์ฆ‰ ๊ฐ์ฒด ์ƒ์„ฑ ํ”„๋กœ์„ธ์Šค๋Š” ๊ฐ„๋‹จํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์šฐ๋ฆฌ๋Š” ๊ทธ๊ฒƒ์„ ๊ฐ€์žฅ ์ž‘์€ ๋ถ€๋ถ„์œผ๋กœ ๋‚˜๋ˆˆ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค :)

๊ฐœ์ฒด ์ƒ์„ฑ ์ค‘ ์ž‘์—… ์ˆœ์„œ - 3 ์ด ๊ณผ์ •์„ ์ž˜ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด ์™œ ๊ทธ๋ ‡๊ฒŒ ์ค‘์š”ํ•œ๊ฐ€์š”? "๋‚ด๋ถ€"์—์„œ ๋ฌด์Šจ ์ผ์ด ์ผ์–ด๋‚˜๊ณ  ์žˆ๋Š”์ง€ ์ •ํ™•ํžˆ ์•Œ์ง€ ๋ชปํ•œ๋‹ค๋ฉด ์ผ๋ฐ˜ ๊ฐœ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์–ผ๋งˆ๋‚˜ ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋Š”์ง€ ์ƒ์ƒํ•ด ๋ณด์‹ญ์‹œ์˜ค :) ์ด์ œ ์ฝ”์Šค๋กœ ๋Œ์•„๊ฐ€์„œ ๋ช‡ ๊ฐ€์ง€ ์ž‘์—…์„ ์™„๋ฃŒํ•  ์‹œ๊ฐ„์ž…๋‹ˆ๋‹ค! ํ–‰์šด์„ ๋นŒ๋ฉฐ ๊ณง ๋ต™๊ฒ ์Šต๋‹ˆ๋‹ค! :) ๊ฐœ์ฒด ์ƒ์„ฑ ์ค‘ ์ž‘์—… ์ˆœ์„œ - 4
์ฝ”๋ฉ˜ํŠธ
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION