一:类和对象
1.定义类,类的实例化,类方法调用
class Turtle:#python中类名约定以大写字母开头 #属性 color:'green' weight:10 legs:4 shell=True mouth:'大嘴' #方法 def climb(self): print('我正在很努力的向前爬') def run(self): print('我正在得快的跑') def eat(self): print('吃东西啦') tt=Turtle() tt.climb()
2.继承
MyList继承列表list
class MyList(list): pass list2=MyList() list2.append(2) list2.append(1) list2.append(9) for each in list2: print(each)
3.多态
class A: def fun(self): print('我是小A') class B: def fun(self): print('我是小B') a=A() b=B() a.fun() b.fun()
4.self参数指代对象自身,相当于this
class Ball(): def setName(self,name): self.name=name def kick(self): print('我叫%s' %self.name) a=Ball() a.setName('土豆') a.kick()
5.公有和私有(伪私有)
class Person: name='小甲鱼' >>> p=Person() >>> p.name '小甲鱼' >>> class Person: __name='小甲鱼' >>> p=Person() >>> p.name Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> p.name AttributeError: 'Person' object has no attribute 'name' >>> class Person: __name='小甲鱼' def getName(self): return self.__name >>> p=Person() >>> p.getName() '小甲鱼' >>> p._Person__name '小甲鱼'
6._init_(self)构造方法,即构造函数
class Ball: def __init__(self,name): self.name=name def kick(self): print('我叫%s' %self.name) b=Ball('土豆') b.kick()
7.继承
>>> import random as r >>> class Fish: def __init__(self): self.x=r.randint(0,10) slef.y=r.randint(0,10) def move(self): self.x-=1 print('我的位置是:',self.x,self.y) >>> class Goldfish(Fish): pass >>> class Shark(Fish): def __init__(self): self.hungry=True def eat(self): if self.hungry: print('吃东西啦') self.hungry=False else: print('太撑了,吃不下啦') >>> fish=Fish() >>> fish.move() 我的位置是: 1 7 >>> fish.move() 我的位置是: 0 7 >>> goldfish=GoldFish() >>> goldfish.move() 我的位置是: 5 3 >>> shark=Shark() >>> shark.eat() 吃东西啦 >>> shark.eat() 太撑了,吃不下啦 >>> shark.move() Traceback (most recent call last): File "<pyshell#39>", line 1, in <module> shark.move() File "<pyshell#23>", line 6, in move self.x-=1 AttributeError: 'Shark' object has no attribute 'x'
上面例子中shark.move()方法调用出错,因为没有继承父类中的变量。
如果想不报错一是调用未绑定的父类方法:
>>> class Shark2(Fish): def _init_(self): Fish._init_(self) self.hungry=True >>> shark2=Shark2() >>> shark2.move() 我的位置是: -1 8
调用super函数:
>>> class Shark2(Fish): def _init_(): super._init_() self.hungry=True >>> shark=Shark2() >>> shark.move() 我的位置是: 5 1
8.组合
class Turtle: def __init__(self,x): self.num=x class Fish: def __init__(self,x): self.num=x class Pool: def __init__(self,x,y): self.turtle=Turtle(x) self.fish=Fish(y) def print_num(self): print('水池里一共有%d只乌龟,%d只鱼' %(self.turtle.num,self.fish.num)) pool=Pool(1,10) pool.print_num()
二:类,类对象,实例对象
三:一些相关的BIF
1.issubclass()
>>> class A: pass >>> class B(A): pass >>> issubclass(B,A) True >>> issubclass(B,B)#一个类被认为是自身的自类 True
2.isinstance()
>>> b1=B() >>> isinstance(b1,B) True >>> isinstance(b1,A) True >>> isinstance(b1,(A,B)) True
3.hasattr()
class Coordinate: x = 10 y = -5 z = 0 point1 = Coordinate() print(hasattr(point1, 'x')) print(hasattr(point1, 'y')) print(hasattr(point1, 'z')) print(hasattr(point1, 'no')) # 没有该属性
4.getattr()
class Coordinate: x = 10 y = -5 z = 0 point1 = Coordinate() print(getattr(point1,'x'))
5.setattr()
class Coordinate: x = 10 y = -5 z = 0 point1 = Coordinate() setattr(point1,'w',9) print(getattr(point1,'w'))
6.delattr()
7.property()
四:魔法方法,魔法方法总是被双下划线包围,例如_init_()
1.构造和析构
_init_(self[,...])
_del_析构函数
View Code
View Code
_del_析构函数
>>> class C: def __init__(self): print('我是init') def __del__(self): print('我是del') >>> c1=C() 我是init >>> c2=c1 >>> del c2 >>> del c1 我是del
2.算术运算
1 >>> class New_int(int): 2 def __add__(self, other): 3 return int.__sub__(self,other) 4 def __sub__(self, other): 5 return int.__add__(self,other) 6 7 >>> a=New_int(3) 8 >>> b=New_int(5) 9 >>> a-b 10 8 11 12 >>> a+b 13 -2
3.简单定制
4.属性访问
5.描述符(Property的原理)
6.定制序列
7.迭代器