인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

Inflearn Community Q&A

dbsgh17341732's profile image
dbsgh17341732

asked

[MMORPG Game Development Series with C# and Unity] Part 1: Introduction to Basic C# Programming

생성자 관련 질문입니다.

Written on

·

187

0

public knight()

{

    hp = 100;

    attack = 10;

    Console.WriteLine("생성자 호출")

}

public knight(int hp) : this()

{

    this.hp = hp;

    Console.WriteLine("int 생성자 호출!")

}

아래 생성자를 호출했을 시 attack 이 없는 문제를 해결하려고 this()를 사용한다는 건 알겠습니다. 그런데 이렇게 하면 hp와 attack을 가진 생성자를 만들려는 원래의 의도와 다르게 hp만 가진 생성자 하나, hp와 attack을 모두 가진 생성자 하나, 이렇게 두 개를 생성하게 되는데, 이렇게 했을 때 생기는 문제는 없나요?

생성자C#

Answer 1

0

rookiss님의 프로필 이미지
rookiss
Instructor

hp만 가진 생성자를 사용할 경우,
attack은 기본값으로 설정한다는 의미가 되겠죠.
위 예시는 정말 문법적인 예제이고,
실제로는 사양에 맞게 사용하면 됩니다.

dbsgh17341732's profile image
dbsgh17341732

asked

Ask a question