오늘은 저장 예제를 만들어 이해가 전혀안가는 구간 복습을 했다.
어제 분명 복습하다가 저장에서 뇌정지가 왔기 때문에, 오늘 강사님께 질문한 결과
내생각이 마치 0.5평도안되는 1칸짜리 배열로 정의된 정적인 방에 갇혀있었다는 생각이 들었다.
왜 json파일을 직렬화 하는데 더러운 배열형태로 꼭 만들어서 저장을 해야한다고 생각했을까?
강사님의 말씀 중 Key와 Value값만 있으면 그타입이 무엇이든지 저장된다는 말에 머릿속이 바로 클리어됐다.
완벽하게 이해를 했다...사실 질문하다가 제가 아주 잘못 생각하고 있었다는걸 깨달았습니다ㅎㅎㅎ
그리고 이번일로 항상 문제를 겪을때마다, 그 한 곳에 집중해 크게 못보는 버릇이 있다는 사실도 깨달았다...
머릿속에 한참 머물고 있었던게 시원하게 뻥뚫렸기 때문에 오늘 복습은 꼭 데이터매니져를 응용해서 해보고 싶었다.
코드를 열심히 치고 저장된 json을 보며 아니나 다를까, 정말 완벽히 이해가 됐다고 생각한다.
희노애락중 노*10000000000000000000000000000000000 와같던 데이터매니져여...안녕ㅋ
.테이블만들기 귀찮아서 그냥 값넣음.
-----------------------------------------------------------------------------------------------------------------------------------
App Class
-----------------------------------------------------------------------------------------------------------------------------------
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Newtonsoft.Json;
namespace Repeat_017
{
class App
{
public App()
{
DataManager dataManager = new DataManager();
//File.WriteAllText("./data_manager.json", JsonConvert.SerializeObject(dataManager)); //데이터매니져로 묶어서 저장
//Console.WriteLine("저장완료");
dataManager = dataManager.LoadData();
dataManager.instance.PrintAll();
dataManager.dictionary.PrintAll();
}
}
}
|
-----------------------------------------------------------------------------------------------------------------------------------
DataManager Class
-----------------------------------------------------------------------------------------------------------------------------------
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Newtonsoft.Json;
namespace Repeat_017
{
class DataManager
{
public Instance instance;
public Array array;
public Dictionary dictionary;
public List list;
public DataManager()
{
this.Init();
this.dictionary.InputData(this.array.arrCreatureTypeId1, this.array.arrCreatureTypeId2, this.array.arrLandCreatureNames, this.array.arrMarineCreatureNames);
this.list.Input(this.array.arrCreatureTypeId1, this.array.arrLandCreatureNames, this.array.arrMarineCreatureNames);
Console.WriteLine("--------------------------------");
Console.WriteLine("데이터매니져 로드 완료");
Console.WriteLine("--------------------------------");
}
public void Init()
{
this.instance = new Instance();
this.array = new Array();
this.dictionary = new Dictionary();
this.list = new List();
}
public DataManager LoadData()
{
DataManager loaddata = JsonConvert.DeserializeObject<DataManager>(File.ReadAllText("./data_manager.json"));
Console.WriteLine("불러오기 완료");
return loaddata;
}
}
}
|
-----------------------------------------------------------------------------------------------------------------------------------
Instance Class
-----------------------------------------------------------------------------------------------------------------------------------
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Repeat_017
{
class Instance
{
public int id;
public string name;
public string desc;
public int amount;
public Instance()
{
Console.WriteLine("Instance 객체가 생성되었습니다.");
}
public void Init(int id,string name, string desc,int amount)
{
this.id = id;
this.name = name;
this.desc = desc;
this.amount = amount;
}
public void PrintAll()
{
Console.WriteLine("--------Instance 객체출력--------");
Console.WriteLine("id : {0}",this.id);
Console.WriteLine("name : {0}",this.name);
Console.WriteLine("deac : {0}",this.desc);
Console.WriteLine("amount : {0}",this.amount);
Console.WriteLine("---------------------------------");
}
}
}
|
-----------------------------------------------------------------------------------------------------------------------------------
Array Class
-----------------------------------------------------------------------------------------------------------------------------------
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Repeat_017
{
class Array
{
public int[] arrCreatureTypeId1;
public int[] arrCreatureTypeId2;
public string[] arrLandCreatureNames;
public string[] arrMarineCreatureNames;
public Array()
{
this.Init();
Console.WriteLine("Array 객체가 생성되었습니다.");
}
public void Init()
{
this.arrCreatureTypeId1 = new int[3] { 100, 101,102};
this.arrCreatureTypeId2 = new int[3] { 200, 201, 202 };
this.arrLandCreatureNames = new string[3] { "고양이", "강아지", "호랑이"};
this.arrMarineCreatureNames = new string[3] { "고래", "고등어", "상어" };
}
public void PrintAll()
{
Console.WriteLine("--------Array 객체출력--------");
foreach (var element in this.arrCreatureTypeId1)
{
Console.WriteLine(element);
}
foreach (var element in this.arrCreatureTypeId2)
{
Console.WriteLine(element);
}
foreach (var element in this.arrLandCreatureNames)
{
Console.WriteLine(element);
}
foreach (var element in this.arrMarineCreatureNames)
{
Console.WriteLine(element);
}
Console.WriteLine("---------------------------------");
}
}
}
|
-----------------------------------------------------------------------------------------------------------------------------------
Dictionary Class
-----------------------------------------------------------------------------------------------------------------------------------
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
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
namespace Repeat_017
{
class Dictionary
{
public Dictionary<int, string> dicLandCreatureNames;
public Dictionary<int, string> dicMarineCreatureNames;
public Dictionary()
{
this.Init();
Console.WriteLine("Dictionary 객체가 생성되었습니다.");
}
public void Init()
{
this.dicLandCreatureNames = new Dictionary<int, string>();
this.dicMarineCreatureNames = new Dictionary<int, string>();
}
public void InputData(int[] arrays1, int[] arrays2, string[] arrays3, string[] arrays4)
{
{
}
}
public void PrintAll()
{
Console.WriteLine("--------Dictionary 객체출력--------");
foreach (var pair in this.dicLandCreatureNames)
{
}
foreach (var pair in this.dicMarineCreatureNames)
{
}
Console.WriteLine("---------------------------------");
}
}
}
|
-----------------------------------------------------------------------------------------------------------------------------------
List Class
-----------------------------------------------------------------------------------------------------------------------------------
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
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Repeat_017
{
class List
{
public List<int> CreatureTypeIdList;
public List<string> landCreatureNamesList;
public List<string> marineCreatureNamesList;
public List()
{
this.Init();
Console.WriteLine("List 객체가 생성되었습니다.");
}
public void Init()
{
this.CreatureTypeIdList = new List<int>();
this.landCreatureNamesList = new List<string>();
this.marineCreatureNamesList = new List<string>();
}
public void Input(int[] arrays1,string[] arrays2, string[] arrays3)
{
this.CreatureTypeIdList.AddRange(arrays1);
this.landCreatureNamesList.AddRange(arrays2);
this.marineCreatureNamesList.AddRange(arrays3);
}
public void PrintAll()
{
Console.WriteLine("--------List 객체출력--------");
foreach (var element in this.CreatureTypeIdList)
{
Console.WriteLine(element);
}
foreach (var element in this.landCreatureNamesList)
{
Console.WriteLine(element);
}
foreach (var element in this.marineCreatureNamesList)
{
Console.WriteLine(element);
}
Console.WriteLine("---------------------------------");
}
}
}
|
-----------------------------------------------------------------------------------------------------------------------------------
출력값
-----------------------------------------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------------------------------------
저장된 json파일
-----------------------------------------------------------------------------------------------------------------------------------
'C# > 복습' 카테고리의 다른 글
2020-05-02 이름 자동으로 섞기 (0) | 2020.05.02 |
---|---|
2020-04-30 삼항연산자, Null병합연산자, 연산자 우선순위 (0) | 2020.04.30 |
2020-04-25 업적 시스템 복습(2) && 생각정리 (0) | 2020.04.25 |
2020-04-25 업적시스템 복습 & 생각정리 1 (0) | 2020.04.25 |
메서드반환 문제2 (0) | 2020.04.21 |