Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7주차] Generic에 대해 설명하시오. #13

Open
obtusa07 opened this issue Jan 8, 2023 · 1 comment
Open

[7주차] Generic에 대해 설명하시오. #13

obtusa07 opened this issue Jan 8, 2023 · 1 comment

Comments

@obtusa07
Copy link

obtusa07 commented Jan 8, 2023

제네릭 (Generic)

제네릭은 어떤 타입에도 유연하게 대응하기 위한 기능.
대표적인 장점으로 재사용이 쉽다는 것, 코드의 중복을 줄일 수 있다는 것, 깔끔하고 추상적인 표현이 가능하다는 것이 있다.

어떻게 사용하지?

제네릭이 필요한 타입이나 메서드 이름 뒤에 <>을 적는데 이 때 <> 사이에는 타입 매개변수를 써준다. 이 때, 안에는 타입매개변수로 플레이스홀더(placeholder)를 입력하는데 <T, U, V> 등의 관용적 표현을 사용한다. 각 플레이스 홀더는 들어오는 타입이 동일함을 의미한다.

제네릭으로 구현되어도 특수한 이름을 가지는 경우가 아래 두 경우가 예시이다.

  • Key, Value
  • Element

제네릭 타입

class, struct, enum 등 단순히 함수에만 제네릭을 사용하는 것이 아니라 타입에도 사용한다.

@hongssup
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants