본문 바로가기
C#프로그래밍

24일 수업 복습 및 어려웠던 부분 복습

by 노재두내 2023. 7. 24.

1. 상속

internal class Marine : TerranUnit

-> : 부모 클래스  를 이용해서 상속한다.

Marine marine = new Marine();

int hp = marine.GetHp();

-> 부모 클래스의 메서드를 사용할 수 있음

 

2. 오버로딩과 오버라이딩

<Marine 클래스>

public override void Attack() { //base.Attack(); Console.WriteLine("총으로 공격합니다."); }

//메서드 overloading

public void Attack(TerranUnit target) {} 

 

-----------------------------------------------------------------------------------------------------

어려웠던 부분

1. this

클래스의 현재 인스턴스를 가르킨다.

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

namespace LearnDotnet
{
    public class BreadMachine
    {
        public string serialNumber;
        public BreadMachine(string serialNumber)
        {
            this.serialNumber = serialNumber;
            Console.WriteLine("빵기계가 생성되었습니다.{0}", serialNumber);
        }
    }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LearnDotnet
{
    internal class App
    {
        //생성자
        public App()
        {
            BreadMachine machine1 = new BreadMachine("6a199236-c5bb-478b-9946-6db75ee75cd3");
            Console.WriteLine("machine1.serialNumber:{0}", machine1.serialNumber);

            BreadMachine machine2 = new BreadMachine("5bcbb381-19f2-4d48-84f8-6340bc35e49e");
            Console.WriteLine("machine2.serialNumber: {0}", machine2.serialNumber);
        }

    }
}

2. 매개변수 있는 메서드

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

namespace Practice
{
    class BreadMachine
    {
        public string serialNumber;
        //생성자
        public BreadMachine(string serialNumber)
        {
            this.serialNumber = serialNumber;
            Console.WriteLine("빵기계가 생성되었습니다.{0}", this.serialNumber);
        }

        public void MakeBread(string ingredient)
        {

            Console.WriteLine("{0}로 빵을 만들었습니다.", ingredient);
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Practice
{
    public class App
    {
        public App()
        {
            BreadMachine machine1 = new BreadMachine("3822992233dsfsse");
            machine1.MakeBread("밀가루");
            machine1.MakeBread("쌀가루");
        }
    }
}

----->  BreadMachine 에서는 this를 사용하고 MakeBread에서는 사용하지 않는 이유를 잘 모르겠다 -> MakeBread 에서도 사용가능하다 . 마찬가지로 BreadMachine에서 this를 사용하지 않아도 결과는 똑같이 나오는데 왜 사용하는지 , 어떨 때 사용하는지 잘 모르겠다.

 

3. 1차원 배열 연습

namespace Practice
{
    class Item
    {
        public string Name { get; set; }
        //생성자
        public Item(string name)
        {
            this.Name = name;
            Console.WriteLine("{0}", this.Name);
        }
    }
}
namespace Practice
{
    public class App
    {
        public App()
        {
            Item item0 = new Item("장검");
            Item item1 = new Item("담검");
            Item item2 = new Item("활");

            Item[] items = new Item[5];
            items[0] = item0;
            items[1] = item1;
            items[2] = item2;

            for(int i = 0; i < 3; i++)
            {
                Item item = items[i];
                if(item != null)
                {
                    Console.WriteLine("=> {0}", item.Name);
                }
            }
        }
    }
}

4. 1차원 배열 인벤토리

    class Inventory
    {
        private Item[] items;
        private int capacity;
        private int index;
        public Inventory(int capactiy)
        {
            this.capacity = capacity;
            items = new Item[this.capacity];
        }

        public void AddItem(Item item)
        {
            int lastIndex = this.capacity - 1;

            if (this.index > lastIndex)
            {
                Console.WriteLine("공간이 부족합니다.");
                return;
            }

            this.items[index] = item;
            index++;
        }

        public void PrintAllItems()
        {
            Console.WriteLine();
            for (int i = 0; i < this.items.Length; i++)
            {
                Item item = this.items[i];
                if (item == null)
                {
                    Console.WriteLine("[   ]");
                }
                else
                {
                    Console.WriteLine(item.name);
                }
            }
        }

        public Item GetItemByName(string searchItemName)
        {
            Console.WriteLine("searchItemName: {0}", searchItemName);

            Item foundItem = null;

            for (int i = 0; i < this.items.Length; i++)
            {
                Item item = this.items[i];
                if (item != null)
                {
                    Console.WriteLine("item name: {0}", item.name);
                    if (item.name == searchItemName)
                    {
                        this.items[i] = null;
                        foundItem = item;
                        break;
                    }
                }
            }
            return foundItem;
        }
    }
}
namespace practice2
{
    class Item
    {
        public string name;
        public Item(string name)
        {
            this.name = name;
        }
    }
}
namespace practice2
{
    class App
    {
        public App()
        {
            Inventory inven = new Inventory(5);
            inven.AddItem(new Item("장검"));  
            inven.AddItem(new Item("단검"));  
            inven.AddItem(new Item("활"));  
            inven.PrintAllItems();
            string searchItemName = "장검";
            Item item = inven.GetItemByName(searchItemName);
            if (item != null)
            {
                Console.WriteLine("{0}을 꺼냈습니다.", item.name);
            }
            else
            {
                Console.WriteLine("{0}을 찾지 못했습니다.", searchItemName);
            }
            inven.PrintAllItems();
        }
    }
}

'C#프로그래밍' 카테고리의 다른 글

인벤토리 연습 2  (0) 2023.07.25
배열 복습  (0) 2023.07.25
아이템 배열  (0) 2023.07.24
메딕이 마린 힐  (0) 2023.07.24
스타크래프트  (0) 2023.07.21