写在最前面:从最简单讲起
先看一个最简单的类的例子
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)
这个很容易使人产生困惑,今天差点没把我绕死,以上的例子都是很经典的,我精心设计的,希望大家能学到一点东西
本来是想写类的继承的,写到私有属性上去了。。。
做学问呢要严谨,一丝不苟,我会对我的每一行代码负责,绝不复制粘贴,用心给大家讲解