1주차 복습
2023. 7. 24.

클래스

 

클래스는 참조형식이다. new 라는 키워드를 이용해 인스턴스를 명시적으로 만든다.

호환되는 형식의 개체를 할당할 때까지 null을 포함한다.

리터럴: 고정된 값

 

new 키워드의 역할

1. 클래스의 인스턴스 생성

2. 해당 클래스의 생성자를 호출한다.

 

.을 찍으면 해당 인스턴스의 멤버에 접근할 수 있다.

 

외부에서 접근하려면 public이어야한다.

public을 써주지 않으면 private이다.

 

메서드

-일련의 코드 블록

메서드를 만들기 위한 단계

1. 기능을 생각한다.

2. 기본형으로 정의

: void 메서드형(){

                           ///기능구현

                                     }

3. 기능을 구현 

4. 사용 (호출한다.)

 

메서드의 종류

1. 기본형

 

2. 매개변수가 있는 메서드

void 메서드명(매개변수: 변수타입 변수명 꼴){

                                                       }

 

3. 반환값만 있는 메서드

값의타입 메서드명(){

returen 값;

}

 

return : 함수의 실행을 종료하고, 함수의 결과가 있는 경우 호출자로 반환한다.

 

4. 매개변수 + 반환값도 있는 메서드

 

string SayHello(string name){

                      string str = name+"님 안녕하세요"

                      return str;

}

 

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

namespace Starcraft
{
   
    internal class Marine
    {
        //멤버 변수 : 객체의 생명 주기동안 유지된다.
        public int hp; //생명력
        int maxHp;
        public int damage; //공격력
        public int attacked;
        float moveSpeed; //이동속도
        int x;//위치정보 x축
        int y;//y축

        //생성자
        public Marine(int hp, int damage, float moveSpeed ,int x, int y)
        {   this.hp = hp;//hp는 매개변수, this.hp 는 멤버 필드 
            //매개변수의 값을 멤버 변수에 할당
            this.damage = damage;
            this.moveSpeed = moveSpeed;
            this.x = x;
            this.y = y;
            Console.WriteLine("\nMarine이 생성되었습니다.");
            
           // Console.WriteLine("생명력: {0}",this.hp);
         //   Console.WriteLine("공격력: {0}", this.damage);
          //  Console.WriteLine("이동속도: {0}\n", this.moveSpeed);
           
            
        }

        //메서드

        public void PrintProperties()//상태 출력 메서드
        {
            Console.WriteLine("생명력: {0}", this.hp);
            Console.WriteLine("공격력: {0}", this.damage);
            Console.WriteLine("이동속도: {0}\n", this.moveSpeed);
            Console.WriteLine("위치 : ({0},{1})", this.x, this.y);
        }
        public void Move(int x, int y)//이동 목표,좌표
        {           
            Console.WriteLine("({0},{1})에서 ({2},{3})로 이동했습니다.",this.x,this.y,x,y);
            this.x = x;
            this.y = y;
        }

        public void HitDamage(int damage)
        {//피해를 받는 메서드
            this.attacked = damage;
            this.hp -= damage;
            // Console.WriteLine("공격자: {0}", zergling);
            Console.WriteLine("피해 ({0})를 받았습니다, 생명력 : {1}", damage, this.hp);
            
        }
        public void Attack(Zergling target)
        {

            Console.WriteLine("{0}을 공격했습니다.",target);
            target.HitDamage(this,damage);//저글링의 공격력만큼 저글링이 피해를 받는 메서드
        }
        //생명력을 반환하는 메서드
        public int GetHp()
        {
           
            return this.hp;//메서드를 종료하고 반환값이 있는 경우
        }
        //생명력을 반환하는 메서드
        public int GetDamage()
        {

            return this.damage;//메서드를 종료하고 반환값이 있는 경우
        }
        //생명력을 반환하는 메서드
        public float GetMoveSpeed()
        {

            return this.moveSpeed;//메서드를 종료하고 반환값이 있는 경우
        }
    }
}

