Python 面向对象编程基础
虽然Pthon是解释性语言,但是Pthon可以进行面向对象开发,小到 脚本程序,大到3D游戏,Python都可以做到。
一类:
语法: class 类名: 类属性,方法等;
如下我们创建一个 Person类
>>> class Person: #创建一个Person类
name = 'tom' #name是类属性,可以通过 Person.name访问,也可以通过对象.name访问
age = 18 #定义一个变量,年龄为 18
>>> p1 = Person();
>>> print(p1.name,Person.age)
tom 18
>>>
二:属性,
Python的属性可以分为 类属性,和对象属性;其中类属性是类和对象默认都可以访问的,但是对象属性只能对象访问;如代码
>>> class Student:
#创建学生类
name = 'lili'
sex = '女'
>>> stu1 = Student();
>>> stu1.address = 'JiNan'
>>> print(stu1.name,stu1.address)
lili JiNan
>>>
如上述代码:stu1对象,有个 address 属性;即是对象创建的属性只能通过对象访问,不能通过类访问
>>> print(Student.address)
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
print(Student.address)
AttributeError: type object 'Student' has no attribute 'address'
>>>
上面定义的属性,默认是对象可以访问,子类可以继承的,但是我们可以用 在类对象前面 加上 __ 定义私有属性成员变量
>>> class People:
name = 'mike' #默认可访问属性
__age = 19 #私有属性,外部不可访问
>>> p1 = People();
>>> print(p1.name,People)
mike <class '__main__.People'>
>>> print(p1.name,People.name)
mike mike
>>> print(p1.age,People.name)
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
print(p1.age,People.name)
AttributeError: 'People' object has no attribute 'age'
>>>
二:方法定义,
语法: def 方法名 (self) 至少会有一个参数,并且第一个参数是 self,其他参数,依次排列,最后加 :
>>> class Teacher:
name = "miss wang"
teach_type = 'php'
def getName(self):
return self.name
def setTeach_type(self,teach_type):
self.teach_type = teach_type
return
>>> t1 = Teacher()
>>> print(t1.name,t1.getName())
miss wang miss wang
>>> t1.setTeach_type('Python')
>>> print(t1.teach_type)
Python
>>>
构造方法和析构方法 __init__(self,...) __del__(self,...)
__init__ 构造方法,在生成对象时会默认调用;支持重载
__def__ 析构方法,在程序最后释放资源用;支持重载
>>> class People:
name = '' #声明name变量
def __init__(self,name):#构造方法初始化name
self.name = name
return
>>> p1 = People('Jerry')
>>> print(p1.name)
Jerry
>>>
三:对象属性覆盖类属性
在类中声明一个 name 属性,对象中声明一个 name 属性,那么对象属性会覆盖类属性 name 如代码
>>> class Student:
name='Buick' #类属性
>>> stu1 = Student();
>>> print(stu1.name)
Buick
>>> stu1.name= "TOM" #改变对象stu1.name 为 TOM
>>> print(stu1.name)
TOM
>>> print(Student.name) #类对象的name不变
Buick
>>>
四:类方法的创建
关键字: @classmethod 并且方法第一个参数一般以 cls
class Teacher:
name = 'miss jin'
def teachPhp(self):
print(self.name,"teach php")
return
@classmethod
def setName(cls,name):
cls.name = name
return
>>> t1 = Teacher()
>>> t1.teachPhp()
miss jin teach php
>>> Teacher.setName("Mickale")
>>> t1.teachPhp
<bound method Teacher.teachPhp of <__main__.Teacher object at 0x02318FB0>>
>>> t1.teachPhp()
Mickale teach php
>>> t1.setName("t1 set Name")
>>> t1.teachPhp()
t1 set Name teach php
>>> Teacher.teachPhp() #这里类无法调用非类方法
Traceback (most recent call last):
File "<pyshell#100>", line 1, in <module>
Teacher.teachPhp()
TypeError: teachPhp() missing 1 required positional argument: 'self'
>>>
五:静态方法
关键字: @staticmethod 不需要第一参数 self了
>>> class People:
name = 'Jerry'
@staticmethod
def getName():
return People.name
>>> print(People.getName())
Jerry
>>> p1 = People()
>>> print(p1.getName())
Jerry
>>>
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com