Python学习week6

面向对象

类(class):实物的模型。

对象:实例化类得到对象。


创建类:

一般方法:

class Person(object):
    def __init__(self,name,sex):
        self.name=name
        self.sex=sex
p1=Person('刚田武','男')
print(p1.name,p1.sex)

 特殊方法:使用type方法:

def talk(self):
    print('hello,%s'%self.name)
def __init__(self,name,age):
    self.name=name
    self.age=age
Foo=type('Foo',(object,),{'function':talk,'__init__':__init__})     #(object,)中的逗号不能丢,因为此处需要的参数是元组,如果不加逗号就只是一个值,('Foo')效果等同于'Foo',都是值
obj=Foo('胖虎',22)
obj.function()

 


继承

继承多个类时:

如果子类有构造方法,实例化时执行子类的构造方法;

如果子类没有构造方法,实例化时执行父类的构造方法,按照参数列表从左往右校验父类,如果父类有构造方法则执行,没有构造方法就校验下一个父类,如果父类有构造方法但其构造方法需要的参数数量和实例化时传入的实参数量不等时,程序报错。只能执行一个构造方法。

A为基类,B,C继承自A,D继承B和C,如果D,B,C均没有构造函数,则实例化D时会执行A的构造方法。

python3里都是新式类,使用广度优先继承。D-B,D-B-C,D-B-C-A

python2:

经典类使用深度优先继承:D-B,D-B-A,

新式类使用广度优先继承:D-B,D-B-C,D-B-C-A

经典类:

class father:

  code

新式类:

class father(object):

  code

class People(object): #新式类
    def __init__(self,name,age):
        self.name = name
        self.age = age
        self.friends = []
        print("in the people class ")
    def eat(self):
        print("%s is eating..." % self.name)
    def talk(self):
        print("%s is talking..." % self.name)
    def sleep(self):
        print("%s is sleeping..." % self.name)
class Relation(object):
    def make_friends(self,obj):
        print("%s is making friends with %s" % (self.name,obj.name))
        self.friends.append(obj.name)
class Man(People,Relation):
    def __init__(self,name,age,money):
        super(Man,self).__init__(name,age) #等效于:People.__init__(self,name,age)
        self.money  = money
        print("%s 一出生就有%s money" %(self.name,self.money))
    def sleep(self):
        People.sleep(self)
        print("man is sleeping")

m1 = Man("刚田武",22,100)
w1=Man('朱二娃',20,100)
m1.sleep()
m1.make_friends(w1)
w1.name = "胖虎"
print(m1.friends[0])
继承


多态:接口重用,一种接口,多种实现。

class Animal(object):
    def __init__(self,name):
        self.name=name
    def animal_talk(obj):   #此处实现了对Dog和Cat类里talk方法的调用
        obj.talk()
class Dog(Animal):
    def talk(self):
        print('%s****woof,woof!***%s'%(self.name,'aaa'))
class Cat(Animal):
    def talk(self):
        print('%s****miow,miow***%s'%(self.name,'aaa'))
d=Dog('朱二娃')
c=Cat('刚田武')
d.talk()    #一般调用方式
c.talk()    
Animal.animal_talk(d)   #多态调用方式
Animal.animal_talk(c)

 静态方法:名义上归类管理,实际上在静态方法里访问不了类或实例的任何属性。

@staticmethod 修饰(装饰器)

类方法:只能访问类变量,不能访问实例变量

@classmethod

 

class Animal(object):
    name='abc'
    def __init__(self,name):
        self.name=name
    @classmethod
    def eat(obj):
        print(obj.name)     #此处的obj是d,name是'abc',是类的变量,不是对象d的变量'朱二娃'
        print('%s is eating'%obj.name)
    def animal_talk(obj):
        obj.talk()
class Dog(Animal):
    def talk(self):
        print('%s****woof,woof!***%s'%(self.name,'aaa'))
d=Dog('朱二娃')
d.eat()

 

属性方法:把一个方法变成一个静态属性,调用属性不需要加括号。可以隐藏实现过程,对用户来说只是属性。

@property

class Animal(object):
    """ 描述类信息 """
    # name='abc'
    def __init__(self,name):
        self.name=name
    @property
    def eat(obj):       #此处默认传递的实参是对象d本身
        print('%s is eating'%obj.name)
    def animal_talk(obj):
        obj.talk()
class Dog(Animal):
    def talk(self):
        print('%s****woof,woof!***%s'%(self.name,'aaa'))
d=Dog('朱二娃')
d.eat   #不用加括号,就像是调用属性值一样,实际上是把对象d本身传递给属性方法
print(Animal.__doc__)

多态

猜你喜欢

转载自www.cnblogs.com/zhhy236400/p/9786116.html