python3的私有屬性和繼承
阿新 • • 發佈:2018-11-01
寫在最前面:從最簡單講起
先看一個最簡單的類的例子
class people: name = '' # 定義基本屬性 age = 0 sex = '' __weight = 0 # 定義私有屬性,私有屬性在類外部無法直接進行訪問 # 定義構造方法 def __init__(self, n, a, w ,s): self.name = n self.age = a self.sex = s self.__weight = w def infomation(self): print("%s is a %d yesrs old %s and weights %skg" % (self.name, self.age, self.sex, self.__weight)) if __name__ == '__main__': # 例項化類 p = people('Mike', 25, 60, 'man') p.infomation()
簡單的不能再簡單了
Mike is a 25 yesrs old man and weights 60kg
當我試圖在類外直接訪問私有屬性
class people: name = '' # 定義基本屬性 age = 0 sex = '' __weight = 0 # 定義私有屬性,私有屬性在類外部無法直接進行訪問 # 定義構造方法 def __init__(self, n, a, w ,s): self.name = n self.age = a self.sex = s self.__weight = w def information(self): print("%s is a %d yesrs old %s and weights %skg" % (self.name, self.age, self.sex, self.__weight)) if __name__ == '__main__': # 例項化類 p = people('Mike', 25, 60, 'man') print(p.__weight)
會報錯
AttributeError: 'people' object has no attribute '__weight'
說明weight作為私有屬性,不能直接訪問
那有沒有可能繞過去呢,可以
為了方便描述,我們給prople類再加一個私有屬性height並賦值180,然後我們試圖訪問__height
class people: name = '' # 定義基本屬性 age = 0 sex = '' __weight = 0 # 定義私有屬性,私有屬性在類外部無法直接進行訪問 # 定義構造方法 def __init__(self, n, a, w ,s): self.name = n self.age = a self.sex = s self.__weight = w self.__height = 180 def information(self): print("%s is a %d yesrs old %s and weights %skg" % (self.name, self.age, self.sex, self.__weight)) class Programmer(people): language = '' def __init__(self,n, a, w, s, l): # 呼叫父類的構函 people.__init__(self, n, a, w, s) self.language = l # 重寫父類的方法 def information(self): print("%s is a %d yesrs old %s and weights %skg.He uses %d" % (self.name, self.age, self.sex, self.__weight,self.language)) if __name__ == '__main__': # 例項化類 p = people('Mike', 25, 60, 'man') print(p.__height)
AttributeError: 'people' object has no attribute '__height'
很顯然,不可以
class people:
name = '' # 定義基本屬性
age = 0
sex = ''
__weight = 0 # 定義私有屬性,私有屬性在類外部無法直接進行訪問
# 定義構造方法
def __init__(self, n, a, w ,s):
self.name = n
self.age = a
self.sex = s
self.__weight = w
self.__height = 180
if __name__ == '__main__':
# 例項化類
p = people('Mike', 25, 60, 'man')
print(p._people__height)
誒,這樣就可以了
180
然後我們創造一個Programmer來繼承這個類的所有屬性,包括私有屬性然後又加了一個language的非私有屬性
class people:
name = '' # 定義基本屬性
age = 0
sex = ''
__weight = 0 # 定義私有屬性,私有屬性在類外部無法直接進行訪問
# 定義構造方法
def __init__(self, n, a, w ,s):
self.name = n
self.age = a
self.sex = s
self.__weight = w
self.__height = 180
def information(self):
print("%s is a %d yesrs old %s and weights %skg" % (self.name, self.age, self.sex, self.__weight))
class Programmer(people):
language = ''
def __init__(self,n, a, w, s, l):
# 呼叫父類的構函
people.__init__(self, n, a, w, s)
self.language = l
# 重寫父類的方法
def information(self):
print("%s is a %d yesrs old %s and weights %dkg.He uses %s" % (self.name, self.age, self.sex, self.__weight,self.language))
if __name__ == '__main__':
# 例項化類
p = people('Mike', 25, 60, 'man')
print(p._people__weight)
q = Programmer('Mike', 25, 60, 'man', 'python')
p.information()
q.information()
執行,我們會發現報錯
AttributeError: 'Programmer' object has no attribute '_Programmer__weight'
如果要訪問,應該是這樣
class people:
name = '' # 定義基本屬性
age = 0
sex = ''
__weight = 0 # 定義私有屬性,私有屬性在類外部無法直接進行訪問
# 定義構造方法
def __init__(self, n, a, w ,s):
self.name = n
self.age = a
self.sex = s
self.__weight = w
self.__height = 180
def information(self):
print("%s is a %d yesrs old %s and weights %skg" % (self.name, self.age, self.sex, self.__weight))
class Programmer(people):
language = ''
def __init__(self,n, a, w, s, l):
# 呼叫父類的構函
people.__init__(self, n, a, w, s)
self.language = l
# 重寫父類的方法
def information(self):
print("%s is a %d yesrs old %s and weights %dkg.He uses %s" % (self.name, self.age, self.sex, self._people__weight,self.language))
if __name__ == '__main__':
# 例項化類
p = people('Mike', 25, 60, 'man')
print(p._people__weight)
q = Programmer('Mike', 25, 60, 'man', 'python')
p.information()
q.information()
Mike is a 25 yesrs old man and weights 60kg
Mike is a 25 yesrs old man and weights 60kg.He uses python
如果我們訪問非私有屬性
class people:
name = '' # 定義基本屬性
age = 0
sex = ''
__weight = 0 # 定義私有屬性,私有屬性在類外部無法直接進行訪問
# 定義構造方法
def __init__(self, n, a, w ,s):
self.name = n
self.age = a
self.sex = s
self.__weight = w
self.__height = 180
def information(self):
print("%s is a %d yesrs old %s and weights %skg" % (self.name, self.age, self.sex, self.__weight))
class Programmer(people):
language = ''
def __init__(self,n, a, w, s, l):
# 呼叫父類的構函
people.__init__(self, n, a, w, s)
self.language = l
# 重寫父類的方法
def information(self):
print("%s is a %d yesrs old %s and weights %skg.He uses %d" % (self.name, self.age, self.sex, self.__weight,self.language))
if __name__ == '__main__':
# 例項化類
p = people('Mike', 25, 60, 'man')
print(p._people__weight)
q = Programmer('Mike', 25, 60, 'man', 'python')
p.information()
#print(q._Programmer__height)
print(q.name)
自然是可以的
60
Mike is a 25 yesrs old man and weights 60kg
Mike
如果我們不繼承weight
class people:
name = '' # 定義基本屬性
age = 0
sex = ''
__weight = 0 # 定義私有屬性,私有屬性在類外部無法直接進行訪問
# 定義構造方法
def __init__(self, n, a, w ,s):
self.name = n
self.age = a
self.sex = s
self.__weight = w
self.__height = 180
def information(self):
print("%s is a %d yesrs old %s and weights %skg" % (self.name, self.age, self.sex, self.__weight))
class Programmer(people):
language = ''
def __init__(self,n, a, s, l):
# 呼叫父類的構函
people.__init__(self, n, a, s, l)
self.language = l
# 重寫父類的方法
def information(self):
print("%s is a %d yesrs old %s and he uses %s" % (self.name, self.age, self.sex,self.language))
if __name__ == '__main__':
# 例項化類
p = people('Mike', 25, 60, 'man')
print(p._people__weight)
q = Programmer('Mike', 25, 'man', 'python')
p.information()
#print(q._Programmer__height)
print(q.name)
q.information()
60
Mike is a 25 yesrs old man and weights 60kg
Mike
Mike is a 25 yesrs old python and he uses python
對於私有屬性,我們雖然繼承了,但是可以用不上
class people:
name = '' # 定義基本屬性
age = 0
sex = ''
__weight = 0 # 定義私有屬性,私有屬性在類外部無法直接進行訪問
# 定義構造方法
def __init__(self, n, a, w ,s, h):
self.name = n
self.age = a
self.__weight = w
self.sex = s
self.height = h
def information(self):
print("%s is a %d yesrs old %s and weights %skg" % (self.name, self.age, self.sex, self.__weight))
print(self.sex)
class Programmer(people):
language = ''
def __init__(self, n, a, w, s, h, l):
# 呼叫父類的構函
people.__init__(self, n, a, w, s, h)
self.language = l
# 重寫父類的方法
def information(self):
print("%s is a %d yesrs old %s and he is %d tall and he uses %s" % (self.name, self.age, self.sex,self.height,self.language))
if __name__ == '__main__':
# 例項化類
q = Programmer('Mike',25,60,'man',170,'python')
q.information()
Mike is a 25 yesrs old man and he is 170 tall and he uses python
可以看到,雖然我們繼承了weight,引數也給了weight,但是我們並沒有用到,如果想要訪問weight這個私有屬性呢
class people:
name = '' # 定義基本屬性
age = 0
sex = ''
__weight = 0 # 定義私有屬性,私有屬性在類外部無法直接進行訪問
# 定義構造方法
def __init__(self, n, a, w ,s, h):
self.name = n
self.age = a
self.__weight = w
self.sex = s
self.height = h
def information(self):
print("%s is a %d yesrs old %s and weights %skg" % (self.name, self.age, self.sex, self.__weight))
print(self.sex)
class Programmer(people):
language = ''
def __init__(self, n, a, w, s, h,l):
# 呼叫父類的構函
people.__init__(self, n, a, w, s, h)
self.language = l
# 重寫父類的方法
def information(self):
print("%s is a %d yesrs old %s and he is %d tall and he uses %s,weights %dkg" % (self.name, self.age, self.sex,self.height,self.language,self._people__weight))
if __name__ == '__main__':
# 例項化類
q = Programmer('Mike',25,60,'man',170,'python')
q.information()
然後可以看到
Mike is a 25 yesrs old man and he is 170 tall and he uses python,weights 60kg
如果我們想直接通過子類訪問父類的私有屬性呢?也可以
class people:
name = '' # 定義基本屬性
age = 0
sex = ''
__weight = 0 # 定義私有屬性,私有屬性在類外部無法直接進行訪問
# 定義構造方法
def __init__(self, n, a, w ,s, h):
self.name = n
self.age = a
self.__weight = w
self.sex = s
self.height = h
def information(self):
print("%s is a %d yesrs old %s and weights %skg" % (self.name, self.age, self.sex, self.__weight))
print(self.sex)
class Programmer(people):
language = ''
def __init__(self, n, a, w, s, h,l):
# 呼叫父類的構函
people.__init__(self, n, a, w, s, h)
self.language = l
# 重寫父類的方法
def information(self):
print("%s is a %d yesrs old %s and he is %d tall and he uses %s,weights %dkg" % (self.name, self.age, self.sex,self.height,self.language,self._people__weight))
if __name__ == '__main__':
# 例項化類
q = Programmer('Mike',25,60,'man',170,'python')
q.information()
print(q._people__weight)
Mike is a 25 yesrs old man and he is 170 tall and he uses python,weights 60kg
60
再來看一個例子,是我看到某位博主的貼一下他的程式碼
class Person(object):
# 建構函式
def __init__(self, name):
self.name = name
self.__age = 18
obj = Person("lily")
print(obj.name)
class Student(Person):
def __init__(self):
self.__gender = 'male'
stu = Student()
print(stu._Student__gender)
print(stu._Person__age)
這裡他當然無法訪問age,因為沒有繼承父類的建構函式,應該像這樣
class Person(object):
# 建構函式
def __init__(self, name):
self.name = name
self.__age = 18
obj = Person("lily")
print(obj.name)
class Student(Person):
def __init__(self,name):
Person.__init__(self,name)
self.__gender = 'male'
stu = Student('lily')
print(stu._Student__gender)
print(stu.name)
print(stu._Person__age)
這個很容易使人產生困惑,今天差點沒把我繞死,以上的例子都是很經典的,我精心設計的,希望大家能學到一點東西
本來是想寫類的繼承的,寫到私有屬性上去了。。。
做學問呢要嚴謹,一絲不苟,我會對我的每一行程式碼負責,絕不復制貼上,用心給大家講解