一、訪問私有成員
1、對象無法訪問私有變量
在下面的 Python 類 Student 中 , 定義了私有的成員變量 ,
# 定義私有成員
__address = None
該私有成員變量 , 只能在類內部進行訪問 , 類的外部無法進行訪問 ;
在 類外部 創(chuàng)建的 Student 實例對象 , 是無法訪問 __address
私有成員的 ;
使用 實例對象 訪問 類的私有成員 , 編譯時不會報錯 , 但是運行時會報錯
AttributeError: 'Student' object has no attribute '__address'. Did you mean: 'address'?
最后一行代碼
print(s1.__address)
訪問 s1 實例對象 的 __address
成員 , 會報如下錯誤 ;
代碼示例 :
"""
面向對象 - 封裝
"""
# 定義 Python 類
class Student:
name = None
age = None
def say(self):
print(f"{self.name} is {self.age} years old")
# 定義私有成員
__address = None
# 定義私有成員方法
def __say(self):
print(f"地址是 {self.__address}")
def __init__(self, name, age, address):
self.name = name
self.age = age
self.address = address
# 創(chuàng)建對象
s1 = Student("Tom", 18, "學院路6號")
print(s1.name)
print(s1.age)
print(s1.__address)
執(zhí)行結果 :
Y:\002_WorkSpace\PycharmProjects\pythonProject\venv\Scripts\python.exe Y:/002_WorkSpace/PycharmProjects/HelloPython/hello.py
Traceback (most recent call last):
File "Y:\002_WorkSpace\PycharmProjects\HelloPython\hello.py", line 31, in <module>
print(s1.__address)
AttributeError: 'Student' object has no attribute '__address'. Did you mean: 'address'?
Tom
18
Process finished with exit code 1
2、對象無法訪問私有方法
在類中 定義私有成員方法
# 定義私有成員方法
def __say(self):
print(f"地址是 {self.__address}")
創(chuàng)建對象 , 調用該私有成員方法 ,
# 創(chuàng)建對象
s1 = Student("Tom", 18, "學院路6號")
s1.__say()
報如下錯誤 :
AttributeError: 'Student' object has no attribute '__say'
代碼示例 :
"""
面向對象 - 封裝
"""
# 定義 Python 類
class Student:
name = None
age = None
def say(self):
print(f"{self.name} is {self.age} years old")
# 定義私有成員
__address = None
# 定義私有成員方法
def __say(self):
print(f"地址是 {self.__address}")
def __init__(self, name, age, address):
self.name = name
self.age = age
self.address = address
# 創(chuàng)建對象
s1 = Student("Tom", 18, "學院路6號")
print(s1.name)
print(s1.age)
s1.__say()
執(zhí)行結果 :
Y:\002_WorkSpace\PycharmProjects\pythonProject\venv\Scripts\python.exe Y:/002_WorkSpace/PycharmProjects/HelloPython/hello.py
Tom
18
Traceback (most recent call last):
File "Y:\002_WorkSpace\PycharmProjects\HelloPython\hello.py", line 31, in <module>
s1.__say()
AttributeError: 'Student' object has no attribute '__say'
Process finished with exit code 1
3、類內部訪問私有成員
在 Student 類中 , 定義了 私有成員變量 和 私有成員方法 :
# 定義私有成員
__address = None
# 定義私有成員方法
def __say(self):
print(f"address is {self.__address}")
并且在 say 成員方法 中 , 調用了 上述 私有的 成員變量 和 成員方法 :
def say(self):
print(f"{self.name} is {self.age} years old , address is {self.__address}")
self.__say()
最終的執(zhí)行結果為 :
Tom is 18 years old , address is 學院路6號
address is 學院路6號
完整代碼示例 :
"""
面向對象 - 封裝
"""
# 定義 Python 類
class Student:
name = None
age = None
def say(self):
print(f"{self.name} is {self.age} years old , address is {self.__address}")
self.__say()
# 定義私有成員
__address = None
# 定義私有成員方法
def __say(self):
print(f"address is {self.__address}")
def __init__(self, name, age, address):
self.name = name
self.age = age
self.__address = address
# 創(chuàng)建對象
s1 = Student("Tom", 18, "學院路6號")
print(s1.name)
print(s1.age)
s1.say()
執(zhí)行結果 :文章來源:http://www.zghlxwxcb.cn/news/detail-540305.html
Y:\002_WorkSpace\PycharmProjects\pythonProject\venv\Scripts\python.exe Y:/002_WorkSpace/PycharmProjects/HelloPython/hello.py
Tom
18
Tom is 18 years old , address is 學院路6號
address is 學院路6號
Process finished with exit code 0
文章來源地址http://www.zghlxwxcb.cn/news/detail-540305.html
到了這里,關于【Python】面向對象 - 封裝 ② ( 訪問私有成員 | 對象無法訪問私有變量 / 方法 | 類內部訪問私有成員 )的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!