Notice
Recent Posts
Recent Comments
Link
«   2025/10   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

つ⋅⌄⋅)づ⌁⌁도움⌁⌁⋆

2020.06.04_수업일지 본문

카테고리 없음

2020.06.04_수업일지

ㅈㅏ윤이 2020. 6. 4. 18:17

** 오늘의 꿀팁 ~!

< 클래스 간의 형 변환 >

 public class Currency
    {
        decimal money;
        public decimal Money { get { return money; } } -> get 프로퍼티

        public Currency(decimal money)
        {
            this.money = money;
        }
    }
    public class Won : Currency    // Currency를 상속받는다 
    {
        public Won(decimal money) : base(money) { }

        public override string ToString()
        {
            return Money + "Won";
        }
    }
    public class Dollar : Currency  // Currency를 상속받는다 
    {
        public Dollar(decimal money) : base(money) { }

        public override string ToString()
        {
            return Money + "Dollar";
        }
        static public implicit operator Won(Dollar dollar)
        {
            return new Won(dollar.Money * 1000m);
        }
    }
    public class Yen : Currency  // Currency를 상속받는다 
    {
        public Yen(decimal money) : base(money) { }

        public override string ToString()
        {
            return Money + "Yen";
        }
       static  public implicit operator Won (Yen yen) =1. 오류가 나지않게 형변환해야한다
        {
            return new Won(yen.Money * 13m);
        }

    }
 class Program
    {

        static void Main(string[] args)
        {
            Won won = new Won(1000);
            Dollar dollar = new Dollar(1);
            Yen yen = new Yen(13);
            Won won1 = yen;
            Won won2 = (Won)yen;

            won = yen;
            Console.WriteLine(won1);

        }
    }

-> 결과값 !  Dollar 도 똑같이 한다 

 

 

- implicit 암시적 형 변환 -> iplicit operator는 암시적 형 변환을 할 수 있고, 암 시저긴 형 변환이 가능하다

                                    명시적으로 캐스팅 연산자를 쓰는 것도 혀용된다

- explicit 명시적 형 변환

 

< 추상 클래스 >

: abstract 코드 없는 메서드 / 설계 가이드 역할

1) new를 사용해 인스턴스로 만들 수 없다 -> 객체를 만들 수 없다

2) 추산 메서 들러 가질 수 있다

-> 반드시 상속받아서 사용해야 한다 상속받은 애는 추산 클래스 안에 있는 추상 메서드를 다 구현해줘야 한다 ( 똑같은 이름으로 ) -> 오버 라이딩 필요

구현할 때 overrinde 적어줘야 한다

class Point
    {
        int x, y;

        public Point(int x, int y)
        {
            this.x = x; this.y = y;
        }

        public override string ToString()
        {
            return "X: " + x + ", Y: " + y;
        }
    }
    abstract class DrawingObject
    {
        public abstract void Draw();

        public void Move() { Console.WriteLine("Move"); }
    }
    class Line : DrawingObject
    {
        Point pt1, pt2;
        public Line(Point pt1, Point pt2)
        {
            this.pt1 = pt1;
            this.pt2 = pt2;
        }

        public override void Draw()
        {
            Console.WriteLine("Line" + pt1.ToString() + " ~ " + pt2.ToString());

        }
    }
    class Program
    { 
        static void Main(string[] args)
        {
        }
    }

< 델리게이트 >

- 새로운 자료형을 만드는 것 -> 클래스와 비슷함

- 메서드를 가리키는 변수를 델리게이트라고 한다

: 접근 제한자 delegate 대상_메서드의_반환 타입 식별자(대상_메서드의_매개변수_목록);

delegate int CalcDelegate (int x, int y) ;   // CalcDelegate가 Type이 된다

 

Delegate 타입의 정의 방법

시그니처가 일치하는 애만 구현 가능하다

-> Car2를 구현시키려면 새로운 delegate를 만들어준다

class Program
    {
        delegate void CarDeal();
        delegate void CarDeal2(int z);

        static void Car()
        {
            Console.WriteLine("메뚜기");
        }
        static void Car2(int A)
        {
            Console.WriteLine("메뚜기2 " + A);
        }
        static void Ottogi()
        {
            Console.WriteLine("오뚜기");
        }
        static void Main(string[] args)
        {
            Car();

            CarDeal Dealer;
            Dealer = Car;
            Dealer();
            Dealer = Ottogi;
            Dealer();

            CarDeal2 Dealer2;
            Dealer2 = Car2;
            Dealer2(100);


        }
    }

int 이름 (int , int)  / Methods -델리게이트 배열 객체를 가리키는 참조 변수

                          델리게이트 배열 CalDelegate ( Add, Sub, Mul, Div)

 class Program
    {
        delegate void CalcDelegate(int x, int y);

        static void Add(int x, int y) { Console.WriteLine(x + y); }
        static void Sub(int x, int y) { Console.WriteLine(x - y); }
        static void Mul(int x, int y) { Console.WriteLine(x * y); }
        static void Div(int x, int y) { Console.WriteLine(x / y); }
        static void Main(string[] args)
        {
            CalcDelegate calc = Add;
            calc += Sub;
            calc += Mul;
            calc += Div;

            calc(20, 5);

        }
    }

-> 2개의 정수에 대해 단 한 번의 함수 호출로 사칙 연산 메서드가 모두 호출 가능한 예제

calc += 는 목록(메서드를 델리게이트 인스턴스)에 추가 > 넣은 순서대로 나온다  -> 하나하나 호출할 필요 없이 간단하게 적어서 호출 가능하다.

calc -=는 목록에서 삭제 

 < Test >
 
 class Person
    {
        protected string sName;
        protected int sAge;
        protected string sDept;

        public override string ToString()
        {
            return "성명 : " + sName + "\n나이 : " + sAge + "\n부서 : " + sDept ;
        }
        public virtual void 끝()
        {
            Console.WriteLine("회사원입니다.");
        }
    }
    class Developer : Person
    {
        public Developer(string sName, int sAge, string sDept)
        {
            this.sName = sName;
            this.sAge = sAge;
            this.sDept = sDept;
        }
        public override void 끝()
        {
            Console.WriteLine("개발자입니다.");
        }
    }
    class Sales : Person
    {
        public Sales(string sName, int sAge, string sDept)
        {
            this.sName = sName;
            this.sAge = sAge;
            this.sDept = sDept;
        }
        public override void 끝()
        {
            Console.WriteLine("세일즈맨입니다.");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("과정명 : 스마트팩토리 과정");
            Console.WriteLine("교과목 : C# OOP/자료구조");
            Console.WriteLine("응시일 : 2020.06.04.");
            Console.WriteLine("응시자 : 박지윤\n");

            Person[] man = new Person[2];
            man[0] = new Developer("홍길동", 27 ,"연구소");
            man[1] = new Sales("고길동", 25, "판매");
           
            foreach (Person self in man)
            {
                Console.WriteLine(self);
                self.끝();
                Console.WriteLine("========================");
            }
        }
    }

< 콜백 메서드 >

: 사용자가 만든 소스 타입에서 타깃 타입 내에 정의된 메서드를 호출할 때 호출자는 소스가 되고 피호출자는 타깃이 된다

-> 피호출자에서 호출자의 메서드를 호출 하는 것을 의미하고 이때 역으로 호출된 호출자 측의 메서드 = 콜백 메서드 

특정한 상황에 따라서 자동으로 호출되는 것을 말한다.