标签:attribute line ase elf 消息 ror eth str 删除
如何使用类
一、实例化:创建对象
例子一
x=int(10) print(x)
python中一切皆为对象,且python3统一了类与类型的概念,类型就是类
>>> dict #类型dict就是类dict <class ‘dict‘> >>> d=dict(name=‘egon‘) #实例化 >>> d.pop(‘name‘) #向d发一条消息,执行d的方法pop。 ‘egon‘
例子二:
class Garen:
camp=‘Demacia‘
def attack(self):
print(‘attack‘)
obj=Garen() #实例化,实例就是对象
print(obj) #----> <__main__.Garen object at 0x0000000001E7CC18>
二、引用类的特征(类的变量)和技能(类的函数)
基于面向对象设计一个款游戏:英雄联盟,每个玩家选一个英雄,每个英雄都有自己的特征和和技能,
特征即数据属性,技能即方法属性,特征与技能的结合体就一个对象。
从一组对象中提取相似的部分就是类,类所有对象都具有的特征和技能的结合体
在python中,用变量表示特征,用函数表示技能,因而类是变量与函数的结合体,对象是变量与方法(指向类的函数)的结合体
class Garen:
camp=‘Demacia‘ #变量代表特征
def attack(self): #函数代表技能
print(‘attack‘)
print(Garen.camp)
---->Demacia
print(Garen.attack)
---> <function Garen.attack at 0x0000000001E7F158>
Garen.attack(123)
---->attack
如何使用实例
class Garen:
camp=‘Demacia‘ #变量,共有特征
def __init__(self,nickname):
self.nick=nickname #变量,独有特征
def attack(self,enemy): #函数,技能
# print(‘---->‘,self.nick) #g1.nick
print(‘%s attack %s‘ %(self.nick,enemy))
g1=Garen(‘草丛伦‘) #Garen.__init___(g1,‘草丛伦‘)
g2=Garen(‘猥琐轮‘)
print(g1.nick)
---->草丛伦
g1.attack(‘alex‘)
---->草丛伦 attack alex
print(g1.camp)
---->Demacia
print(g1.attack)
---> <bound method Garen.attack of <__main__.Garen object at 0x00000000027BCD30>>
print(Garen.attack)
----><function Garen.attack at 0x00000000021DF048>
class Student:
country=‘china‘
def __init__(self,ID,NAME,SEX,PROVINCE):
self.id=ID
self.name=NAME
self.sex=SEX
self.province=PROVINCE
def search_score(self):
print(‘tell score‘)
def study(self):
print(‘study‘,self)
s1=Student(‘123‘,‘egon‘,‘male‘,‘shandong‘)
print(Student.__init__(s1,‘123‘,‘egon‘,‘male‘,‘shandong‘)) #调用__init__函数
----> None
#调用对象的数据属性,对象的属性:对象本身就只有特征(变量)
s1.id=‘123‘ #调用对象的id属性
s1.name=‘egon‘ #调用对象的name属性
s1.sex=‘male‘ #调用对象的sex属性
s1.province=‘shandong‘ #调用对象的province属性
#查看函数
print(Student.country) #查看类的数据属性
---->china
print(Student.__init__)
----><function Student.__init__ at 0x0000000001E73B70>
print(Student.study)
----><function Student.study at 0x0000000001E7F2F0>
print(Student.search_score)
----><function Student.search_score at 0x000000000221F2F0>
Student.__init__(s1,‘123‘,‘egon‘,‘male‘,‘shandong‘)
Student.study(s1)
---->study <__main__.Student object at 0x00000000021EC470>
总结:
类:一:实例化,二:引用名字(类名.变量名,类名.函数名)
实例:引用名字(实例名.类的变量,实例名.绑定方法,实例名.实例自己的变量名)
变量的增删改查及及运算相加
#变量的增删改查
class Garen:
camp=‘Demacia‘
def __init__(self,nickname):
self.nick=nickname #g1.nick=‘草丛伦‘
def attack(self,enemy):
print(‘%s attack %s‘ %(self.nick,enemy))
print(Garen.camp) #查
---->Demacia
Garen.camp=‘aaaaaa‘ #改
print(Garen.camp)
---->aaaaaa
del Garen.camp #删除
print(Garen.camp)
#报错:----> AttributeError: type object ‘Garen‘ has no attribute ‘camp‘
Garen.x=1 #增
print(Garen.x)
---->1
g1=Garen(‘alex‘)
print(g1.nick)
---->alex
g1.nick=‘asb‘ #改
print(g1.nick)
---->asb
del g1.nick #删
print(g1.nick)
#报错:----> AttributeError: ‘Garen‘ object has no attribute ‘nick‘
g1.sex=‘female‘ #增
print(g1.sex)
---->female
Garen.x=1
Garen.y=2
Garen.res=Garen.x+Garen.y
print(Garen.res=Garen.x+Garen.y)
---->3
python3中所有的类都是新式类
class A:pass print(A.__bases__) #__bases__查看继承 ---->(<class ‘object‘>,),继承是object的都是新式类
python2中分为新式类和经典类
新式类和经典类声明的最大不同在于,所有新式类必须继承至少一个父类
所有类甭管是否显式声明父类,都有一个默认继承object父类
python2中新式类 class B(object):pass class C(B):pass print(B.__bases__) #__bases__查看继承 ---->(<class ‘object‘>,) print(C.__bases__) ---->(<class ‘__main__.B‘>,)
#python2中经典类 class D:pass print(D.__bases__) ---->()
在 python3中上面的新式类和经典类都是新式类
类的名称空间与对象的名称空间
class Student:
country = ‘China‘
def __init__(self, ID, NAME, SEX, PROVINCE):
self.id = ID
self.name = NAME
self.sex = SEX
self.province = PROVINCE
def search_score(self):
print(‘tell score‘)
def study(self): #self=s1
print(‘study‘,self)
def walk(self):
print(‘name:%s is walking‘ %self.name)
s1 = Student(‘123‘, ‘cobila‘, ‘female‘, ‘shanxi‘)
s2 = Student(‘124‘, ‘cobilamei‘, ‘femaleasfd‘, ‘shasdfanxi‘)
s1.walk()
---->name:cobila is walking
s2.walk()
---->name:cobilamei is walking
print(Student.__dict__) #__dict__,查看名称空间,查看类的名称空间 ---->{‘__module__‘: ‘__main__‘, ‘country‘: ‘China‘, ‘__init__‘: <function Student.__init__ at 0x00000000021D3A60>, ‘search_score‘: <function Student.search_score at 0x00000000021DF048>, ‘study‘: <function Student.study at 0x00000000021DF2F0>, ‘walk‘: <function Student.walk at 0x00000000021DF158>, ‘__dict__‘: <attribute ‘__dict__‘ of ‘Student‘ objects>, ‘__weakref__‘: <attribute ‘__weakref__‘ of ‘Student‘ objects>, ‘__doc__‘: None} print(s1.__dict__) #__dict__,查看名称空间,查看对象的名称空间 {‘id‘: ‘123‘, ‘name‘: ‘cobila‘, ‘sex‘: ‘female‘, ‘province‘: ‘shanxi‘} print(s1.id) ---->123 s1.country="321" print(id(s1.country)) ---->42061192 print(s2.id) ---->124 s2.country="421" print(id(s2.country)) ---->42061248
#绑定方法,绑定方法的核心在于“绑定”,唯一绑定一个确定的对象
print(s1.study,id(s1.study))
----><bound method Student.study of <__main__.Student object at 0x000000000284CEB8>> 5139208
print(Student.study,id(Student.study))
----><function Student.study at 0x000000000284F2F0> 42267376
对象之间的交互
class Riven:
camp=‘Noxus‘ #所有玩家的英雄(锐雯)的阵营都是Noxus;
def __init__(self,nickname,aggressivity=54,life_value=414): #英雄的初始攻击力54;
self.nickname=nickname #为自己的锐雯起个别名;
self.aggressivity=aggressivity #英雄都有自己的攻击力;
self.life_value=life_value #英雄都有自己的生命值;
def attack(self,enemy): #普通攻击技能,enemy是敌人;
enemy.life_value-=self.aggressivity #根据自己的攻击力,攻击敌人就减掉敌人的生命值。
class Garen:
camp=‘Noxus‘ #所有玩家的英雄(锐雯)的阵营都是Noxus;
def __init__(self,nickname,aggressivity=54,life_value=414): #英雄的初始攻击力54;
self.nickname=nickname #为自己的锐雯起个别名;
self.aggressivity=aggressivity #英雄都有自己的攻击力;
self.life_value=life_value #英雄都有自己的生命值;
def attack(self,enemy): #普通攻击技能,enemy是敌人;
enemy.life_value-=self.aggressivity #根据自己的攻击力,攻击敌人就减掉敌人的生命值。
# 对象之间的交互
r1=Riven(‘芮雯雯‘)
g1=Garen(‘草丛轮‘)
print(r1.life_value)
---->414
g1.attack(r1)
print(r1.life_value)
---->360
name=input(‘your nickname: ‘)
r1=Riven(name)
面向对象的编程思路
#应用场景
#找不到共同特征和技能不用强求
#对象:学校----->归类
#共有的特征:商标为etiantian
#共有的技能:招生
#独有的特征:地址不一样,老师们,课程
class School:
tag=‘etiantian‘
def __init__(self,addr):
self.addr=addr
self.teacher_list=[]
self.course_list=[]
def zhaosheng(self):
pass
#对象:老师---->归类
#共同的技能:教课
#独有的特征:名字,性别,level,课程
class Teacher:
def __init__(self,name,sex,level):
self.name=name
self.sex=sex
self.level=level
self.course_list=[]
def teach(self):
pass
#对象:学生---->归类
#共同的特征:
#共同的技能:search_score,handin
#独有的特征:学号,名字,性别,课程
class Student:
def __init__(self,ID,name,sex):
self.id=ID
self.name=name
self.sex=sex
self.course_list=[]
def search_score(self):
pass
def handin(self):
pass
标签:attribute line ase elf 消息 ror eth str 删除
原文地址:http://www.cnblogs.com/xuyaping/p/6723862.html