'사용자 지정 타입'에 해당되는 글 1건

  1. 2019.10.31 10.31 제너릭을 이용해 사용자 지정 타입 활용하기 (where문 활용)

10.31 제너릭을 이용해 사용자 지정 타입 활용하기 (where문 활용)

C#/실습 2019. 10. 31. 13:51

예제:

코드:

 

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _10._31_step1
{
    
    class App
    {
        public App()
        {
            Inventory<Item> inventory = new Inventory<Item>(10);
            inventory.AddItem(new Item(10"장검"));
            inventory.AddItem(new Item(11"단검"));
            inventory.AddItem(new Item(12"활"));
            Console.WriteLine("초기 배열 값:");
            inventory.Print();
            inventory.RemoveItem("단검");
            Console.WriteLine("단검 지운 값:");
            inventory.Print();
            Console.WriteLine("활 찾기");
            Console.WriteLine(inventory.FindItem("활").Id);
 
            
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _10._31_step1
{
    class Inventory <T> where T: Item //Item 클래스나 Item 파생 클래스만 들어올 수 있다.
    {
        private T[] items;
        public T this[int i]
        {
            get
            {
                return this.items[i];
            }
        }
        public void AddItem(T item)
        {
            for (int i=0; i<items.Length; i++)
            {
                if(items[i] == null)
                {
                    items[i] = item;
                    break;
                }
            }
        }
        public void RemoveItem(string name)
        {
            for(int i =0; i<items.Length; i++)
            {
                if(items[i] is Item)
                {
                    if((items[i] as Item).Name == name)
                    {
                        items[i] = default;
                    }
                }                
            }
        }
        public T FindItem(string name)
        {
            for (int i = 0; i < items.Length; i++)
            {
                if (items[i] is Item)
                {
                    if (((Item)items[i]).Name == name)
                    {
                        return items[i];
                    }
                    continue;
                }
                continue;
            }
            return default;
        }
        public void Print()
        {
            foreach(var data in items)
            {
                if(data != null)
                Console.WriteLine("ID: {0}  Name: {1}",data.Id, data.Name);
            }
        }
 
        public Inventory(int capacity)
        {
            this.items = new T[capacity];
        }
        
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _10._31_step1
{
    class Item
    {
        public int Id { get; private set; }
        public string Name { get; private set; }
        public Item(int id, string name)
        {
            this.Id = id;
            this.Name = name;
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

: