Notice
Recent Posts
Recent Comments
Link
«   2025/09   »
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
Archives
Today
Total
관리 메뉴

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

2020.06.03_수업일지 본문

카테고리 없음

2020.06.03_수업일지

ㅈㅏ윤이 2020. 6. 3. 18:25

< 다형성 > 

메서드 오버라이드 : void test ( int A )

                          void test ( int A )

-> 내용만 다르고 다 똑같이 ( 인자 메서드 ) 

메서드 오버로드 : void test ( void )

                        void test ( int A )

                        void test ( string A )

-> 메서드만 같고 인자는 다르게 

 

** virtual을 부모의 것에 적었을 시 ( 객체 타입에 맞혀서 메서드 호출된다 ) 

 override 적었을 시 -> 자신의 것을 호출한다 / 부모의 move를 대신하기 위해서 넣는 것 -> 똑같은 메서드여야 함

 new 적었을 시/ 아무것도 안 적었을 시   -> 부모의 것과 동일한 값이나 곂칠려는 것이 아닌 다른 것을 만들려고 함(오류)

의도하지 않게 내가 만든 메서드가 부모와 정말 동일한 메서드를 만들 수 있다 -> 그때 overrride를 이용하여 자식을 위주로 컴파일해주게 된다 / 변수 이름 다른 것으로 사용 불가능하다 

=> 부모에 있는 메서드는 new 타입 ( 오브젝트 상속 ) 

new는 4개짜리 배열을 만들어준다

배열을 만들어서 하기 1단계
배열을 쉽게 만들기 2단계
aMammal에서 Temp에 넣은 것 3단계

=> virtual 이 없으면 다 똑같은 대답이 나오기 때문에 virtual을 써서 객체를 다 따로 구분할 수 있다( 부모 클래스 단계에 명시한다 ) 

 

< object 기본 메서드 확장 >

: Tostring의 경우 클래스의 인스턴스 값을 적절하게 표현하는 내용으로 재정의하는 것이 보통 -> 

주석 처리시 이렇게 나온다

aCar를 호출시키면 aCar에 있는 ToString을 호출한다  / ( \n = enter와 같은 기능 )

더보기
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20200603_004
{
    class Car
    {
        int iSpeed;     // 속도
        string sColor;     // 색상
        string sName;      // 이름
        string sVender;    // 제조사

        public override string ToString()
        {
            return "속  도 : " + iSpeed 
                + "\n색  상 : " + sColor
                + "\n이  름 : " + sName 
                + "\n제조사 : " + sVender;
        }
        public void Print()
        {
            Console.WriteLine(ToString());
            //Console.WriteLine("속  도 : 100\n색  상 : 빨캉\n이  름 : 그냥죠\n제조사 : 현다이");
        }
        
        public Car(int iSpeed, string sColor, string sName, string sVender)
        {
            this.iSpeed = iSpeed;
            this.sColor = sColor;
            this.sName = sName;
            this.sVender = sVender;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Car aCar = new Car(100, "빨캉", "그냥죠", "현다이");
            aCar.Print();
            Console.WriteLine(aCar);
        }
    }
}

 

< 오버로드 >

: 시그니처 중에서 "반환 값"은 무시하고 "이름"만 같은 메서드가 "매개변수의 수", "개별 매개변수 타입"만 다르게 재정의되는 경우 

메서드 시그니처 : 어떤 메서드를 고유하게 규정할 수 있는 정보를 의미 

1) 메서도 오버로드 

메서드 오버로드를 이용하여 일관성있는 클래스 작성
Abd라는 메서드 하나로 기억하는 편이 타입별로 나눠진 메서드를 기억하는 것보다 쉽다

=> 다양한 타입의 값을 받을 수 있다

2) 연산자 오버로드 ( 자바 X )

: 정수형 타입과 문자열 타입에 대해 각각 더하기 연산을 수행하는데, 타입에 따라 연산자의 역할이 달라진다

( ex : 정수형 타입에서는 정수 연사에 걸맞게 숫자 값을 더하는 반면 문자열 타입에서는 말 글대로 순수하게 문자열을 이어 붙이는 역할 )

operator (예약어/ 참조 변수) + (연산자 기호) 

=> 반환 값 이름 (메개)   : 기본값
     int operator + (int A , int B)
     {
               return A+B;
      }

ex) int operate + (int N1, int N2)

    {

      return N1 + N2

    }

