python3的私有属性和继承

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/ssjdoudou/article/details/83545438

写在最前面:从最简单讲起

先看一个最简单的类的例子


 
 
  1. class people:
  2. name = '' # 定义基本属性
  3. age = 0
  4. sex = ''
  5. __weight = 0 # 定义私有属性,私有属性在类外部无法直接进行访问
  6. # 定义构造方法
  7. def __init__(self, n, a, w ,s):
  8. self.name = n
  9. self.age = a
  10. self.sex = s
  11. self.__weight = w
  12. def infomation(self):
  13. print( "%s is a %d yesrs old %s and weights %skg" % ( self.name, self.age, self.sex, self.__weight))
  14. if __name_ _ == '__main__':
  15. # 实例化类
  16. p = people( 'Mike', 25, 60, 'man')
  17. p.infomation()

简单的不能再简单了

Mike is a 25 yesrs old man and weights 60kg

 
 

当我试图在类外直接访问私有属性


 
 
  1. class people:
  2. name = '' # 定义基本属性
  3. age = 0
  4. sex = ''
  5. __weight = 0 # 定义私有属性,私有属性在类外部无法直接进行访问
  6. # 定义构造方法
  7. def __init__(self, n, a, w ,s):
  8. self.name = n
  9. self.age = a
  10. self.sex = s
  11. self.__weight = w
  12. def information(self):
  13. print( "%s is a %d yesrs old %s and weights %skg" % ( self.name, self.age, self.sex, self.__weight))
  14. if __name_ _ == '__main__':
  15. # 实例化类
  16. p = people( 'Mike', 25, 60, 'man')
  17. print(p.__weight)

 会报错

AttributeError: 'people' object has no attribute '__weight'
 
 

说明weight作为私有属性,不能直接访问

那有没有可能绕过去呢,可以

为了方便描述,我们给prople类再加一个私有属性height并赋值180,然后我们试图访问__height


 
 
  1. class people:
  2. name = '' # 定义基本属性
  3. age = 0
  4. sex = ''
  5. __weight = 0 # 定义私有属性,私有属性在类外部无法直接进行访问
  6. # 定义构造方法
  7. def __init__(self, n, a, w ,s):
  8. self.name = n
  9. self.age = a
  10. self.sex = s
  11. self.__weight = w
  12. self.__height = 180
  13. def information(self):
  14. print( "%s is a %d yesrs old %s and weights %skg" % ( self.name, self.age, self.sex, self.__weight))
  15. class Programmer(people):
  16. language = ''
  17. def __init__(self,n, a, w, s, l):
  18. # 调用父类的构函
  19. people.__init_ _( self, n, a, w, s)
  20. self.language = l
  21. # 重写父类的方法
  22. def information(self):
  23. print( "%s is a %d yesrs old %s and weights %skg.He uses %d" % ( self.name, self.age, self.sex, self.__weight, self.language))
  24. if __name_ _ == '__main__':
  25. # 实例化类
  26. p = people( 'Mike', 25, 60, 'man')
  27. print(p.__height)
AttributeError: 'people' object has no attribute '__height'
 
 

很显然,不可以


 
 
  1. class people:
  2. name = '' # 定义基本属性
  3. age = 0
  4. sex = ''
  5. __weight = 0 # 定义私有属性,私有属性在类外部无法直接进行访问
  6. # 定义构造方法
  7. def __init__(self, n, a, w ,s):
  8. self.name = n
  9. self.age = a
  10. self.sex = s
  11. self.__weight = w
  12. self.__height = 180
  13. if __name_ _ == '__main__':
  14. # 实例化类
  15. p = people( 'Mike', 25, 60, 'man')
  16. print(p._people__height)

诶,这样就可以了

180
 
 

 

 然后我们创造一个Programmer来继承这个类的所有属性,包括私有属性然后又加了一个language的非私有属性


 
 
  1. class people:
  2. name = '' # 定义基本属性
  3. age = 0
  4. sex = ''
  5. __weight = 0 # 定义私有属性,私有属性在类外部无法直接进行访问
  6. # 定义构造方法
  7. def __init__(self, n, a, w ,s):
  8. self.name = n
  9. self.age = a
  10. self.sex = s
  11. self.__weight = w
  12. self.__height = 180
  13. def information(self):
  14. print( "%s is a %d yesrs old %s and weights %skg" % ( self.name, self.age, self.sex, self.__weight))
  15. class Programmer(people):
  16. language = ''
  17. def __init__(self,n, a, w, s, l):
  18. # 调用父类的构函
  19. people.__init_ _( self, n, a, w, s)
  20. self.language = l
  21. # 重写父类的方法
  22. def information(self):
  23. print( "%s is a %d yesrs old %s and weights %dkg.He uses %s" % ( self.name, self.age, self.sex, self.__weight, self.language))
  24. if __name_ _ == '__main__':
  25. # 实例化类
  26. p = people( 'Mike', 25, 60, 'man')
  27. print(p._people__weight)
  28. q = Programmer( 'Mike', 25, 60, 'man', 'python')
  29. p.information()
  30. q.information()

