Skip to content

개인과제 문제풀이입니다.#7

Open
ybin4548 wants to merge 9 commits into
mainfrom
yeongbin
Open

개인과제 문제풀이입니다.#7
ybin4548 wants to merge 9 commits into
mainfrom
yeongbin

Conversation

@ybin4548
Copy link
Copy Markdown
Collaborator

@ybin4548 ybin4548 commented Jan 8, 2026

아, gitignore 추가 안하고 올렸네요 죄송합니다 ㅠㅠ
풀이 과정이 최적화되어있진 않을 것 같습니다.
요구사항에 맞춰서 작성하는 것에 중점을 두고 작성하다보니 부족한 부분들이 많을텐데, 의견 공유해주시면 감사할 것 같습니다.
질문도 환영입니다.

Comment thread chap2/chap2/main.swift
Comment on lines +208 to +211
protocol Introducible{
var name: String {get set}
func introduce()->String
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didSet을 위해 name 프로퍼티를 { get set }으로 선언하신 것일지 궁금합니다..!

프로토콜을 채택한 객체 내에서 name을 따로 연산 프로퍼티로 사용하거나 하시진 않은 것 같아 여쭈어봅니다..!

혹시 위와 같은 이유라면, didSet은 (예외를 제외하고) 저장 프로퍼티에서만 사용할 수 있기 때문에 { get }으로만 선언해주었어도 좋을 것 같아요☺️

프로퍼티 선언 시 { get set }으로 선언을 하면 객체에서 채택 시 해당 프로퍼티는 var로만 선언 가능하여 구현의 자유도가 떨어진다고 합니다.(let으로 선언 불가능) 좀더 자유로운 구현을 위해 제한 사항을 덜어주는 게 어떨까 싶어요!

저는 아무생각없이(...) { get }으로만 선언을 하고 있었는데 영빈님 코드 덕분에 자세히 학습하는 기회가 되었습니다☺️

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아, var name: String {get, set}으로 설정한 이유는
{get,set}, {get}은 프로토콜 자체를 변수로 선언해서 사용할 경우에 차이점이 나타납니다.
테스트 시에는 아래와 같은 코드도 작성했었는데,
var otherRobot: Introducible = Robot(name: "다른로봇")
이렇게 사용할 시 에러가 발생해서 get,set 둘 다 선언했습니다.
물론, 현재 코드에서는 프로토콜 자체 변수 선언하는 구문을 빼고 업로드해서 {get}만 사용해도 문제 없습니다 !
질문, 설명 감사해요.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗 그런 문제가 발생했었군요!! 해당 부분 수정으로 해결하셨다니 다행입니다! 자세한 설명 감사드립니다😁😁

Comment thread chap2/chap2/main.swift Outdated
Comment on lines +426 to +429
private func isFull(){
if fuel >= 100{
print("연료가 가득찼습니다.")
}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

선택 사항인데도 실제 자동차처럼 부가 기능을 많이 구현하신 점이 인상깊습니다ㅎㅎㅎ

refuel()함수에 직접적으로 print("연료가 가득찼습니다."가 바로 출력되도록 하지 않고 isFull 함수를 따로 만드신 이유가 있으실지 궁금합니당!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

두 메소드를 하나의 메소드로 만들어서 제공할 수 있으나, 접근 제어자를 달리하여 인스턴스 선언 후 외부 호출 가능 유무를 달리하고자 했습니다.
선언을 확인해보시면 두 메소드는
private func isFull(), public func refuel() 입니다.
isFull은 private으로 선언되어 외부 인스턴스에서 직접 참조가 불가능하여, 이를 사용하기 위해서는 public으로 선언한 refuel을 이용할 때만, 조건부로 사용할 수 있도록 구현했습니다.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아하.. 접근 제어자를 확인하지 못했네요ㅠㅠ! 설명 감사드립니다 어떤 의도이신지 이해했습니다!!

다만 현재 코드에서 refuel()에서 이미 fuel >= 100 조건을 체크한 다음 isFull()에서 다시 동일 조건인 fuel >= 100을 확인하고 있는 것 같습니다!

refuel()에서 isFull()을 실행한 다음 조건문으로 들어가는 건 어떠실지 말씀드려봅니당..ㅎㅎㅎ

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

메소드 구성할 때, 조건을 크게 신경안쓰고 동작만 하게 만들자 생각했더니 ....
조금 더 수정했습니다. 의견 감사해요.

Comment thread chap2/chap2/main.swift Outdated
init(name: String){
self.name = name
}
var B: B?
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

인스턴스 변수명은 네이밍 컨벤션에 따라 소문자로 선언해주셨으면 좋았을 것 같아요! var b: B?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

매번 작성하던 방식이랑 달라서 헷갈리네요.. 짚어주셔서 감사합니다.

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

Successfully merging this pull request may close these issues.

2 participants