Python 面向对象编程 类的基础用法
本文的Python知识基于 Python3
class 类
- 以
class关键字开头 - 一个参数为
object
一个类的简单实例:
class class_demo(object):
pass
c = class_demo()
print(type(c))
c.name = 'root' # 绑定变量
print(c.name)
c.pwd = 'root' # 绑定变量
print(c.pwd)
# ----------------执行结果---------------
>>> class class_demo(object):
... pass
...
>>> c = class_demo()
>>> print(type(c))
<class '__main__.class_demo'>
>>> c.name = 'root' # 绑定变量
>>> print(c.name)
root
>>> c.pwd = 'root' # 绑定变量
>>> print(c.pwd)
root
>>> 注意, 以上代码绑定的变量只存在于当前对象中
__init__方法
- 这个方法可以初始化类的一些参数
- 它的
第一个参数是固定的self - 其他的可以有任意个参数
>>> class user_info(object):
... def __init__(self, name, pwd, salary, money):
... self.name = name
... self.pwd = pwd
... self.salary = salary
... self.money = money
...
>>> user = user_info()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: __init__() missing 4 required positional arguments: 'name', 'pwd', 'salary', and 'money'
>>> user = user_info('root', 'rrr', 1000, 500)
>>> user.name
'root'
>>> user.pwd
'rrr'
>>> user.salary
1000
>>> user.money
500
>>> 类的其他方法
- 类中还可以有其他的方法
- 类中的方法的第一个参数都必须是
self, 代表当前对象本身 - 但是
self的名字不是固定的, self不是关键字, 只是一种约定
>>> class user_info(object):
... def __init__(self, name, pwd, salary, money):
... self.name = name
... self.pwd = pwd
... self.salary = salary
... self.money = money
...
... def print(self):
... print("Name:%s Salary:%d Money:%d" % (self.name, self.salary, self.money))
...
>>> user = user_info('root', 'rrr', 1000, 500)
>>> user.name
'root'
>>> user.pwd
'rrr'
>>> user.salary
1000
>>> user.money
500
>>> user.print()
Name:root Salary:1000 Money:500
>>> 类的权限控制
- 在类的变量前边加上
两个下划线__ - 加上
__后, 外部不能通过常规手段进行访问
>>> class user_info(object):
... def __init__(self, name, pwd, salary, money):
... self.name = name
... self.__pwd = pwd
... self.salary = salary
... self.__money = money
...
... def print(self):
... print("Name:%s Salary:%d Money:%d" % (self.name, self.salary, self.__money))
...
>>> user = user_info('root', 'rrr', 1000, 500)
>>> user.name
'root'
>>> user.__pwd
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'user_info' object has no attribute '__pwd'
>>> user.salary
1000
>>> user.__money
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'user_info' object has no attribute '__money'
>>> user.print()
Name:root Salary:1000 Money:500
>>> 那么我们怎么进行访问呢?
我们可以提供get和set的方法
>>> class user_info(object):
... def __init__(self, name, pwd, salary, money):
... self.name = name
... self.__pwd = pwd
... self.salary = salary
... self.__money = money
...
... def print(self):
... print("Name:%s Salary:%d Money:%d" % (self.name, self.salary, self.__money))
...
... def get_money(self):
... return self.__money
...
... def set_money(self, val):
... if input("输入超级管理员密码:") == '123':
... self.__money = val
...
>>> user = user_info('root', 'rrr', 1000, 500)
>>> user.name
'root'
>>> user.__pwd
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'user_info' object has no attribute '__pwd'
>>> user.salary
1000
>>> user.get_money()
500
>>> user.set_money(600)
输入超级管理员密码:
111
>>> user.print()
Name:root Salary:1000 Money:500
>>> user.set_money(600)
输入超级管理员密码:
123
>>> user.print()
Name:root Salary:1000 Money:600
>>> 非常规访问class的私有变量
直接看效果吧
>>> class user_info(object):
... def __init__(self, name, pwd, salary, money):
... self.name = name
... self.__pwd = pwd
... self.salary = salary
... self.__money = money
...
... def print(self):
... print("Name:%s Salary:%d Money:%d" % (self.name, self.salary, self.__money))
...
... def get_money(self):
... return self.__money
...
... def set_money(self, val):
... if input("输入超级管理员密码:") == '123':
... self.__money = val
...
>>> user = user_info('root', 'rrr', 1000, 500)
>>> user.name
'root'
>>> user.__pwd
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'user_info' object has no attribute '__pwd'
>>> user._user_info__pwd
'rrr'
>>> user.salary
1000
>>> user.get_money()
500
>>> user.print()
Name:root Salary:1000 Money:500
>>> user._user_info__money
500
>>> user._user_info__money = 1000
>>> user.print()
Name:root Salary:1000 Money:1000
>>> 类的继承
- Python中, class的后边会有一个
object, 表示从object类继承 object是Python的所有类的基类__init__实际上就是从object继承来的
>>> # animal类继承自object
... class animal(object):
... def eat(self):
... print("Animal eat...")
...
... pass
...
... # pig类继承自animal, 也拥有object的东西
...
>>> class pig(animal):
... pass
...
... # dog类继承自animal, 也拥有object的东西
...
>>> class dog(animal):
... # 重写eat
... def eat(self):
... print("Dog eat Wang Wang Wang....")
... pass
...
>>> def main():
... xx = pig()
... xx.eat()
... zz = dog()
... zz.eat()
...
... print(isinstance(xx, pig))
... print(isinstance(xx, animal))
... print(isinstance(xx, dog))
... print(isinstance(zz, pig))
... print(isinstance(zz, animal))
... print(isinstance(zz, dog))
...
>>> main()
Animal eat...
Dog eat Wang Wang Wang....
True
True
False
False
True
True
>>> object的各种__xxx__方法
object中的 xx 方法是有意义的
- 比如
__init__表示初始化对象的时候进行调用 __len__是早len()函数的时候调用- .....
- 比如
>>> class animal(object):
... def __init__(self):
... print("Animal init...")
... def __len__(self):
... print("__len__被调用")
... return 100
...
>>> ani = animal()
Animal init...
>>> len(ani)
__len__被调用
100
>>> 未完待续...
如有错误,请提出指正!谢谢.
本文由 花心胡萝卜 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: 2017-07-22 at 04:00 am