본문 바로가기
Interview

[내일배움캠프 53일차 TIL] 기술 면접 대비 01. 객체와 한정자

by 식냥 2024. 7. 1.
728x90

 

[ 확인 문제 ]

using System;

public class Car
{
    // 속성
    public string Make { get; set; }
    public string Model { get; set; }
    public int Year { get; set; }

    // 메서드
    public void DisplayInfo()
    {
        Console.WriteLine($"Car Make: {Make}, Model: {Model}, Year: {Year}");
    }
}

class Program
{
    static void Main(string[] args)
    {
        Car car1 = new Car();
        car1.Make = "Genesis";
        car1.Model = "G70";
        car1.Year = 2020;
        car1.DisplayInfo();

        Car car2 = new Car();
        car2.Make = "Hyundai";
        car2.Model = "Sonata";
        car2.Year = 2022;
        car2.DisplayInfo();
    }
}

1. 위 예제 코드에서 Car는 무엇을 나타내나요? car1과 car2는 무엇을 나타내나요?

: Car는 클래스이다. car1과 car2는 Car 클래스의 객체이다.

 

2. 위 코드에서 Car 클래스에 정의된 메서드를 호출할 때 car1과 car2를 사용해야 하는 이유는 무엇인가요?

: 같은 클래스의 객체라도 각 객체가 별도의 메서드를 가지고 있기 때문에 car1과 car2로 구분해 주어야 한다.

 

3. 위 코드에 어떤 메서드를 추가하고 아래와 같이 사용했습니다. SomeMethod에는 static 한정자가 붙어있을까요? 그 이유는 무엇인가요?

Car.SomeMethod();

: Main 메서드가 static이기 때문에 붙어있다.

 

[ 설명 문제 ]

1. 객체란 무엇인가요? 클래스와 어떤 연관이 있나요?

: 변수와 메서드를 정의한 클래스를 실체화한 것이 객체이다.

 

2. 생성자에 대해 간단하게 설명해주세요.

클래스 명과 같으며 객체를 생성할 때 바로 초기화할 수 있다.

 

3. 접근제한자란 무엇이며, 각각 어떤 차이가 있는지 비교해서 설명해주세요.

외부의 접근 허용 정도이다. public은 모든 곳에서 접근할 수 있고, private은 클래스 내에서만 접근이 가능하며, protected는 상속 받은 클래스까지 접근할 수 있다.

 

4. static 한정자에 대해 설명해주세요.

객체로 생성되지 않으며 클래스 자체로 접근해서 호출할 수 있다. 각 정적 속성, 필드, 메서드는 객체로 생성될 수 없기 때문에 클래스 내에 단 하나로 존재한다.

 

[ 실습 문제 ]

💡 [현재까지 생성된 Item의 개수를 추적해보기]

생성자를 통해 아이템을 생성할 때마다, 지금까지 생성된 아이템의 개수를 따로 저장하는 변수를 Item에 만들고 싶습니다.

Item : 아이템의 정보를 저장하는 클래스

using System;

namespace StaticFieldPractice
{
    public class Item
    {
        // TODO 1: Item의 개수를 저장하는 static 필드 선언 //
        
        //

        public string Name { get; set; }

        // 생성자 함수
        public Item(string name)
        {
            this.Name = name;
            // TODO 2: static 필드 값을 증가시키기 //
            
            // ******************************** //
        }

        
        public static int GetItemCount()
        {
            // TODO 3: 현재까지 생성된 Item 개수를 반환  //
            
            // ******************************** //
        }
    }

    class Program
    static void Main(string[] args)
        {
            // Item 인스턴스 생성
            Item item1 = new Item("Item1");
            Item item2 = new Item("Item2");

            // 생성된 Item 개수 출력
            Console.WriteLine("Total Items Created: " + Item.GetItemCount());
        }
    }
}

 

개별 코드

// TODO 1: Item의 개수를 저장하는 static 필드 선언 //
public static int count;

 

public Item(string name)
        {
            this.Name = name;
            // TODO 2: static 필드 값을 증가시키기 //
            count += 1;
        }

 

public static int GetItemCount()
        {
            // TODO 3: 현재까지 생성된 Item 개수를 반환  //
            retutn count;
        }

 

전체 코드

using System;

namespace StaticFieldPractice
{
    public class Item
    {
        public static int count;
        public string Name { get; set; }

        // 생성자 함수
        public Item(string name)
        {
            this.Name = name;
            count += 1;
        }

        
        public static int GetItemCount()
        {
            return count;
        }
    }

    class Program
    static void Main(string[] args)
        {
            // Item 인스턴스 생성
            Item item1 = new Item("Item1");
            Item item2 = new Item("Item2");

            // 생성된 Item 개수 출력
            Console.WriteLine("Total Items Created: " + Item.GetItemCount());
        }
    }
}
728x90
반응형