运行,我们会发现报错

AttributeError: 'Programmer' object has no attribute '_Programmer__weight'

 
 

 

如果要访问,应该是这样


 
 
  1. class people:
  2. name = '' # 定义基本属性
  3. age = 0
  4. sex = ''
  5. __weight = 0 # 定义私有属性,私有属性在类外部无法直接进行访问
  6. # 定义构造方法
  7. def __init__(self, n, a, w ,s):
  8. self.name = n
  9. self.age = a
  10. self.sex = s
  11. self.__weight = w
  12. self.__height = 180
  13. def information(self):
  14. print( "%s is a %d yesrs old %s and weights %skg" % ( self.name, self.age, self.sex, self.__weight))
  15. class Programmer(people):
  16. language = ''
  17. def __init__(self,n, a, w, s, l):
  18. # 调用父类的构函
  19. people.__init_ _( self, n, a, w, s)
  20. self.language = l
  21. # 重写父类的方法
  22. def information(self):
  23. 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))
  24. if __name_ _ == '__main__':
  25. # 实例化类
  26. p = people( 'Mike', 25, 60, 'man')
  27. print(p._people__weight)
  28. q = Programmer( 'Mike', 25, 60, 'man', 'python')
  29. p.information()
  30. q.information()

 
 
  1. Mike is a 25 yesrs old man and weights 60kg
  2. Mike is a 25 yesrs old man and weights 60kg.He uses python

 

如果我们访问非私有属性


 
 
  1. class people:
  2. name = '' # 定义基本属性
  3. age = 0
  4. sex = ''
  5. __weight = 0 # 定义私有属性,私有属性在类外部无法直接进行访问
  6. # 定义构造方法
  7. def __init__(self, n, a, w ,s):
  8. self.name = n
  9. self.age = a
  10. self.sex = s
  11. self.__weight = w
  12. self.__height = 180
  13. def information(self):
  14. print( "%s is a %d yesrs old %s and weights %skg" % ( self.name, self.age, self.sex, self.__weight))
  15. class Programmer(people):
  16. language = ''
  17. def __init__(self,n, a, w, s, l):
  18. # 调用父类的构函
  19. people.__init_ _( self, n, a, w, s)
  20. self.language = l
  21. # 重写父类的方法
  22. def information(self):
  23. print( "%s is a %d yesrs old %s and weights %skg.He uses %d" % ( self.name, self.age, self.sex, self.__weight, self.language))
  24. if __name_ _ == '__main__':
  25. # 实例化类
  26. p = people( 'Mike', 25, 60, 'man')
  27. print(p._people__weight)
  28. q = Programmer( 'Mike', 25, 60, 'man', 'python')
  29. p.information()
  30. #print(q._Programmer__height)
  31. print(q.name)

自然是可以的


 
 
  1. 60
  2. Mike is a 25 yesrs old man and weights 60kg
  3. Mike

如果我们不继承weight


 
 
  1. class people:
  2. name = '' # 定义基本属性
  3. age = 0
  4. sex = ''
  5. __weight = 0 # 定义私有属性,私有属性在类外部无法直接进行访问
  6. # 定义构造方法
  7. def __init__(self, n, a, w ,s):
  8. self.name = n
  9. self.age = a
  10. self.sex = s
  11. self.__weight = w
  12. self.__height = 180
  13. def information(self):
  14. print( "%s is a %d yesrs old %s and weights %skg" % ( self.name, self.age, self.sex, self.__weight))
  15. class Programmer(people):
  16. language = ''
  17. def __init__(self,n, a, s, l):
  18. # 调用父类的构函
  19. people.__init_ _( self, n, a, s, l)
  20. self.language = l
  21. # 重写父类的方法
  22. def information(self):
  23. print( "%s is a %d yesrs old %s and he uses %s" % ( self.name, self.age, self.sex, self.language))
  24. if __name_ _ == '__main__':
  25. # 实例化类
  26. p = people( 'Mike', 25, 60, 'man')
  27. print(p._people__weight)
  28. q = Programmer( 'Mike', 25, 'man', 'python')
  29. p.information()
  30. #print(q._Programmer__height)
  31. print(q.name)
  32. q.information()

 
 
  1. 60
  2. Mike is a 25 yesrs old man and weights 60kg
  3. Mike
  4. Mike is a 25 yesrs old python and he uses python

