0311 Class

2021. 3. 11. 16:22C#/수업내용

클래스는 참조 형식 = heap(값)+stack(주소)에 저장된다.

object/string/사용자 정의 형식

new 연산자를 사용하여 인스턴스(실제 메모리를 가진 개체)를 만들어낸다. 

 

Class = 기능과 정보가 정의된 파일

만들어진 호환성 있는 형식의 개체를 할당할 때까지 변수레는 null 값이 포함된다.

  기본 값이 null = 아무것도 참조하고 있지 않은 값

 

new 연산자는

1. 새 유형의 인스턴스를 만듭니다.

2. 클래스의 (메모리에 올라간 클래스의)생성자를 호출합니다.

 

클래스를 만들 때는 추상적으로 우선으로 한다.

상속 = 부모(c#은 부모가 무조건 1개) 자식 관계를 나타낸다.

부모가 가진 요소를 자식들은 모두 물려받아야 한다.

 

예)

1. 모든 유닛들은 위치를 가지고 있다. (일반화를 기본으로 한다.)

2. 모든 유닛들은 움직일 수 있다. 

 

상속관계 ↑로 표시

 

 

배럭 Barracks
=============
체력 hp
=============
유닛생성(유닛타입)  Marine CreateMarine()
이동하다 void Move ()
파괴되다 void Destroy ()

마린 Marine
=============
체력 hp
공격력 damage
=============
공격하다 (타겟) void Attack (Marine target)
이동하다 void Move ()
피해입다 (피해량) void Hit (float damage)
죽다 void Die()

 

//클래스 마린의 메서드와 이름/체력/데미지 선언
    public class Marine
    {
        //맴버 변수
        public float hp;
        public float damage;
        public string name;
        public Marine()
            {
            Console.WriteLine("마린 생성자 호출됨");
            }
        public void Attack(Marine target)
        {
            //타겟에세 피해를 준다.
            Console.WriteLine("{0}가 {1}을 공격합니다.",this.name, target.name);
            target.Hit(this.damage);
        }

        public void Hit(float damage)
        {
            this.hp -= damage;
            Console.WriteLine("{0}가 {1}의 피해를 받았습니다.", this.name,this.damage);
        }
        
        public void Die()
        {
            Console.WriteLine("{0}가 죽었습니다.",this.name);
        }
    }
//마린 두 채가 생성되는 App
    public class App
    {
        //생성자
        public App() {
            Console.WriteLine("App생성자 호출됨");

            Marine hong=new Marine();
            hong.name = "홍길동";
            hong.hp = 75;
            hong.damage = 5;
            Console.WriteLine(hong.name, hong.hp, hong.damage);

            Marine lim = new Marine();
            lim.name = "임꺽정";
            lim.hp = 75;
            lim.damage = 5;
            Console.WriteLine(lim.name, lim.hp, lim.damage);
        }
    }

 

//질럿 클래스 생성
    public class Zealot
    {
        public Zealot() 
        { 
            Console.WriteLine("질럿이 생성되었습니다.");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Zealot zealot = new Zealot();
            
        }
    }

 

public class Hydra
    {
        //히드라 클래스 생성
        public Hydra() 
        {
            Console.WriteLine("히드라가 생성되었습니다.");
        }
    }
class Program
    {
        //히드라 클래스 호출
        static void Main(string[] args)
        {
            Hydra hydra = new Hydra();
        }
    }

 

public class Queen
    {
        //퀸 클래스 생성
        public Queen() 
        {
            Console.WriteLine("퀸이 생성되었습니다.");
        }
    }
//퀸 클래스 호출
        static void Main(string[] args)
        {
            Queen queen = new Queen();
        }

 

public class HighTempler
    {
        public HighTempler() 
        {
            Console.WriteLine("하이템플러가 생성되었습니다.");
        }
    }
//하이템플러 클래스 호출
        static void Main(string[] args)
        {
            HighTempler highTempler = new HighTempler();
        }

 

public class Goliat
    {
        public Goliat()
        {
            Console.WriteLine("골리앗이 생성되었습니다.");
        }
    }
class Program
    {
        //골리앗 클래스 호출
        static void Main(string[] args)
        {
            Goliat goliat = new Goliat();
        }
    }

 

 

public class DarkTempler
    {
        public DarkTempler()
        {
            Console.WriteLine("다크템플러가 생성되었습니다.");
        }
    }
class Program
    {
        //다크템플러 클래스 호출
        static void Main(string[] args)
        {
            DarkTempler darkTempler;
            darkTempler= new DarkTempler();
            Console.WriteLine(darkTempler);
        }
    }

 

 

public class Firebat
    {
        public Firebat()
        {
            Console.WriteLine("파이어벳이 생성되었습니다.");
        }
    }
class Program
    {
        //파이어뱃 클래스 호출
        static void Main(string[] args)
        {
            Firebat fireBat;
            fireBat = new Firebat();
            Console.WriteLine(fireBat);
        }
    }

 

 

public class Carrigon
    {
        public Carrigon()
        { 
            Console.WriteLine("캐리건이 생성되었습니다.");
        }
    }
class Program
    {
        //캐리건 클래스 호출
        static void Main(string[] args)
        {
            Carrigon carrigon;
            carrigon = new Carrigon();
            Console.WriteLine(carrigon);
        }
    }

 

public class Medic
    {
        public Medic()
        {
            Console.WriteLine("'의사가 필요하세요?' 메딕이 생성되었습니다.");
        }
    }
//메딕 클래스 호출
        static void Main(string[] args)
        {
            Medic medic;
            medic = new Medic();
            Console.WriteLine(medic);
        }

 

 

class Valkyrie
    {
        public Valkyrie()
        { 
            Console.WriteLine("발리키가 생성되었습니다.");
        }

    }
//발키리 클래스 호출
        static void Main(string[] args)
        {
            Valkyrie valkyrie;
            valkyrie = new Valkyrie();
            Console.WriteLine(valkyrie);
        }

'C# > 수업내용' 카테고리의 다른 글

0312 Array 배열  (0) 2021.03.12
0312 클래스+메서드  (0) 2021.03.12
0310 메서드  (0) 2021.03.10
0310 if/while/switch statement복습  (0) 2021.03.10
0310 switch 문으로 캐릭터 생성 예시  (0) 2021.03.10