C#/복습

일반화(제네릭) <T>

Game Client Lee Hwanguk 2023. 1. 21. 17:20
using System;
using System.Collections.Generic; //List<T>,Queue<T>,Stack<T>,Dictionary<T>
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections; //컬랙션이 속해있는 네임스페이스.

namespace ConsoleApp1
{
    internal class App
    {
        
        //생성자
        public App()
        {
            //일반화 ,<T>, 제네릭 메서드에 이용
            int[] arr0 = { 1, 2, 3 };
            int[] arr1 = new int[3];
            CopyArray(arr0, arr1);

            string[] arr2 = { "홍길동", "임꺽정", "장길산" };
            string[] arr3 = new string[3];
            CopyArray(arr2, arr3);

            Hero[] arr4 = { new Hero(), new Hero(), new Hero(), };
            Hero[]arr5 = new Hero[3];
            CopyArray(arr4, arr5);

            //일반화 메서드 
            CopyArray<int>(arr0, arr1);
            CopyArray<string>(arr2, arr3);
            CopyArray<Hero>(arr4, arr5);

            PrintArray<int>(arr1);
            PrintArray<string>(arr3);
            PrintArray<Hero>(arr5);
            //------------------------------------------------------------------
            //일반화 클래스를 이용하여 Inventory에 여러타입(클래스)를 AddItem 해보자
            Inventory<Weapon> inventory0 = new Inventory<Weapon>(10); //Weapon 클래스를 담을 inventory0을 생성(capacity는 10)
            Inventory<Armor> inventory1 = new Inventory<Armor>(8); //Weapon 클래스를 담을 inventory0을 생성(capacity는 10)

            inventory0.AddItem(new Weapon("장검")); 
            inventory1.AddItem(new Armor("투구"));

        }
        //메서드 오버로딩(과적)->동일한 이름의 메서드를 다른기능(컨디션)으로 쓸수있다
        void CopyArray<T>(T[] a, T[]b) //<Type> , Type[]a, Type[]b
        {
            for (int i=0; i<a.Length; i++)
            {
                b[i] = a[i]; //arr0 의 요소들을 arr1에 할당. 
            }
        }
        void CopyArray(Hero[] a, Hero[] b) 
        {
            for (int i = 0; i < a.Length; i++)
            {
                b[i] = a[i]; //arr0 의 요소들을 arr1에 할당. 
            }
        }

        void PrintArray<T>(T[]a)
        {
            foreach(Object element in a)
            {
                Console.WriteLine(element);
            }
        }




    }

}

#CopyArray를 통해  arr0을 arr1에 , arr2를 arr3에 할당

#arro 은 int, arr1은 string 타입으로 같은 메서드를 여러번 사용하려면 일반화가 필요했다

#void CopyArray<T>(T[]a,T[]b) 로 메서드 생성 <Type>, type[]a, type[]b 

#T에는 여러가지 type이 들어올수있음

 

 

#클래스의 일반화

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

namespace ConsoleApp1
{
    internal class Inventory<T> //1.여러 타입(클래스)가 들어올 Inventory Class
    {
        public T[] equipments; //2.
        private int index = 0;
        public Inventory(int capacity) 
        {
            //일반화(제너릭)을 이용하여 여러 클래스(타입)의 요소들을 담아보자

            this.equipments=new T[capacity]; 
        }
        public void AddItem(T item)
        {
            this.equipments[this.index++] = item; //요소를 추가하고 index 1증가(리스트는 자동이지만 배열은 계속 같은자리에 덮어씌워짐)
            PrintItem();
        }

        public void PrintItem()
        {
            //foreach(Object item in this.equipments) 
            //{
            //    Console.WriteLine(item); //null값까지 모두 출력됨 
            //}

            for(int i=0; i<equipments.Length; i++) //null 체크
            {
                if (equipments[i] != null)
                {
                    Console.WriteLine(equipments[i]);
                }
                else
                {
                    Console.WriteLine("[    ]");
                }
            }
        }
    }
}

#메서드와 마찬가지로 클래스에서도 일반화가 가능하다

#Inventory 클래스를 생성하고 Weapon 타입과 Armor 타입을 담아보자

#Class Inventory<T> 생성

#여러 타입이 들어올 T[]equipments 생성 (List도 가능)

#PrintItem메서드에서는 null체크와 동시에 출력해보자