프로그램/C# - Study / / 2010. 9. 16. 09:59

7D, 8D -

반응형


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ex15OOP6
{
    class Program
    {
        static void Main(string[] args)
        {
            //1. ToString()
            Item item = new Item();
            item.Number = 1;
            item.Description = "Test Item";
            Console.WriteLine(item.ToString());

            //2.
            Item item2 = new Item();
            item2.Number = 1;
            item2.Description = "Test Item2";

            //if(item.Equals(item2))
            //if(item == item2) // Item.operator==( item, item2)
            if (item == item2)
            {                 // Item.operator!=
                Console.WriteLine("Equals");
            }
            else
            {
                Console.WriteLine("Different");
            }

            if (object.ReferenceEquals(item, item2))
            {
                Console.WriteLine("Reference Equals");
            }
            else
            {
                Console.WriteLine("Reference Different");
            }

            Console.WriteLine("{0}/{1}", item.GetHashCode(), item2.GetHashCode());
        }
    }
}



****************************************************************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ex15OOP6
{
    class Item : object  //  : object가 생략되어 있다.
    {
        private int number;
        public int Number
        {
            get { return number; }
            set { number = value; }
        }

        private string description;
        public string Description
        {
            get { return description; }
            set { description = value; }
        }
        public override string ToString()
        {
            return string.Format("[{0}] [{1}]", number, description);
        }
        public override bool Equals(object obj)
        {
            Item item = (Item)obj;
            if (this.number == item.number &&
                this.description.Equals(item.Description))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static bool operator ==(Item item1, Item item2)
        {
            return item1.Equals(item2);
        }
        public static bool operator !=(Item item1, Item item2)
        {
            return !(item1.Equals(item2));
        }

        public override int GetHashCode()
        {
            return number;
        }
    }
}

************************************************************************************************

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ex15OOP6
{
    class ContainerClass
    {
        public  class InnerClass
        {
        }
    }
    class TestClass
    {
        public void TesetMethod()
        {
            ContainerClass.InnerClass inner =
                new ContainerClass.InnerClass();
        }
    }

}
//  Class 앞에 sealed 라는 키워드를 시용하면 상속 불가능 클래스가 된다.
// class 내부 class 이용 가능 (내부 클래스)

반응형

'프로그램 > C# - Study' 카테고리의 다른 글

9D - Ex16OOP7 - Interface  (0) 2010.09.16
수업자료  (0) 2010.09.16
7D - 다형성, 상속  (0) 2010.09.14
7D - 상속, protected  (0) 2010.09.14
7D - readonly, const  (0) 2010.09.14
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유