类的定义:
#!/usr/bin/env python
# coding:utf-8
class Animals(object):
#类的数据属性
name = "tom"
#访问类的数据属性
print Animals
-类的方法即为函数
在类中定义的函数叫做方法;
类的方法中,python解释器要求的第一个形参必须为self
self实质上是类实例化后的对象本身
类的实例化产生的就是对象:将抽象化的类创造出实际存在的事物
#object:对象
#!/usr/bin/env python
# coding:utf-8
class Animals(object):
name = "tom"
age = 1
sex = "boy"
def action(self):
print "eat meat"
tom = Animals()
print tom
#调用类的方法
tom.action()
封装:
封装实际上是把数据封装到某个地方,之后在去调用被封装的内容或者数据
1.封装数据
2.调用封装数据(通过对象至今诶调用,通过self间接调用)
#!/usr/bin/env python
# coding:utf-8
class Animals(object):
#构造方法
#当实例化时会自动调用__init__构造方法
#name,age,weight为必选参数,当实例化之后必须要传参
def __init__(self,name,age,weight):
self.name = name
self.age = age
self.weight = weight
def eat(self):
print "%s eat meat" %(self.name)
self.weight +=3
def drink(self): #self为类的实例化对象(tom)
print "%s drinking water" %(self.name)
self.weight +=1
tom = Animals("tom",1,12)
tom.eat()
print tom.weight
tom.drink()
代码执行后的效果:
小张,18,开车打架
小李,12,上山打猎
小玄,15,辍学喝酒
代码如下:
#!/usr/bin/env python
# coding:utf-8
class people(object): #首先定义一个叫people的类
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex
def action1(self):
print "%s,%s,%s,开车打架" %(self.name,self.age,self.sex)
def action2(self):
print "%s,%s,%s,上山打猎" %(self.name,self.age,self.sex)
def action3(self):
print "%s,%s,%s,辍学喝酒" %(self.name,self.age,self.sex)
a = people("小张",18,"男") #实例化类
b = people("小李",16,"男")
c = people("小玄",13,"男")
a.action1()
b.action2()
c.action3()
继承:
父类和子类:(也叫基类和派生类)
#!/usr/bin/env python
# coding:utf-8
class Animals(object): #Animals为父类
def __init__(self,name,age,weight):
self.name = name
self.age = age
self.weight = weight
def eat(self):
print "%s eat meat" %(self.name)
self.weight +=3
def drink(self):
print "%s drinking water" %(self.name)
self.weight +=1
class Dog(Animals): #Dog为子类
def voice(self): #类里面的方法第一个参数必须为self
print "wulibabab"
tom = Dog("jierry",1,12)
tom.eat()
tom.voice()
可以看到子类可以继承父类里的函数功能
重写父类的构造函数:
父类名.__init__(self,形参)
super(自己类的名称,self).__init__(形参)
-不需要告诉父类的名称,如果父类改变,只需要修改class后面的继承关系即可
#!/usr/bin/env python
# coding:utf-8
class Animals(object):
def __init__(self,name,age,weight):
self.name = name
self.age = age
self.weight = weight
def eat(self):
print "%s eat meat" %(self.name)
self.weight +=3
def drink(self):
print "%s drinking water" %(self.name)
self.weight +=1
class Dog(Animals):
def __init__(self,name,age,weight,dogid):
#第一种重写父类的构造方法
# self.name = name
# self.age = age
# self.weight = weight
#第二种重写父类的构造方法
# Animals.__init__(self,name,age,weight)
#第三种重写父类的构造方法
super(Dog,self).__init__(name,age,weight)
self.dogid = dogid
tom = ("tom",12,13,"9527"
print tom.dogid
当父类和子类有相同的方法时,优先执行子类的方法
#!/usr/bin/env python
# coding:utf-8
class Animals(object):
def __init__(self,name,age,weight):
self.name = name
self.age = age
self.weight = weight
def eat(self):
print "%s eat meat" %(self.name)
self.weight +=3
def drink(self):
print "%s drinking water" %(self.name)
self.weight +=1
class Dog(Animals):
def eat(self):
print "do not eat meat"
tom = Dog("tom",12,13)
父类.__bases__ 打印类的所有父类,以元组的方式返回
父类.__dict__ 以字典的方式返回类的方法和属性
类属性:
class Info(object):
a = 1
#类属性,在内存中只存一份
country = "china"
def __init__(self,name):
#构造函数里面属性存的份数取决于对象的个数
self.name = name
a = Info("a")
print a.country
实现查看类实例化对象的个数
#!/usr/bin/env python
# coding:utf-8
class Info(object):
count = 0
#构造函数
def __init__(self):
Info.count +=1
#析构函数,在删除对象的时候自动调用
def __del__(self):
Info.count -=1
a = Info()
b = Info()
print Info.count
del a
#!/usr/bin/env python
# coding:utf-8
class Animals(object):
#类的数据属性
name = "tom"
#访问类的数据属性
print Animals
print Animals.name
-类的方法即为函数
在类中定义的函数叫做方法;
类的方法中,python解释器要求的第一个形参必须为self
self实质上是类实例化后的对象本身
类的实例化产生的就是对象:将抽象化的类创造出实际存在的事物
#object:对象
#!/usr/bin/env python
# coding:utf-8
class Animals(object):
name = "tom"
age = 1
sex = "boy"
def action(self):
print "eat meat"
tom = Animals()
print tom
#调用类的方法
tom.action()
封装:
封装实际上是把数据封装到某个地方,之后在去调用被封装的内容或者数据
1.封装数据
2.调用封装数据(通过对象至今诶调用,通过self间接调用)
#!/usr/bin/env python
# coding:utf-8
class Animals(object):
#构造方法
#当实例化时会自动调用__init__构造方法
#name,age,weight为必选参数,当实例化之后必须要传参
def __init__(self,name,age,weight):
self.name = name
self.age = age
self.weight = weight
def eat(self):
print "%s eat meat" %(self.name)
self.weight +=3
def drink(self): #self为类的实例化对象(tom)
print "%s drinking water" %(self.name)
self.weight +=1
tom = Animals("tom",1,12)
tom.eat()
print tom.weight
tom.drink()
print tom.weight
代码执行后的效果:
小张,18,开车打架
小李,12,上山打猎
小玄,15,辍学喝酒
代码如下:
#!/usr/bin/env python
# coding:utf-8
class people(object): #首先定义一个叫people的类
def __init__(self,name,age,sex):
self.name = name
self.age = age
self.sex = sex
def action1(self):
print "%s,%s,%s,开车打架" %(self.name,self.age,self.sex)
def action2(self):
print "%s,%s,%s,上山打猎" %(self.name,self.age,self.sex)
def action3(self):
print "%s,%s,%s,辍学喝酒" %(self.name,self.age,self.sex)
a = people("小张",18,"男") #实例化类
b = people("小李",16,"男")
c = people("小玄",13,"男")
a.action1()
b.action2()
c.action3()
继承:
父类和子类:(也叫基类和派生类)
#!/usr/bin/env python
# coding:utf-8
class Animals(object): #Animals为父类
def __init__(self,name,age,weight):
self.name = name
self.age = age
self.weight = weight
def eat(self):
print "%s eat meat" %(self.name)
self.weight +=3
def drink(self):
print "%s drinking water" %(self.name)
self.weight +=1
class Dog(Animals): #Dog为子类
def voice(self): #类里面的方法第一个参数必须为self
print "wulibabab"
tom = Dog("jierry",1,12)
tom.eat()
tom.voice()
可以看到子类可以继承父类里的函数功能
重写父类的构造函数:
父类名.__init__(self,形参)
super(自己类的名称,self).__init__(形参)
-不需要告诉父类的名称,如果父类改变,只需要修改class后面的继承关系即可
#!/usr/bin/env python
# coding:utf-8
class Animals(object):
def __init__(self,name,age,weight):
self.name = name
self.age = age
self.weight = weight
def eat(self):
print "%s eat meat" %(self.name)
self.weight +=3
def drink(self):
print "%s drinking water" %(self.name)
self.weight +=1
class Dog(Animals):
def __init__(self,name,age,weight,dogid):
#第一种重写父类的构造方法
# self.name = name
# self.age = age
# self.weight = weight
#第二种重写父类的构造方法
# Animals.__init__(self,name,age,weight)
#第三种重写父类的构造方法
super(Dog,self).__init__(name,age,weight)
self.dogid = dogid
tom = ("tom",12,13,"9527"
print tom.dogid
当父类和子类有相同的方法时,优先执行子类的方法
#!/usr/bin/env python
# coding:utf-8
class Animals(object):
def __init__(self,name,age,weight):
self.name = name
self.age = age
self.weight = weight
def eat(self):
print "%s eat meat" %(self.name)
self.weight +=3
def drink(self):
print "%s drinking water" %(self.name)
self.weight +=1
class Dog(Animals):
def eat(self):
print "do not eat meat"
tom = Dog("tom",12,13)
tom.eat()
父类.__bases__ 打印类的所有父类,以元组的方式返回
父类.__dict__ 以字典的方式返回类的方法和属性
类属性:
class Info(object):
a = 1
#类属性,在内存中只存一份
country = "china"
def __init__(self,name):
#构造函数里面属性存的份数取决于对象的个数
self.name = name
a = Info("a")
print a.country
实现查看类实例化对象的个数
#!/usr/bin/env python
# coding:utf-8
class Info(object):
count = 0
#构造函数
def __init__(self):
Info.count +=1
#析构函数,在删除对象的时候自动调用
def __del__(self):
Info.count -=1
a = Info()
b = Info()
print Info.count
del a
print Info.count