좋은 CTO님을 만나서 놓치고, 모르는 내용들을 많이 배우고 있다. 오늘은 그 내용을 좀 공부하고 정리하는 차원에서(까먹지 않기 위해) 이론을 정리해놓으려고 한다.
class MyClass:
def __init__(self):
self.__private_member = "private"
def __private_method(self):
print("This is a private method.")
def public_method(self):
print("This is a public method.")
self.__private_method()
my_class = MyClass()
print(my_class.__private_member) # 접근 불가능
my_class.__private_method() # 접근 불가능
my_class.public_method() # 접근 가능
class MyClass:
def __init__(self):
self.public_member = "public"
def public_method(self):
print("This is a public method.")
class MyClass:
def __init__(self):
self._protected_member = "protected"
def _protected_method(self):
print("This is a protected method.")
def public_method(self):
print("This is a public method.")
self._protected_method()
class MySubclass(MyClass):
def __init__(self):
super().__init__()
def public_method(self):
print("This is a public method in the subclass.")
self._protected_method()
print(self._protected_member)
my_class = MyClass()
print(my_class._protected_member) # 접근 가능하지만 권장하지 않음
my_class._protected_method() # 접근 가능하지만 권장하지 않음
my_class.public