** 예제를 이용해 연산자 오버로드 만들기

 class Market
    {
        public int iApple;
        public int iCherry;

        public static Market operator +(Market obj1, Market obj2)
        {
            Market Temp = new Market();
            Console.WriteLine("operator + 호출됨");
            return Temp;
        }
    }
        class Program
        {

            static void Main(string[] args)
            {
                Market 가게1 = new Market();
                Market 가게2 = new Market();


                가게1.iApple = 10;
                가게1.iCherry = 10;

                가게2.iApple = 100;
                가게2.iCherry = 100;

                Market 가게3 = new Market();
                // 가게3 = 가게1 + 가게2;
                가게3.iApple = 가게2.iApple + 가게1.iApple;
                가게3.iCherry = 가게2.iCherry + 가게1.iCherry;

                Console.WriteLine("가게3.iApple : "+ 가게3.iApple);
                Console.WriteLine("가게3.iCherry : "+ 가게3.iCherry);

                가게3 = 가게1 + 가게2;
            }
        }

연산자는 혼자서 메서드로 사용못하기 때문에 operate 연산자를 메소드로 사용한다.

Console.WriteLine("operator + 호출됨"); -> 호출의 여부를 알아보기 위해서 호출

가게 1 + 가게 2의 값이 Market이 되어 Market 타입이 되어서 나와야 한다

 class Market
    {
        public int iApple;
        public int iCherry;

        public override string ToString()
        {
            return "{iApple=" + iApple + ", iCherry=" + iCherry + "}";
        }

        public static Market operator +(Market Obj1, Market Obj2)
        {
            Console.WriteLine("1 ===========================");
            Market Obj3 = new Market();

            Console.WriteLine("Obj1" + Obj1);
            Console.WriteLine("Obj2" + Obj2);
            Console.WriteLine("2 ===========================");
            return Obj3;
        }
    }
        class Program
        {
            static void Main(string[] args)
            {
                Market 가게1 = new Market();
                Market 가게2 = new Market();

                가게1.iApple = 10;
                가게1.iCherry = 10;

                가게2.iApple = 100;
                가게2.iCherry = 100;

                Market 가게3 = new Market();

                가게3.iApple = 가게2.iApple + 가게1.iApple;
                가게3.iCherry = 가게2.iCherry + 가게1.iCherry;

                Console.WriteLine("가게3 : " + 가게3);

                int a = 3 + 4;

                가게3 = 가게1 + 가게2;

            }
        }

 

operator를 통해서 Market Obj1 -> 가게 1  Market Obj2 -> 가게 2 가 된다

Market Obj3 = new Market(); 은 컴파일을 위해서 넣어준다 

static 붙이는 이유는 객체가 존재하든 안 하든 덧셈(연산자)이 실행되도록 해야 하기 때문에 붙인다

 

** 실제 숫자 이용해서 연산자 오버로드 만들기

 class Complex
    {
        int Real;
        int Image;

        public override string ToString()
        {
            // { 3 + 4i }
            return "{" + Real + " + " + Image + "i}";
        }
    }
    class Program
    { 
        static void Main(string[] args)
        {
            Complex Num1 = new Complex();
            Console.WriteLine(Num1);
        }
    }

** "toString" 메서드 : 객체가 가지고 있는 정보나 값들을 문자열로 만들어 리턴하는 메서드 

 class Complex
    {
        int Real;
        int Image;

        public Complex(int Real, int Image)
        {
            this.Real = Real;
            this.Image = Image;

        }
        public override string ToString()
        {
            // { 3 + 4i }
            return "{" + Real + " + " + Image + "i}";
        }
        // int operator + (int A ,int B)  { return A+B; }
        public static Complex operator +(Complex A, Complex B)
        {
            Complex C = new Complex(0, 0);
            C.Real = A.Real + B.Real;
            C.Image = A.Image + B.Image;

            return C;
        }

    }
    class Program
    { 
        static void Main(string[] args)
        {
            Complex Num1 = new Complex(3, 4);
            Complex Num2 = new Complex(5, 6);
            Complex Num3 = Num1 + Num2;
            Console.WriteLine(Num1);
            Console.WriteLine(Num2);
            Console.WriteLine(Num3);
        }
    }

기존에 A, B가 있었고 새로운 C를 만들기 위해서 Complex에 Real. Image에 0,0을 넣어준다