-as , is 연산자

  1 private int checktype(Man mi)

  2         {
  3             if (mi is Stu && !(mi is StuTea)) return 1;            
  4             if (mi is StuTea) return 2;
  5             if (mi is Tea && !(mi is TeaStu)) return 3;
  6             if (mi is TeaStu) return 4;
  7 
  8             return -1;
  9         }



1 foreach (Stu st in lb)

  2             {
  3                 int num;
  4                 if ((num = lb.Compare(st, name)) == 1)
  5                 {
  6                     Stu stu = st;
  7                     return  stu as Man;
  8                 }
  9             }



-명시적, 묵시적 인터페이스 구현

  //명시적 인터페이스!

        void ITeach.Work()
        {
            Console.WriteLine("학생 선생입니다~ 공부하고 일하고 바빠~ 바쁨 IQ-2");
            int Iq = this.IQS;
            this.SettingIq(ref Iq, -2);
        }
        //묵시적 인터페이스!
        public void Teach()
        {
            Console.WriteLine("학생 선생입니다~ 가르칩니다~ IQ+2");            
        }

-static 클래스 구현

  public static class ShowWindow

    {      
        // 읽기전용 static 읽기전용
        static readonly ArrayList arr;
        static ShowWindow()
        {  
            arr = new ArrayList();
            SetShowwindow();
        }

        private static void SetShowwindow()
        {
           arr.Add(new Stu("조대준",42));            
           arr.Add(new Stu("대마왕", 142));
            
            arr.Add(new StuTea("조스타", 124));
            arr.Add(new StuTea("킹스타", 102));
           
            arr.Add(new Tea("최강사", 50));            
            arr.Add( new Tea("진짜강사", 84));
            
            arr.Add( new TeaStu("진중왕",45));            
            arr.Add(new TeaStu("왕중진", 55));          
            
        }

        public static string StaticToString()
        {
            bool type = false;
            foreach (object o in arr)
            {
                if (o is Stu && !(o is StuTea))
                {
                    if (!type)
                    {
                        Console.WriteLine("========= 학생 =========");
                    }
                    Console.WriteLine(o.ToString());
                    type = true;
                }
                if (o is StuTea)
                {
                    if (type)
                    {
                        Console.WriteLine("=========학생 선생 =========");
                    }
                    Console.WriteLine(o.ToString());
                    type = false;
                }

                if (o is Tea && !(o is TeaStu))
                {
                    if (!type)
                    {
                        Console.WriteLine("=========  선생 =========");
                    }
                    Console.WriteLine(o.ToString());
                    type = true;
                }

                if (o is TeaStu)
                {
                    if (type)
                    {
                        Console.WriteLine("========= 선생 학생=========");
                    }
                    Console.WriteLine(o.ToString());
                    type = false;
                }
            }
            return null;
        }

        public static Man CompareName(string _name)
        {            
            foreach (Man o in arr)
            {
                if (o.Name == _name)
                {
                    if (o is Stu && !(o is StuTea))
                    {
                        Stu stu = o as Stu;
                        return stu.Clone() as Man;   
                    }
                    if (o is StuTea)
                    {
                        StuTea stutea = o as StuTea;
                        return (StuTea)stutea.Clone() as Man;                        
                    }
                    if (o is Tea && !(o is TeaStu))
                    {
                        Tea tea = o as Tea;
                        return tea.Clone() as Man;
                    }
                    if(o is TeaStu)
                    {
                        TeaStu tea = o as TeaStu;
                        return (TeaStu)tea.Clone() as Man;
                    }
                    return null;
                }                
            }
            return null;
        }
    }

-static 생성자 구현

  //static 생성자!!

        static LecturRoom()
        {
            singleton = new LecturRoom();
        }
        private LecturRoom()
        {
            Console.WriteLine("강의실 생성");
            Console.ReadLine();
        }    

-abstract 클래스 구현

 //abstract 클래스!!!

    public abstract class Man: IComparable
    {
        string name;             

        readonly int hp;
        const int min_Hp = 0;
        const int max_Hp = 100;

        public Man(string _name)
        {
            name = _name;
            hp = max_Hp;
        }
        //비대칭 속성!!
        public string Name
        {
            get
            {
                return name;
            }
            private set
            {
                if (Avail(value))
                {
                    name = value;
                }
            }
        }

        private bool Avail(string value)
        {
            return (value != null);
        }


        public int CompareTo(object obj)
        {
            Man man = obj as Man;
            if(man !=null)
            {
                return this.Name.CompareTo(man.Name);
            }
            else
            {
                throw new Exception("안되요...");
            }
        }
        public override string ToString()
        {
            return "체력 : " + hp+"\n";
        }  

    }

    class StuHelper :IComparer<Man>
    {
        #region IComparer(Man) 멤버

        public int Compare(Man x, Man y)
        {
            return x.Name.CompareTo(y.Name);
        }

        #endregion
    }

