커뮤니티
WSC Info.
제목:    Method 및 class 요약
  3256   김윤중

summary

  • 메쏘드 매개변수
    • 값, 참조,, 출력용,가변길이,선택적,명명된,메소드오버로딩
  • 클래스
    • 정적필드,(동적)필드,메쏘드,프로퍼티,인덱서,생성자,소멸자
    • this,this생성자, base,base생성자
    • 얕은복사,깊은복사,
    • 다형성(virtual-override)
    • 중첩클래스, 분할클래스,
    • 메쏘드의 확장
    • 무명형식
    • 구조체

Method

  • Method 형식 :
    한정자 반환형식 메쏘드이름(매개변수) { … }
  • 매개변수
    • Call-by-Value,    
      • public int Add(int a, int b){.. }
      • int c =Add(1,2);
    • Call-by-Reference
      • public void Swap(ref int a, ref int b){..}
      • int a=1,b=2;  
        swap(ref a, ref b);
    • 출력용매개변수
      • public void Divide(int a, int b, out int quotient, out int reminder){..}
      • int c;
        Devide(1,2,out c);
    • Method Overloading
      • int Plus(int a, int b);
        int Plus(double a, double b);
    • 가변길이 매개변수
      • int Sum(params int[] array);
        { int s=0; foreach(int n in params) s+n; return s;}
      • Sum(1,2) ; Sum(1,2,30,40);
    • 명명된 매개변수
      • void Print(string name, string phone);
      • print(phone:”010-1234-5678”, name:”박찬호”);
    • 선택적 매개변수
      • void AddPrint(int a, int b=0);
      • AddPrint(3,0); AddPrint(3);

Class

  • 크래스의 구조
    • public class Cat {     //클래스정의 한정자 class 클래스 이름
            public string name="사랑";   //필드 한정자 형이름 데이터이름{=초기값};
           public static int count;          //정적 필드
           public Cat( )                      //생성자
               {   count++; name="나비"}
            public Cat(string name="나비")       //생성자 클래스이름(선택적 파라메터){ } 
                { this.name=name; coun++; }
            public void Meow()            //메소드 한정자 반환형 메소드이름(파라메터) { …}
                {     Console.WriteLine(“{0}:야옹”,Name);   }
           public striong Name           // property
              { get; set; }
           public static int Count() {      //정적 메소드
                return count; }
           ~Cat() { }                         // 소멸자 ~클래스이름(){ }
      }
    • Cat cat=new Cat("야옹");  string name=cat.Name;  cat.Name="메롬"; int n=Cat.Count();
  • 객체복사
    • 얕은 복사(Shallow Copy), 참조형식
      • Cat cata=new Cat();
      • Cat catb=cata;  //얕은 복사 참조주소
    • 깊은복사(DeepCopy)
      • Cat catc=cat.Clone();  //깊은복사
      • System.IClonable 인터페이스
      • class Cat : IClonable{
             pubic Clone() {  }
             ... }
      • Cat catc=cat.Clone();
  • this, this()
    • this        //자신의 객체,
    • this();     //자신의 생성자
      public MyClass(int a) { this.a=a; }
      public MyClass(int a,int b) : this(b) { this.b=b; }
  • 접근한정자
    • public, protected, private, internal, protected internal,sealed
  • 상속, base, base()
    • base      //부모의 객체,  
    • base()    //부모클래스의 생성자
    • class Mammal {
        protected string Name;
         public Mammal(string Name) { this.Name=Name; }
      }
      class Cat : Mammal {
         protected string Color;
         public Cat(string Name, string Color) : base(Name)
         { this.Color=Color;  Console.WriteLine(base.Name/**Name**/);}
      }
  • 상속 객체간 할당
    • Mammal mammal=new Mammal();
      Dog dog=new Dog();
      mammal=dog;
      dog=(Dog)mammal
      ;
  • 객체 비교 및 변환 is,as
    • If(mammal is Dog )    // (manal.GetType() == typeof(Dog) ? true : false)
      { dog=(Dog)mammal; }
    • Cat cat=mammal as Cat;  //(manal.GetType() == typeof(Dog) ? (Cat)cat : null)
      if(cat!=null) cat.Meow();
  • 다형성,  virtual, override, sealed, new
    • class Mammal { 
           public virtual void Say(){   Console.WriteLine("말하기"); }  }
      class Dog : Mammal{
          public override void Say() {Console.WriteLine("멍멍");} }
      class Cat : Mammal{
          public override void Say(){ Console.WriteLine("야옹"); } }
      class Kitten : Cat{
          public sealed override void Say(){Console.WriteLine("양");} }
      class Kitten : Cat{
      public  new  void Say(){Console.WriteLine("양");} }
  • 중첩클래스
    • Class OuterClass {
        int a=0;
        class InnerClass { int b=0; …}
      }
  • 분할클래스  partial
    • partial class MyClass { … }
      partial class MyClass { … }
  • 무명형식 (Anoymous Class)
    •    var p = new {Name="홍길동",Age=17);
         Console.WriteLine(p.Name+"/"+p.Age);
    •   string Name = "홍길동";
         int Age = 17;
          var p = new {Name,Age }; //프로퍼티 타입 유추
         Console.WriteLine(p.Name+"/"+p.Age);
  • 확장메소드
    • namespace TypeExtension {
       public static class IntegerExtension {
          public static int Power(this int myInt, int exponent) {
               int result = myInt;
              for (int i = 1; i < exponent; i++) result = result * myInt;
              return result;  
          }
          public static double Power(this double val, int Exponent)
              {
                  double r = val;
                  for (int i = 1; i < Exponent; i++) r = r * val;
                  return r;
              }
      }
    • using TyepeExtension;
         int a=2; int b=a.Power(3) ; b=2.Power(3);//8
         double d=3; double dd=3.1.Power(3); //
  • struct 구조체 값형식, 항상 DeepCopy
    • struct MyStruct
         { public int d=5; }
    • MyStruct a=new MyStruct();  // a(5)
      MyStruct b=a;                         //a(5) b(5)
      b.d=6;                                      //a(5) b(6)