1、创建和使用类
1、1 创建类
class Human(object):
def __init__(self,name,age):
self.name=name
self.age=age
def eat(self):
print self.name.title()+' is eating'
def sleep(self):
print self.name.title()+' must go to sleep'
1)约定首字母大写的名称是类
2)方法__init__左右两侧的下划线是两个,旨在避免Python默认方法和普通方法发生名称冲突
3)方法__init__定义中包含三个形参,self,name,age。形参self是必须的,并且要位于其他形参的前面。在Python调用这个方法来创建对象的时候,将自动传入实参self。
每个与类相关的方法调用都自动传递实参self,他是一个指向对象本身的引用,让对象能访问类中的属性和方法。
4)以self为前缀的变量都可供类中的所有方法使用 ,可通过类的任何对象来访问这些变量。
1、2 根据类创建对象
zhao=Human('cheng',66)
print 'my name is '+zhao.name
print 'i am '+str(zhao.age)
zhao.eat()
zhao.sleep()
约定小写的名称代表创建的的对象
1)访问属性:使用句点表示法来访问对象的属性。
2)调用方法:创建好对象后,可使用句点法来调用类中定义的任何方法。
2 、使用类和对象
2、1 给属性指定默认值
类中的每个属性都必须有初始值,在有些情况,在方法__init__()内指定这种初始值是可行的,如果对某个属性这样做了,就无需包含为他提供初始值的形参。
class Human(object):
def __init__(self,name,age):
self.name=name
self.age=age
self.skin='yello'
def eat(self):
print self.name.title()+' is eating'
def sleep(self):
print self.name.title()+' must go to sleep'
zhao=Human('cheng',66)
print 'my name is '+zhao.name
print 'i am '+str(zhao.age)
print 'my skin is '+zhao.skin.title()
class Human(object):
def __init__(self,name,age,skin='yello'):
self.name=name
self.age=age
self.skin=skin
def eat(self):
print self.name.title()+' is eating'
def sleep(self):
print self.name.title()+' must go to sleep'
zhao=Human('cheng',66)
print 'my name is '+zhao.name
print 'i am '+str(zhao.age)
print 'my skin is '+zhao.skin.title()
这两种都是可行的
2、2 修改属性的值
1)通过对象来修改属性的值
class Human(object):
def __init__(self,name,age,skin='yello'):
self.name=name
self.age=age
self.skin=skin
def eat(self):
print self.name.title()+' is eating'
def sleep(self):
print self.name.title()+' must go to sleep'
zhao=Human('cheng',66)
zhao.skin='black'
print 'my skin is '+zhao.skin.title()
2)通过方法修改属性的值
class Human(object):
def __init__(self,name,age,nationality,skin='yello'):
self.name=name
self.age=age
self.nationality=nationality
self.skin=skin
def eat(self):
print self.name.title()+' is eating'
def sleep(self):
print self.name.title()+' must go to sleep'
def update_skin(self):
if self.nationality!='china':
self.skin='white'
zhao=Human('cheng',66,'usa')
zhao.update_skin()
print 'my skin is '+zhao.skin.title()
通过方法update_skin()来修改skin属性的值
3)通过方法对属性的值进行递增
class Human(object):
def __init__(self,name,age,nationality,high=170,skin='yello'):
self.name=name
self.age=age
self.nationality=nationality
self.high=high
self.skin=skin
def eat(self):
print self.name.title()+' is eating'
def sleep(self):
print self.name.title()+' must go to sleep'
def update_high(self,add_high):
self.high+=add_high
def update_skin(self):
if self.nationality!='china':
self.skin='white'
zhao=Human('cheng',66,'usa')
zhao.update_skin()
zhao.update_high(80)
print 'my high is '+str(zhao.high)
zhao.update_high(20)
print 'my high is '+str(zhao.high)
print 'my skin is '+zhao.skin.title()
上面创建了方法update_high()来更新high属性
3、继承
一个类继承另一个类时,他将自动获得另一个类的所有属性和方法,原有的类称为父类,新类称为子类
3、1 子类的方法__init__()
class Human(object):
def __init__(self,name,age,nationality,high=177,skin='yello'):
self.name=name
self.age=age
self.nationality=nationality
self.high=high
self.skin=skin
def eat(self):
print self.name.title()+' is eating'
def sleep(self):
print self.name.title()+' must go to sleep'
def update_high(self,add_high):
self.high+=add_high
def update_skin(self):
if self.nationality!='china':
self.skin='white'
class Man(Human):
def __init__(self,name,age,nationality,high=177,skin='yello'):
super(Man,self).__init__(name,age,nationality,high=177,skin='yello')
zhao=Man('qian',22,'china')
print str(zhao.high)
1)创建子类时,父类必须包含在当前文件中,且位于子类前面。
2)定义子类时,必须在括号内指定父类的名称
3)super()是一个特殊的函数,帮助Python将父类和子类关联起来:
super(Man,self).__init__(name,age,nationality,high=177,skin='yello')
注意:这里的__self__()中无对象self
Python2.7中函数super()需要两个实参,子类名和对象self
3、2 给子类定义属性和方法
class Human(object):
def __init__(self,name,age,nationality,high=177,skin='yello'):
self.name=name
self.age=age
self.nationality=nationality
self.high=high
self.skin=skin
def eat(self):
print self.name.title()+' is eating'
def sleep(self):
print self.name.title()+' must go to sleep'
def update_high(self,add_high):
self.high+=add_high
def update_skin(self):
if self.nationality!='china':
self.skin='white'
class Man(Human):
def __init__(self,name,age,nationality,high=177,skin='yello'):
super(Man,self).__init__(name,age,nationality,high=177,skin='yello')
self.weight=126
def my_weight(self,food):
if food>0:
self.weight+=food
print 'my weight is '+str(self.weight)
zhao=Man('qian',22,'china')
print str(zhao.high)
zhao.my_weight(10)
子类Man中定义了属性weight和方法my_weight()
3、3 重写父类的方法
对于父类的方法,只要不符合子类模拟实物的行为,都可对其重写,为此,可在子类中定义一个这样的方法,即它与要重写的父类方法同名。这样,Python将不会考虑这个父类的方法,只关注子类中的定义的方法。
3、4 将对象作为属性
使用代码模拟实物时,在类中添加的细节越来越多:属性和方法清单及文件都越来越长。此时,可将类的一部分作为独立的类提取出来。
3、5 关于在子类中重写__init__()的情况说明
class P():
def __init__(self):
print('calling ps construtor')
class C(P):
def __init__(self):
print('calling Cs construtor')
c=C()
print(c)
1)若在子类中重定义了构造器,那么这将覆盖父类的构造器。若在子类未定义构造器,子类将继承法并自动调用父类的构造器
2)如果还想调用父类的构造器,要明确指出使用子类的对象去调用父类(未绑定)的方法。类似如下的做法:
class P():
def __init__(self):
print('calling ps construtor')
class C(P):
def __init__(self):
P.__init__(self)
print('calling Cs construtor')
c=C()
print(c)
创建C类的对象后,输出结果如下:
calling ps construtor
calling Cs construtor
3.6、关于子类继父类的方法的说明:
class P():
def __init__(self):
print('calling ps construtor')
class C(P):
def __init__(self):
super(C,self).__init__()
print('calling Cs construtor')
c=C()
print(c)
1)使用super(C,self).__init__()可同时继承父类的属性及方法
2)创建类C的对象后输出如下:
calling ps construtor
calling Cs construtor
4、导入类
4、1 在一个模块中导入多个类
from use_class import Human,Animal,Botany
zhao=Human('qian',25,'china')
print 'my name is '+zhao.name
4、2 导入整个模块
import use_class
zhao=use_class.Human('qian',25,'china')
print 'my name is '+zhao.name
导入整个模块之后,在使用句点表示法访问需要的类
4、3 导入模块中的所有类
格式:
from module_name import *
4、4 在一个模块中导入另一个模块