#coding=utf-8
'''
1.私有变量不会被继承,父函数中如果使用私有变量,在子类没有重写此函数的情况下,函数中引用的私有变量总是父类的,
如果父类中没有初始化这个私有变量,则会报错
例如:
父类中的self.__taste变量将永远指向_Person__taste
def showperson(self):
print(self.name)
print(self._age)
print(self.__taste) #就算子类继承了这个函数,这个self指向的因为是私有的,所以总是父类的
子类中是无法使用父类的_Person__taste的,只能重写一个一样的函数,但是这个函数的self.__taste指向_Student__taste
'''
class Person(object):
def __init__(self, name, age, taste):
self.name = name
self._age = age
self.__taste = taste
def showperson(self):
print(self.name)
print(self._age)
print(self.__taste)
class Student(Person):
def construction(self, name, age, taste):
self.name = name
self._age = age
self.__taste = taste
def showperson(self):
print(self.name)
print(self._age)
print(self.__taste)
s1 = Student('jack', 25, 'football')
s1.showperson()
print('*'*20)
s1.construction('rose', 30, 'basketball')
s1.showperson()
print('*'*20)
s1.showstudent()
print('*'*20)
py继承相关
最新推荐文章于 2025-01-05 12:48:20 发布