C#교과서 마스터하기 35. 개체와 개체 지향 프로그래밍 요약 정리

min seung moon·2021년 7월 14일
0

C#

목록 보기
37/54

https://www.youtube.com/watch?v=cD6PA0ngXDM&list=PLO56HZSjrPTB4NxAsEP8HRk6YKBDLbp7m&index=70

1. 개체와 개체 지향 프로그래밍

  • F11로 테스트 하기
using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Reflection;

// [1] 네임스페이스 : 클래스명 충돌 방지
namespace testProject
{
    // [2] 인터페이스 : 표준, 다중상속
    interface IStandard { void Run(); } // 추상화

    // [3] 클래스 : 설계도
    class Car : IStandard // 상속
    {
        // 캡슐화
        #region [4] 필드 : Private Member Variable
        private string name; // 필드 : 부품
        private string[] names; // 배열형 필드
        private readonly int _Length; // 읽기 전용 필드
        #endregion

        #region [5] 생성자 : Constructors
        public Car()
        {
            this.name = "좋은차"; // 필드를 기본값으로 초기화
        }
        public Car(string name) // 생성자 : 조립/시동, 필드 초기화
        {
            this.name = name;
        }
        public Car(int length)
        {
            this.name = "좋은차";
            _Length = length; // 읽기 전용 필드는 생성자에 의해서 초기화 가능
            names = new string[length]; // 넘겨온 값으로 요소 생성
        }
        #endregion

        #region [6] 메서드 : Public Method
        // 메서드 : 기능/동작
        public void Run() => WriteLine("{0} 자동차가 달립니다.",name);
        #endregion

        #region [7] 속성 : Public Properties
        public string Name // 속성 : private 필드 -> 외부 공개
        {
            get { return name; }
            set { name = value;  }
        }
        public int Length { get { return _Length;  } }
        #endregion

        #region [8] 소멸자 : Destructor
        ~Car() // 소멸자 : 폐차, 만들어진 객체 소멸될 때
        {
            WriteLine("{0} 자동차가 폐차됨", name);
        }
        #endregion

        #region [9] 인덱서 : Indexer
        public string this[int index] // 인덱서 : 카탈로그 화
        {
            get { return names[index];  }
            set { names[index] = value;  }
        }
        #endregion

        #region [10] 이터레이터 : Iterfators
        public IEnumerator GetEnumerator() // 반복키
        {
            for(int i = 0; i < _Length; i++)
            {
                yield return names[i];
            }
        }
        #endregion
        
        #region [11] 대리자 : Public Delefates
        public delegate void EventHandler(); // 대리자 : 다중메서드 호출
        // 최근에는 delegate를 사용하기 보다 delegate를 감싸는 Func, Action, Predicate ... 등을 사용
        #endregion

        #region [12] 이벤트 : Public Events
        public event EventHandler Click; // 이벤트
        #endregion

        #region [13] 이벤트 처리기 : Event Handlers
        public void onClick() // 이벤트 핸들러
        {
            if(Click != null)
            {
                Click();
            }
        }
        #endregion
    }

    class CarRepair
    {
        // [14] 다형성
        public CarRepair(IStandard car) => car.Run();
    }

    class Program
    {
        static void Main(string[] args)
        {
            // [A] 클래스, 생성자, 메서드 테스트
            Car campingCar = new Car("캠핑카");
            campingCar.Run();

            // [B] 속성 테스트
            Car sportCar = new Car();
            sportCar.Name = "스포츠카";
            sportCar.Run();

            // [C] 인덱서 테스트
            Car cars = new Car(2);
            cars[0] = "1번 자동차";
            cars[1] = "2번 자동차";
            for(int i = 0; i < cars.Length; i++)
            {
                WriteLine(cars[i]);
            }

            // [D] 이터레이터 테스트
            foreach(string name in cars)
            {
                WriteLine(name);
            }

            // [E] 대리자, 이벤트, 이벤트 처리기 테스트
            Car btn = new Car("전기자동차");
            btn.Click += new Car.EventHandler(btn.Run);
            btn.Click += new Car.EventHandler(btn.Run);
            btn.onClick();

            // [F] 다형성 테스트
            new CarRepair(campingCar);
            new CarRepair(sportCar);
        }
    }
}

profile
아직까지는 코린이!

0개의 댓글