Blog Content

    티스토리 뷰

    54.C# - 클래스 복습

    반응형

    Program.cs
    using System;
    using Hyundai;

    public class 클래스복습
    {
        public static void Main()
        {
            Car car = new Car("현대");

            car.Length = 2; //2대
            car[0] = "에쿠스";
            car[1] = "제니시스";

            car.Show(); //에쿠스,제니시스

            //예3
            CarHandler ch = new CarHandler(car.Show);
            ch();

            //예4
            car.Go += car.Show;
            car.OnGo();
        }
    }

    Car.cs
     //네임스페이스 추가
    namespace Hyundai
    {
        using System;
     
        //Class
        public class Car
        {
            private string name; //Field

            public Car() //Constructor {
                //Empty
            }

            public Car(string name) { this.name = name; }

            //Property
            private int _Length;

            public int Length
            {
                get { return _Length; }
                set { _Length = value;
                    names = new string[value]; }
            }

            //Indexed
            private string[] names;

            public string this[int index]
            {
                get { return names[index]; }
                set { names[index] = value; }
            }
            public void Show()
            {
                //string msg = string.Format()
                Console.WriteLine("{0}", name);
                foreach (string s in names) {
                    Console.WriteLine("{0}", s);
                }
            }
            //이벤트 예4
            ~Car() {
                names = null; }

            //예4 Event
            public event CarHandler Go;
            public void OnGo()
            {
                if (Go != null) { Go(); }
            }
        }
        //Delegate 예3
        public delegate void CarHandler();
    }

    결과화면


    54.클래스복습.zip
    다운로드

    반응형

    Comments