[C#] base - 부모 클래스의 생성자 불러오기
나는 C++ 프로그래머라서 그런지 C#의 문법적인건 가끔 까먹는 경우가 많다.
C++의 경우에는 부모 클래스의 생성자를 자식 클래스의 생성자에서 호출할때는 이니셜라이저에 부모 클래스 이름으로 호출해주면 됐었다.
C++의 경우
class Parent;
{
private:
int i;
public:
Parent(int i) : i(i) { }
}
class Child : public Parent
{
private:
int j;
public:
Child(int i, int j) : Parent(i), j(j) { } // 부모의 이름으로 부모의 생성자를 호출할 수 있다.
}
하지만 C#의 경우는 약간 달라서 부모 클래스의 이름으로는 부모 클래스의 생성자를 호출할 수 없고 base 키워드를 사용해야 한다.
C#의 경우
public class Parent;
{
private int i;
public Parent(int i)
{
this.i = i;
}
}
public class Child : Parent
{
private int j;
public Child(int i, int j) : base(i) // 부모 클래스의 생성자를 호출하려면 base 키워드를 사용해야 한다.
{
this.j = j;
}
}
C# 프로그래밍 책을 한 번 훑어보면서 봤던 내용인데 C#코딩을 자주하는 편은 아니라 정확한 방법을 까먹어서 다시 찾아보게 되었다.
[유니티 어필리에이트 프로그램]
아래의 링크를 통해 에셋을 구매하시거나 유니티를 구독하시면 수익의 일부가 베르에게 수수료로 지급되어 채널의 운영에 도움이 됩니다.
에셋스토어
여러분의 작업에 필요한 베스트 에셋을 찾아보세요. 유니티 에셋스토어가 2D, 3D 모델, SDK, 템플릿, 툴 등 여러분의 콘텐츠 제작에 날개를 달아줄 다양한 에셋을 제공합니다.
assetstore.unity.com
Easy 2D, 3D, VR, & AR software for cross-platform development of games and mobile apps. - Unity Store
Have a 2D, 3D, VR, or AR project that needs cross-platform functionality? We can help. Take a look at the easy-to-use Unity Plus real-time dev platform!
store.unity.com
Create 2D & 3D Experiences With Unity's Game Engine | Unity Pro - Unity Store
Unity Pro software is a real-time 3D platform for teams who want to design cross-platform, 2D, 3D, VR, AR & mobile experiences with a full suite of advanced tools.
store.unity.com
[투네이션]
-
toon.at
[Patreon]
WER's GAME DEVELOP CHANNEL님이 Game making class videos 창작 중 | Patreon
WER's GAME DEVELOP CHANNEL의 후원자가 되어보세요. 아티스트와 크리에이터를 위한 세계 최대의 멤버십 플랫폼에서 멤버십 전용 콘텐츠와 체험을 즐길 수 있습니다.
www.patreon.com
[디스코드 채널]
Join the 베르의 게임 개발 채널 Discord Server!
Check out the 베르의 게임 개발 채널 community on Discord - hang out with 399 other members and enjoy free voice and text chat.
discord.com