PoEdu培训 Python班 第九课 Python 面向对象编程 类的基础用法
文章类别: 培训笔记 0 评论

PoEdu培训 Python班 第九课 Python 面向对象编程 类的基础用法

文章类别: 培训笔记 0 评论

Python 面向对象编程 类的基础用法

本文的Python知识基于 Python3

class 类

一个类的简单实例:

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__方法

>>> 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
>>> 

类的其他方法

>>> 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
>>> 

类的继承

>>> # 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__方法

>>> class animal(object):
...     def __init__(self):
...         print("Animal init...")
...     def __len__(self):
...         print("__len__被调用")
...         return 100
... 
>>> ani = animal()
Animal init...
>>> len(ani)
__len__被调用
100
>>> 

未完待续...

如有错误,请提出指正!谢谢.

回复