클래스 3 비공개속성

비공개 속성(Private attribute)

비공개 속성 사용하기

  • 비공개 속성(private attribute) : 클래스 밖에서는 접근할 수 없고 클래스 안에서만 사용할 수 있는 속성
  • 속성 앞에 밑줄 두개로 시작한다.
1
2
3
4
5
6
7
8
9
10
11
# Person 클래스에 지갑 속성 __wallet 추가
class Person:
def __init__(self, name, age, address, wallet):
self.name = name
self.age = age
self.address = address
self.__wallet = wallet # 변수 앞에 밑줄 두개

leesin = Person('리신', 35, '아이오니아 어딘가', 10000)
# leesin.__wallet 에러 발생
# 클래스 밖에서 비공개 속성을 접근하면 에러 발생
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# pay 메서드 만들기
class Person:
def __init__(self, name, age, address, wallet):
self.name = name
self.age = age
self.address = address
self.__wallet = wallet

def pay(self, amount):
self.__wallet -= amount # 비공개 속성은 클래스 안에서만 접근 가능
print('이제 {0}원 남았네요.'.format(self.__wallet))

leesin = Person('리신', 35, '아이오니아 어딘가', 10000)
leesin.pay(3000)
이제 7000원 남았네요.

비공개 메서드

  • 속성뿐만 아니라 메서드도 동일하다.
1
2
3
4
5
6
7
8
9
10
class Person:
def __greeting(self):
print('Hi')

def hello(self):
self.__greeting() # 클래스 안에서는 비공개 메서드를 호출할 수 있다.

lux = Person('럭스', 21, '데마시아 궁전', 2000)
# lux.__greeting()
# 클래스 바깥에서는 비공개 메서드를 호출할 수 없다.

출처

Author

HS

Posted on

2022-04-21

Updated on

2022-04-21

Licensed under

You need to set install_url to use ShareThis. Please set it in _config.yml.
You forgot to set the business or currency_code for Paypal. Please set it in _config.yml.

Comments

You forgot to set the shortname for Disqus. Please set it in _config.yml.
You need to set client_id and slot_id to show this AD unit. Please set it in _config.yml.