Marine.cs

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;

namespace Starcraft
{
    internal class Medic
    {
        //멤버변수
        int hp;
        float heal;
        float moveSpeed;


        //생성자
        public Medic(int hp, float heal, float moveSpeed) {
            this.hp = hp; //멤버 변수에 매개변수 저장
            this.heal = heal;
            this.moveSpeed = moveSpeed;
            Console.WriteLine("\n의무관이 생성되었습니다.");
        }

        //메서드
        public void ShowMedicState()
        {
            Console.WriteLine("생명력: {0}", hp);
            Console.WriteLine("초당치료량: {0}", heal);
            Console.WriteLine("이동속도: {0}\n", moveSpeed);

        }

        public void MoveStop()//정지
        {
            Console.WriteLine("의무관이 정지했습니다.");

        }

        public void Move()//걷기
        {
            Console.WriteLine("의무관이 이동합니다.");
        }

        public void Heal(Marine marine)
        {//피해입은 만큼 회복
            marine.hp += marine.attacked;
            Console.WriteLine("마린이 치료({0})를 받았습니다. 생명력: {1}.",marine.attacked,marine.GetHp());
        }

        public void Die()
        {
            Console.WriteLine("의무관이 사망하였습니다.");
        }

    }
}

Medic.cs

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

namespace Starcraft
{
    internal class Zergling
    {
        //멤버 변수
        int hp;
        public int damage;
        float moveSpeed;
        int x;//x축
        int y;//y축
        //생성자
        public Zergling(int hp, int damage, float moveSpeed) {
            this.hp = hp;
            this.damage = damage;
            this.moveSpeed = moveSpeed;

            Console.WriteLine("\n저글링이 생성되었습니다.");
            
        }

        //메서드

        public void PrintProperties()//상태 출력 메서드
        {
            Console.WriteLine("생명력: {0}", this.hp);
            Console.WriteLine("공격력: {0}", this.damage);
            Console.WriteLine("이동속도: {0}\n", this.moveSpeed);
        }
        public void SetPosition(int x, int y)
        {//좌표를 설정하는 메서드
            this.x = x;
            this.y = y;
            Console.WriteLine("좌표 설정 완료: ({0},{1})", this.x, this.y);
        }
        public void Move()
        {
            Console.WriteLine("({0},{1})에서 ({2},{3})로 이동했습니다.", this.x, this.y, x, y);
            this.x = x;
            this.y = y;
        }
        public void HitDamage(Marine marine, int damage)
        {//피해를 받는 메서드

            this.hp -= damage;
            Console.WriteLine("공격자: {0}", marine);
            Console.WriteLine("피해 ({0})를 받았습니다, 생명력 : {1}", damage,this.hp);

        }
        public void Attack()
        {
            Console.WriteLine("공격했습니다.");
        }
        public void Die()
        {
            Console.WriteLine("저글링이 사망했습니다.");
        }
    }
}

Zergling.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace Starcraft
{
    internal class App
    {
        //생성자
        public App()
        {
            Marine marine0 = new Marine(40, 6, 1.8f,5 ,0);
            marine0.HitDamage(3);

       //     Zergling zergling0 = new Zergling(35, 5, 2.61f);
        //    zergling0.SetPosition(4, 0);

            //마린 ->  저글링 공격
       //     marine0.Attack(zergling0);

            Medic medic = new Medic(60, 5.8f, 1.8f);
            medic.Heal(marine0);
        }
    }

 }

App.cs

'C# 기초 복습' 카테고리의 다른 글

Quaternion.identity, LookRotation, 비트 연산자  (0) 2023.08.21
Unity- Coroutines(코루틴)  (0) 2023.08.08
2주차 복습  (0) 2023.07.31
JSON예제, 대리자 복습  (0) 2023.07.27
배열 예제 복습  (0) 2023.07.25
myoskin