IT/C#
[C#_클래스] this, static 키워드
배당 줍는 다람쥐
2021. 9. 28. 22:54
반응형
this 키워드
- 객체 자신을 참조하는 키워드
- 사용
- 함수의 파라미터 이름과 멤버 변수 이름이 동일
- 클래스 내부에서 멤버변수를 접근
class A{ int a; public A(int a){ this.a = a; // this가 가르키는 값은 클래스 내부에 선언된 멤버변수이다. } }
static 키워드
- 클래스의 멤버를 객체 생성 없이 사용 가능
- new 키워드 없이도 접근하여 사용 가능
- 클래스 static 필드(변수)
- 클래스 static 메소드(함수)
- static 메소드 내부에 사용하는 변수는 반드시 static
class A { public static int a; public static int b; public static void Print() { Console.WriteLine("a: {0}", a); } }
반응형