python—day22

多态:

  1、什么是多态?

    多态指的是同一种事物多种形态

  2、为什么要用多态

    用基类创建一套统一的规则,强制子类去遵循(使用抽象类实现)这样便可以在不用考虑对象具体类型的前提下而直接使用对象下的方法

  3、如何用多态:

 1 class Animal:
 2 
 3     def eat(self):
 4         pass
 5 
 6     def drink(self):
 7         pass
 8 
 9     def run(self):
10         pass
11 
12 class Cat(Animal):
13     def jiao(self):
14         print('喵喵喵')
15 
16 class Dog(Animal):
17     def speak(self):
18         print('汪汪汪')
19 
20 class Pig(Animal):
21     def han(self):
22         print('哼哼哼')
23 
24 
25 
26 c1 = Cat()
27 d1 = Dog()
28 p1 = Pig()

多态性:可以在不用考虑对象具体类型的前提下直接使用对象下的方法

 1 c1.eat()
 2 d1.eat()
 3 p1.eat()
 4 
 5 c1.drink()
 6 d1.drink()
 7 p1.drink()
 8 
 9 c1.run()
10 d1.run()
11 p1.run()
12 
13 c1.jiao()
14 d1.speak()
15 p1.han()

 abstract class:

 1 import abc
 2 
 3 class Animal(metaclass=abc.ABCMeta):
 4 
 5     @abc.abstractmethod
 6     def eat(self):
 7         pass
 8 
 9     @abc.abstractmethod
10     def drink(self):
11         pass
12 
13     @abc.abstractmethod
14     def run(self):
15         pass
16 
17 class Cat(Animal):
18 
19     def eat(self):
20         print('cat eat...')
21 
22 
23     def drink(self):
24         print('cat drink...')
25 
26     def run(self):
27         print('cat running...')
28 
29 
30 class Dog(Animal):
31     def eat(self):
32         print('dog eating...')
33 
34     def drink(self):
35         print('dog drinking...')
36 
37     def run(self):
38         print('dog running...')
39 
40 
41 class Pig(Animal):
42     def eat(self):
43         print('pig eating...')
44 
45     def drink(self):
46         print('pig drinking...')
47 
48     def run(self):
49         print('pig running...')
50 
51 c = Cat()
52 d = Dog()
53 p = Pig()
54 
55 # c.eat()
56 # c.drink()
57 # c.run()
58 #
59 # d.eat()
60 # d.drink()
61 # d.run()
62 #
63 # p.eat()
64 # p.drink()
65 # p.run()
66 
67 
68 def RUN(animal):
69     animal.eat()
70     animal.drink()
71     animal.run()
72 
73 RUN(c)
74 RUN(d)
75 RUN(p)

 鸭子类型:只要你想鸭子你就是鸭子类型!!

 1 class Foo:
 2     def f1(self):
 3         print('Foo f1...')
 4 
 5     def f2(self):
 6         print('Foo f2...')
 7 
 8 class Bar:
 9     def f1(self):
10         print('Bar f1...')
11 
12     def f2(self):
13         print('Bar f2...')
14 obj1 = Foo()
15 obj2 = Bar()
16 
17 obj1.f1()
18 obj1.f2()
19 
20 obj2.f1()
21 obj2.f2()
22 
23 
24 #只要你类都有相似的状态都可以成为鸭子类型
25 class Disk:
26     def read(self):
27         print('disk reading...')
28 
29     def write(self):
30         print('disk writing...')
31 
32 
33 class Txt:
34     def read(self):
35         print('Txt reading...')
36 
37     def write(self):
38         print('Txt writing...')
39 
40 class Process:
41     def read(self):
42         print('process reading...')
43 
44     def write(self):
45         print('process writing...')
46 
47 obj1 = Disk()
48 obj2 = Txt()
49 obj3 = Process()
50 
51 obj1.read()
52 obj2.read()
53 obj3.read()
54 
55 obj1.write()
56 obj2.write()
57 obj3.write()

 classmethod 与 staticmethod:

1、绑定方法:

  在类内部定义的函数,默认就是给对象来用,而且是绑定给对象用的,称为对象的绑定方法对象的方法特殊之处:

  应该由对象来调用,对象来调用,会自动将对象当作第一个对象传入;

  绑定到类的方法特殊之处:

  应该由类来调用,类来调用,会自动将类当作第一个参数传入

classmethod:

 1 import settings
 2 class People:
 3     def __init__(self,name,age):
 4         self.name = name
 5         self.age = age
 6 
 7     def tell(self):
 8         print('%s:%s' %(self.name,self.age))
 9 
10     @classmethod
11     def from_conf(cls):
12         return cls(settings.NAME,settings.AGE)
13 
14 # p = People('kermit',18)
15 # p.tell()
16 
17 # p = People(settings.NAME,settings.AGE)
18 # p.tell()
19 
20 p = People.from_conf()
21 p.tell()

staticmethod:非绑定方法,就是一个普通函数

特殊:既不跟类绑定,也不跟对象绑定,这意味着谁都能用谁来用都是一个普通的函数,也就是说没有自动传值的特性了;

 1 import settings
 2 import hashlib
 3 import time
 4 class People:
 5     def __init__(self,name,age):
 6         self.uid = self.create_id()
 7         self.name = name
 8         self.age = age
 9 
10     def tell(self):
11         print('%s:%s:%s' %(self.uid,self.name,self.age))
12 
13     @classmethod
14     def from_conf(cls):
15         return cls(settings.NAME,settings.AGE)
16 
17     @staticmethod
18     def create_id():
19         m =hashlib.md5()
20         m.update(str(time.clock()).encode('utf-8'))
21         return m.hexdigest()
22 
23 obj = People('egon',18)
24 print(obj.uid,obj.name,obj.age)
25 obj.tell()
26 
27 print(obj.create_id())
28 print(People.create_id())

  

猜你喜欢

转载自www.cnblogs.com/kermitjam/p/8856158.html