this란?

클래스의 현재 인스턴스를 가리키는 키워드

 

https://www.youtube.com/watch?v=w3YquM7W_cY&list=PLTFRwWXfOIYBmr3fK17E0VhKPyYrGy75z&index=34

 

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

namespace Step34
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Character hong = new Character("홍길동", 3, 10);
            Character lim = new Character("임꺽정", 2, 12);
            Console.WriteLine("캐릭터의 이름: {0}, 공격력: {1}, 체력:{2}/{3} ", hong.name, hong.damage, hong.hp, hong.maxHp);
            Console.WriteLine("캐릭터의 이름: {0}, 공격력: {1}, 체력:{2}/{3} ", lim.name, lim.damage, lim.hp, lim.maxHp);
        }
    }
}

 

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

namespace Step34
{
    internal class Character
    {
        //맴버변수 정의
        public string name;
        public int damage;
        public int hp;
        public int maxHp;

        //생성자
        public Character(string name, int damage, int maxHp)
        {
            this.name = name;
            this.damage = damage;
            this.maxHp = maxHp;
            this.hp = this.maxHp;



        }
    }
}

 

 

 

'Study > C#' 카테고리의 다른 글

[C#] 상속  (1) 2024.06.03
[C#] static 한정자  (0) 2024.06.02
[C#] 점연산자 NullReferenceException  (0) 2024.05.30
[C#] 맴버변수와 지역변수  (0) 2024.05.29
[C#] 생성자 메서드  (0) 2024.05.29

car.name => 맴버 변수

car.Move(); => 맴버 메서드

 

다음과 같이 .을 사용하여 형식 맴버에 액세스

 

참조하고 있지 않는 참조변수에서 맴버에 액세스하려면

NullReferenceException 발생

 

여기서 Exception이란?

애플리케이션 실행 중에 발생하는 오류

 

예시

 

참조가 없는 변수의 맴버를 액세스 하려고 했기 때문에

NullReferenceException 발생

 

'Study > C#' 카테고리의 다른 글

[C#] static 한정자  (0) 2024.06.02
[C#] this 키워드  (0) 2024.05.31
[C#] 맴버변수와 지역변수  (0) 2024.05.29
[C#] 생성자 메서드  (0) 2024.05.29
[C#] 클래스와 new 연산자  (0) 2024.05.29

맴버변수 ?

 

클래스에 정의된 변수

맴버변수는 인스턴스가 메모리에 있는동안 접근 가능

 

지역변수?

메서드에 정의된 변수

메서드가 실행되는 동안 접근 가능

https://www.youtube.com/watch?v=tR376tcsgag&list=PLTFRwWXfOIYBmr3fK17E0VhKPyYrGy75z&index=32

 

https://www.youtube.com/watch?v=tR376tcsgag&list=PLTFRwWXfOIYBmr3fK17E0VhKPyYrGy75z&index=32
https://www.youtube.com/watch?v=tR376tcsgag&list=PLTFRwWXfOIYBmr3fK17E0VhKPyYrGy75z&index=32
https://www.youtube.com/watch?v=tR376tcsgag&list=PLTFRwWXfOIYBmr3fK17E0VhKPyYrGy75z&index=32
https://www.youtube.com/watch?v=tR376tcsgag&list=PLTFRwWXfOIYBmr3fK17E0VhKPyYrGy75z&index=32

 

가비지 컬렉션?

메모리를 자동으로 관리해주는 메커니즘

 

https://www.youtube.com/watch?v=tR376tcsgag&list=PLTFRwWXfOIYBmr3fK17E0VhKPyYrGy75z&index=32
https://www.youtube.com/watch?v=tR376tcsgag&list=PLTFRwWXfOIYBmr3fK17E0VhKPyYrGy75z&index=32
https://www.youtube.com/watch?v=tR376tcsgag&list=PLTFRwWXfOIYBmr3fK17E0VhKPyYrGy75z&index=32

 

https://www.youtube.com/watch?v=tR376tcsgag&list=PLTFRwWXfOIYBmr3fK17E0VhKPyYrGy75z&index=32

 

null은 참조하지 않음

 

https://www.youtube.com/watch?v=tR376tcsgag&list=PLTFRwWXfOIYBmr3fK17E0VhKPyYrGy75z&index=32
https://www.youtube.com/watch?v=tR376tcsgag&list=PLTFRwWXfOIYBmr3fK17E0VhKPyYrGy75z&index=32

 

 

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

namespace Step32
{
    internal class Car
    {
        public string name;
        public float speed;
        public Car(string carName)
        {
            name = carName;
            Console.WriteLine("{0}이(가) 자동차가 생성되었습니다", carName);
        }

        public void Move(float moveSpeed)
        {
            speed = moveSpeed;
            Console.WriteLine("{0}속도로 이동합니다", moveSpeed);
        }
    }
}

 

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

namespace Step32
{
    internal class Car
    {
        public string name;
        public float speed;
        public Car(string carName)
        {
            name = carName;
            Console.WriteLine("{0}이(가) 자동차가 생성되었습니다", carName);
        }

        public void Move(float moveSpeed)
        {
            speed = moveSpeed;
            Console.WriteLine("{0}속도로 이동합니다", moveSpeed);
        }
    }
}

 

'Study > C#' 카테고리의 다른 글

[C#] this 키워드  (0) 2024.05.31
[C#] 점연산자 NullReferenceException  (0) 2024.05.30
[C#] 생성자 메서드  (0) 2024.05.29
[C#] 클래스와 new 연산자  (0) 2024.05.29
[C#]메서드 반환타입  (0) 2024.05.28

+ Recent posts