对于私有属性,我们虽然继承了,但是可以用不上


 
 
  1. class people:
  2. name = '' # 定义基本属性
  3. age = 0
  4. sex = ''
  5. __weight = 0 # 定义私有属性,私有属性在类外部无法直接进行访问
  6. # 定义构造方法
  7. def __init__(self, n, a, w ,s, h):
  8. self.name = n
  9. self.age = a
  10. self.__weight = w
  11. self.sex = s
  12. self.height = h
  13. def information(self):
  14. print( "%s is a %d yesrs old %s and weights %skg" % ( self.name, self.age, self.sex, self.__weight))
  15. print( self.sex)
  16. class Programmer(people):
  17. language = ''
  18. def __init__(self, n, a, w, s, h, l):
  19. # 调用父类的构函
  20. people.__init_ _( self, n, a, w, s, h)
  21. self.language = l
  22. # 重写父类的方法
  23. def information(self):
  24. 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))
  25. if __name_ _ == '__main__':
  26. # 实例化类
  27. q = Programmer( 'Mike', 25, 60, 'man', 170, 'python')
  28. q.information()
Mike is a 25 yesrs old man and he is 170 tall and he uses python
 
 

可以看到,虽然我们继承了weight,参数也给了weight,但是我们并没有用到,如果想要访问weight这个私有属性呢


 
 
  1. class people:
  2. name = '' # 定义基本属性
  3. age = 0
  4. sex = ''
  5. __weight = 0 # 定义私有属性,私有属性在类外部无法直接进行访问
  6. # 定义构造方法
  7. def __init__(self, n, a, w ,s, h):
  8. self.name = n
  9. self.age = a
  10. self.__weight = w
  11. self.sex = s
  12. self.height = h
  13. def information(self):
  14. print( "%s is a %d yesrs old %s and weights %skg" % ( self.name, self.age, self.sex, self.__weight))
  15. print( self.sex)
  16. class Programmer(people):
  17. language = ''
  18. def __init__(self, n, a, w, s, h,l):
  19. # 调用父类的构函
  20. people.__init_ _( self, n, a, w, s, h)
  21. self.language = l
  22. # 重写父类的方法
  23. def information(self):
  24. 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))
  25. if __name_ _ == '__main__':
  26. # 实例化类
  27. q = Programmer( 'Mike', 25, 60, 'man', 170, 'python')
  28. q.information()

然后可以看到

Mike is a 25 yesrs old man and he is 170 tall and he uses python,weights 60kg
 
 

如果我们想直接通过子类访问父类的私有属性呢?也可以


 
 
  1. class people:
  2. name = '' # 定义基本属性
  3. age = 0
  4. sex = ''
  5. __weight = 0 # 定义私有属性,私有属性在类外部无法直接进行访问
  6. # 定义构造方法
  7. def __init__(self, n, a, w ,s, h):
  8. self.name = n
  9. self.age = a
  10. self.__weight = w
  11. self.sex = s
  12. self.height = h
  13. def information(self):
  14. print( "%s is a %d yesrs old %s and weights %skg" % ( self.name, self.age, self.sex, self.__weight))
  15. print( self.sex)
  16. class Programmer(people):
  17. language = ''
  18. def __init__(self, n, a, w, s, h,l):
  19. # 调用父类的构函
  20. people.__init_ _( self, n, a, w, s, h)
  21. self.language = l
  22. # 重写父类的方法
  23. def information(self):
  24. 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))
  25. if __name_ _ == '__main__':
  26. # 实例化类
  27. q = Programmer( 'Mike', 25, 60, 'man', 170, 'python')
  28. q.information()
  29. print(q._people__weight)

 
 
  1. Mike is a 25 yesrs old man and he is 170 tall and he uses python,weights 60kg
  2. 60

再来看一个例子,是我看到某位博主的贴一下他的代码


 
 
  1. class Person(object):
  2. # 构造函数
  3. def __init__(self, name):
  4. self.name = name
  5. self.__age = 18
  6. obj = Person( "lily")
  7. print(obj.name)
  8. class Student(Person):
  9. def __init__(self):
  10. self.__gender = 'male'
  11. stu = Student()
  12. print(stu._Student__gender)
  13. print(stu._Person__age)

这里他当然无法访问age,因为没有继承父类的构造函数,应该像这样


 
 
  1. class Person(object):
  2. # 构造函数
  3. def __init__(self, name):
  4. self.name = name
  5. self.__age = 18
  6. obj = Person( "lily")
  7. print(obj.name)
  8. class Student(Person):
  9. def __init__(self,name):
  10. Person.__init__(self,name)
  11. self.__gender = 'male'
  12. stu = Student( 'lily')
  13. print(stu._Student__gender)
  14. print(stu.name)
  15. print(stu._Person__age)

这个很容易使人产生困惑,今天差点没把我绕死,以上的例子都是很经典的,我精心设计的,希望大家能学到一点东西

本来是想写类的继承的,写到私有属性上去了。。。

做学问呢要严谨,一丝不苟,我会对我的每一行代码负责,绝不复制粘贴,用心给大家讲解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值