一、封装:
一、封装: 属性和方法放到类内部,通过对象访问属性或者方法,隐藏功能的实现过程,不用关心类内部代码的实现的细节。
二、继承的调用
"""
调用父类(超类)方法
1.使用父类调用
2.使用super() 仅适用于新式类 遵循mro顺序
新式类:在创建的时候继承object (或者是从内置类型,如list,dict等)
经典类:是直接声明的
super(A, self).__init__() A和self可以不写 .__init__()可写参数
3.子类继承父类: 01代码演示
子类继承父类不做初始化,会自动继承父类属性
子类继承父类做了初始化,且不调用super初始化父类构造函数,子类不会自动继承父类的属性。
子类继承父类做了初始化,且调用了super初始化了父类的构造函数,子类也会继承父类的属性
4.子类继承父类: 02代码演示
初始化并且调用super初始化构造函数,并且重写父类方法
5.super多继承: 03代码演示
顺序遵循mro
C只能初始化A 对B没法处理,必须在A中去初始化B
"""
二、1代码01
class Person(object):
def __init__(self,name = "person"):
self.name = name
class Pupel(Person): # 不做初始化 会自动继承父类属性 name
pass
class Puple_init(Person): # 做初始化 且不调用super初始化父类构造函数 不会继承父类属性 name
def __init__(self,age):
self.age = age
class Puple_super(Person): # 做初始化 也调用super().__init__(name)初始化父类构造函数 会继承父类属性 name
def __init__(self,name,age):
self.age = age
super().__init__(name)
# pp_i = Puple_init(10)
# print(pp_i.age) # 'Puple_init' object has no attribute 'name'
二、2代码02
def get_logging(word):
return "notice"
class DWHTaskBase(object):
def __init__(self, dwf_config):
self.dwh_config = dwf_config
def notice(self):
self.logger = get_logging("notice")
print(self.logger)
class FragmentCreator(DWHTaskBase):
"""
文章分段任务
"""
def __init__(self, dwf_config):
super().__init__(dwf_config)
self.filter_condition = {
"fragment": {'$exists': 0},
'status': 1
}
self.update_status = {"fragment": "1"}
def notice(self):
# super(FragmentCreator, self).notice()
results = self.dwh_config.mysql.get_each_data()
print(results)
# para_creater = FragmentCreator(dwf_config=dwh_config)
# para_creater.notice()
一、3代码03
class A(object):
def __init__(self):
print("sds")
super(A, self).__init__()
print('>> A')
class B(object):
def __init__(self):
# super(B, self).__init__()
print('>> B')
class C(A, B):
def __init__(self):
super(C, self).__init__()
print('>> C')
# C()
# print(C.mro())
三、多态
"""
一、
多态概念:
多态概念:多态是指一类事物有多种形态:(一个抽象类拥有多个子类,故多态依赖继承) 例如动物类包含 猫类
多态性概念:多态性是指可以通过调用相同的函数名 执行具有不同的函数(实现不同的功能)
面向对象态性表述多:向不同的对象发送同一条消息,不同的对象在接收时会产生不同的行为(即方法)。也就是说,每个对象可以用自己的方式去响应共同的消息。所谓消息,就是调用函数,不同的行为就是指不同的实现,即执行不同的函数。
多态条件:2个
1、继承:多态一定是发生在子类和父类之间; 2、重写:子类重写了父类的方法。
"""
class Animal(object):
def run(self):
print('Animal is running...')
class Dog(Animal):
def run(self):
print('Dog is running...')
class Cat(Animal):
def run(self):
print('Cat is running...')
def run_twice(animal):
animal.run()
run_twice(Dog())
如果觉得不错可以添加关注公众号,不定期分享有趣内容!