-sealed 클래스 구현

 // sealed 클래스!!

    public sealed class TeaStu : Tea  ,IStudy,ICloneable
    {
        public TeaStu(string _name, int _charisma)
            : base(_name, _charisma)
        {
        }
        public TeaStu(string _name)
            : base(_name)
        {
        }
        public override string ToString()
        {
            return base.ToString();
        }
        public void Study()
        {
            Console.WriteLine(" 강사지만 열심히 공부한다~~~");
        }
        public override void Heal()
        {
            Console.WriteLine("선생이 배우고 가르치고 힘들다 쉬어야지~~");            
        }

        #region ICloneable 멤버

        public object Clone()
        {
            Console.WriteLine("이름을 입력하세요~");
            string _name = Console.ReadLine();
            return new TeaStu(_name, this.Charisma);
        }

        #endregion
    }

-ToString 재정의 구현

  //ToString 재정의!

        public override string ToString()
        {
            return "이름 : " + this.Name + " 카리스마 : " + charisma + "\n" + base.ToString();
        }

-읽기전용, static 읽기전용 구현

  public static class ShowWindow

    {      
        // 읽기전용 static 읽기전용
        static readonly ArrayList arr;
        static ShowWindow()
        {  
            arr = new ArrayList();
            SetShowwindow();
        }
    }

-const 멤버 구현 (Man 속성)

  const int min_Hp = 0;

const int max_Hp = 100;


-비대칭 속성 구현 (Man 속성)

  //비대칭 속성!!

        public string Name
        {
            get
            {
                return name;
            }
            private set
            {
                if (Avail(value))
                {
                    name = value;
                }
            }
        }

-base 키워드 구현
-IComparer,IComparable 구현 (Stu 클래스 내 구현, Helper클래스 사용)

  public int CompareTo(object obj)

        {
            Man man = obj as Man;
            if(man !=null)
            {
                return this.Name.CompareTo(man.Name);
            }
            else
            {
                throw new Exception("안되요...");
            }
        }
       

    }

    class StuHelper :IComparer<Man>
    {
        #region IComparer(Man) 멤버

        public int Compare(Man x, Man y)
        {
            return x.Name.CompareTo(y.Name);
        }

        #endregion
    }

-IEnumerable,IEnumerator 구현(LectureRoom)

  #region IEnumerator 멤버

        object IEnumerator.Current
        {
            get
            {
                if (iteach != null)
                {
                    return iteach;
                }
                return istudies[index];
            }
        }

        bool IEnumerator.MoveNext()
        {
            index++;
            if (index < istudies.Count)
            {
             
                return true;
            }
            Reset();
            return false;

        }

        public void Reset()
        {
            index = -1;
        }

        #endregion

        #region IEnumerable 멤버

        public IEnumerator GetEnumerator()
        {
            return (IEnumerator)this;
        }

        #endregion

-ICloneable 구현

  #region ICloneable 멤버

        public object Clone()
        {
            Console.WriteLine("이름을 입력하세요~");
            string _name = Console.ReadLine();
            return new TeaStu(_name, this.Charisma);
        }

        #endregion

-인덱서 구현

 //인덱서

        public Tea this[string index] 
        {
            get
            {
                foreach (Tea t in stulist)
                {
                    if (t.Name == index)
                    {
                        return t;
                    }
                }
                return null;
            }            
        }

-out,ref 구현

  public double CheckAverage(ref double number)

        {
            int i=0;            
            double sum = 0;
            foreach (Stu st in libarr)
            {
                sum += number - st.IQS;                
            }
            double gap = sum / libarr.Count;            
            number = number - gap;            
            return number;          
        }


'Programing > C#&.Net' 카테고리의 다른 글

어셈블리,메타데이터  (0) 2016.11.30
형식 메타데이터  (0) 2016.11.30
Delegate(대리자)  (0) 2016.11.30
dll만들기 명령 프롬프트 사용  (0) 2016.11.30
c# 기초  (0) 2016.11.30

+ Recent posts