python实例化对象是什么意思

编程入门 行业动态 更新时间:2024-10-08 04:27:01

python<a href=https://www.elefans.com/category/jswz/34/1771375.html style=实例化对象是什么意思"/>

python实例化对象是什么意思

一切皆对象,类也是对象,类来自于元类type,如果一个类没有声明自己的元类,默认它就是元类。

即类是元类的实例,通过type(类)会显示type,而实例来自于类。

类有两个属性,数据属性和函数属性,下面是一个创建类和实例化对象的例子

classanimal:'This is class for animal' #类的说明

type='animal'

def __init__(self,name,sex,leg):

self.name=name

self.sex=sex

self.leg=legdefeat(self,food):print('%s likes to eat %s'%(self.name,food))defplay(self):print('%s is playing'%self.name)print(animal.__name__)#打印类的名称

print(animal.__doc__) #打印类的说明,__doc__属性不能继承给子类

print(animal.__dict__) #打印类的属性字典

cat=animal('cat','male',4)print(cat.__dict__) #打印类的属性字典

print(cat.type)

cat.eat('mouse')

cat.play()#执行结果#animal#This is class for animal#{'__module__': '__main__', '__doc__': 'This is class for animal', 'haveTeeth': True, '__init__': , 'eat': , '__dict__': , '__weakref__': }#{'name': 'cat', 'sex': 'male', 'leg': 4}#cat likes to eat mouse#animal

在class animal:范围下面的都是对animal类的定义,其中def __init__()是定义animal类的数据属性,__init__()不应该有返回值,否则会报错,其他函数则是animal类的函数属性,可以看到类下面的函数的第一个参数都是self。

当执行cat=animal('cat','male',4),触发animal类的__init__()函数生成一个cat实例。

__dict__表示属性字典,以字典形式存放,通过打印animal.__dict__和cat.__dict__可以发现,类有数据属性和函数属性,而实例只有数据属性没有函数属性,但是实例可以继承和调用对象的函数属性。

实例调用类的函数时,会自动将实例本身作为第一个参数传给函数,但是类自己调用函数时不会自动将实例本身作为参数传入,例如要通过类调用play()函数,则animal.play(cat)。

类的属性字典是共用的,而实例的属性字典是私有的。

类属性的查看、增加、修改和删除

#查看类的数据属性

print(animal.type)#修改类的数据属性

animal.type='Animal'

print(animal.type)#增加类的数据属性

animal.haveteeth=Trueprint(cat.haveteeth)#删除类的数据属性

delanimal.haveteeth#增加类的函数属性,修改类似

defplay_bal(self,ball):print('The %s is playing %s'%(self.name,ball))

animal.play_ball=play_bal

cat.play_ball('tennis')

实例属性的增加

#增加实例的数据属性,cat.__dict__['key']='value'也可以增加数据属性但不建议使用

cat.living='land'

print(cat.__dict__) #{'name': 'cat', 'sex': 'male', 'leg': 4, 'living': 'land'}#删除实例的数据属性

delcat.livingprint(cat.__dict__) #{'name': 'cat', 'sex': 'male', 'leg': 4}#修改实例的数据属性

cat.sex='female'

print(cat.sex) #female#增加实例的函数属性

deftest():print('hello')

cat.test=test

cat.test()#hello

对于实例增加的函数属性,实例在调用时不会自动将自身作为参数传入。

需要注意的是:

如果在类前面定义了与类的数据属性同名的全局变量,那么只要不通过类或者实例+.调用这个变量,这个变量就是指类前面定义的全局变量

如果实例增加了与类的数据属性同名的属性,相当于给实例增加了这个属性,对类的属性没有影响。

type='mouse'

classanimal:

type='animal'l=['a','b']def __init__(self,name):

self.name=nameprint(type) #此处的type是指全局变量,而不是类内部的数据属性变量

cat=animal('cat')

cat.type='male' #相当于给cat增加了一个数据属性type,对类的type属性没有影响

print(cat.__dict__)print(cat.type)

cat.l.append('c')#此处修改的是类的数据属性l,与通过=赋值(给实例增加属性)不同

__class__:实例来自哪个类

__module__:实例来自哪个模块

类的组合

即在类实例化时,将另一个类的实例作为参数传入,这样可以将两个实例关联起来。

当类之间有显著不同,并且较小的类是较大的类所需要的组件时,用组合比较好。

例如,描述一个机器人类,这个大类是由很多互不相关的小类组成,如机械胳膊类、腿类、电池类等。

当类之间有很多相同的属性,提取这些统统的属性做成基类,用继承比较好。

classcourse:def __init__(self,name,price,period,teacher):

self.name=name

self.price=price

self.period=period

self.teacher=teacherclassteacher:def __init__(self,name,sex):

self.name=name

self.sex=sexclassstudent:def __init__(self,name,age,course):

self.name=name

self.age=age

self.course=course

t1=teacher('Bob',38)

t2=teacher('Jack',45)

t3=teacher('Jane',45)

c1=course('python',6000,'3 months',t2)#将teacher作为参数传递给课程实例

c2=course('linux',7000,'5 months',t1)

c3=course('mysql',6500,'5 months',t3)

d={'1':c1,'2':c2,'3':c3}

name='Alice'age=21

whileTrue:

choice=input('please choice course:')if choice ind.keys():

s1=student(name,age,d[choice])#将课程实例作为参数传递给学生实例

print(s1.__dict__)print('%s choose course %s,%s is the teacher'%(s1.name,s1.course.name,s1.course.teacher.name))elif int(choice)==0:

exit(0)else:print('please input correct choice')

示例:类的组合

运行结果如下图

更多推荐

python实例化对象是什么意思

本文发布于:2024-02-06 12:29:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1748830.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实例   对象   python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!