day27

今日内容:
    1、在子类派生的新方法中重用父类功能的方式二
        super().属性
    2、组合
        继承:is-a
        组合:has-a
    3、多态
    4、面向对象高级
          内置方法__len__
    5、一切皆为对象
    6、反射
        dir(对象)
        hasattr、getattr、setattr、delattr

# super()
'''
# 在子类派生的新方法中重用父类功能
# 方式二:调用super(自己的类名,self)会返回一个特殊的对象,super(自己的类名,self).属性,会参照发起属性查找的类的mro列表去父类中(以父类为起点)查找属性
# 特点:严格依赖于继承关系

# class OldboyPeople:
#     school = "oldboy"
#     def __init__(self, name, age, gender):
#         self.name = name
#         self.age = age
#         self.gender = gender
#
#     def f1(self):
#         print('1111111')
#
# class Student(OldboyPeople):
#     def __init__(self, name, age, gender, stu_id, course):
#         super(Student, self).__init__(name, age, gender)
#         self.stu_id = stu_id
#         self.course = course
#
#     def choose(self):
#         print('%s 正在选课' % self.name)
#
#     def f1(self):
#         # OldboyPeople.f1(self)
#         # super().f1()
#         print("22222")
#
# # print(Student.mro())
# stu1 = Student("艾利克斯", 73, 'male', 1001, "python全栈开发")
# # print(stu1.__dict__)
# stu1.f1()
'''

# 组合
'''
# 组合:把另外一个类的对象赋值给当前对象的属性
# 组合表达的是一种'有'的关系
# class Teacher:
#
#     def __init__(self, name, age, gender):
#         self.name = name
#         self.age = age
#         self.gender = gender
#
#
# class Student:
#     def __init__(self, name, age, gender):
#         self.name = name
#         self.age = age
#         self.gender = gender
#
#
# class Course:
#     def __init__(self, name, price, period):
#         self.name = name
#         self.price = price
#         self.period = period
#
#     def tell(self):
#         print('%s,%s,%s' % (self.name, self.price, self.period))
#
# python = Course('python', 20000, '6months')
# linux = Course('linux', 20000, '4months')
# tea1 = Teacher('aaa', 60, 'male')
# stu1 = Student("bbb", 13, 'male')
# tea1.courses = [python, linux]
# stu1.course = python
# print(tea1.__dict__)
# for course in tea1.courses:
#     course.tell()
# print(stu1.__dict__)
# stu1.course.tell()
#
# stu1,tea1 称为超级对象
'''

# 多态
'''
# 同一种事物有多种形态(多个子类继承同一个父类)
# 例如:动物这种事物有多种形态,如人\狗\猪
# 特性: 我们可以在不考虑某一个对象具体类型的前提下,直接使用该对象
# 父类有的功能,子类一定有
# import abc        # 强制使子类定义父类中的功能
#
# class Animal(metaclass=abc.ABCMeta):
#     @abc.abstractmethod
#     def speak(self):
#         pass
#
#     @abc.abstractmethod
#     def run(self):
#         pass
#
# Animal的作用是用来制定标准的
#
# class People(Animal):
#     def speak(self):
#         print("啊啊啊啊")
#
#     def run(self):
#         print("咻咻咻...")
#
# class Dog(Animal):
#     def giao(self):
#         print("汪汪汪")
#
# class Pig(Animal):
#     def heheng(self):
#         print("哼哼哼")
#
# peo1=People()
# d1=Dog()
# p1=Pig()
#
# peo1.speak()
# 
# def speak(animal):
#     animal.speak()
#
# speak(peo1)
# speak(d1)


# 鸭子类型:duck
# class People:
#     def speak(self):
#         print("啊啊啊啊")
#
#     def run(self):
#         print("咻咻咻...")
#
# class Dog:
#     def speak(self):
#         print("汪汪汪")
#
#     def run(self):
#         print("狂奔...")
#
# class Pig:
#     def speak(self):
#         print("哼哼")
#
#     def run(self):
#         print("咣咣咣...")
#
# peo1=People()
# d1=Dog()
# p1=Pig()
# peo1.run()
# d1.run()
# p1.run()
'''

'''
# 一切皆对象
# 数据类型 == 类
# x = 11  # x=int(11)
# print(int)

# class Foo:
#     pass
# print(Foo)

# x = [1, 2, 3]   # list([1,2,3])
# y = [111, 222]
# x.append(4)           # list.append(x,4)
# y.append(3333)        # list.append(y,333)
# print(x)
# print(y)
'''

# 内置函数
'''
# isinstance(__obj, __class_or_tuple)     # 判断类型
x = 111
print(type(x) is int)           # True
print(isinstance(x, int))        # True
# issubclass(__cls, __class_or_tuple)       # 判断父子类
class Bar:
    pass

class Foo(Bar):
    pass
print(issubclass(Foo,Bar))      # True
'''

# 内置方法
'''
# 内置方法都是在满足某种条件下自动触发的
# 1、__str__
class People:
    def __init__(self, name, age):
        self.name = name
        self.age = age

    def __str__(self):
        return f'{self.name} : {self.age}'


obj = People('aaa', 18)
print(obj)              # print(obj.__str__())

# 2 __del__
class People:
    def __init__(self, name, age, f):
        self.name = name
        self.age = age
        self.f = f

    def __del__(self):
        print('===>')
        # 回收资源
        self.f.close()

obj = People("egon", 18,open("a.txt",'w',encoding='utf-8'))

del obj
# print('运行完毕...')
'''

# 反射
'''
# 让程序能分析对象中有哪些属性,并且能够获取属性值
class Foo:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def f1(self):
        print('from f1')

obj = Foo(111, 222)

print(hasattr(obj, 'x'))        # 判断对象中是否有对应属性
print(getattr(obj, 'x'))        # 获取对象中对应属性的值,若没有找到该属性,则报错,可自定义一个返回值避免报错
print(getattr(obj, 'xxx', None))
setattr(obj, 'xxx', 1111)       # 若对象中包含属性,则修改;否则进行赋值操作     obj.xxx = 1111
delattr(obj, 'xxx')             # 删除属性      del obj.xxx
# import re
#
# for attr in dir(obj):
#     if not re.search("^__.*__$",attr):
#         res=getattr(obj,attr)
#         print(res)


# m = __import__("time")
# m.sleep(3)

# getattr(m,'sleep')(3)
'''

 

posted @ 2020-08-07 20:15  板鸭没有腿  阅读(81)  评论(0)    收藏  举报