zoukankan      html  css  js  c++  java
  • 面向对象编程之——继承(组合,多态,一切皆对象,面向对象高级之内置函数&方法,反射)

    一、组合

    组合:把另外一个类的对象赋值给当前对象的属性
    组合表达的是一种有的关系
    ps: 继承:is-a
       组合:has-a
    class Teacher:
        def __init__(self, name, age, gender, level):
            self.name = name
            self.age = age
            self.gender = gender
            self.level = level
    
        def tell(self):
            print("%s:%s" % (self.name, self.age))
    
    
    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))
    
    
    tea1 = Teacher("egon", 18, "male", 10)
    stu1 = Student("xxx", 19, "male")
    
    python = Course("python开放", 30000, "3mons")
    linux = Course("linux课程", 30000, "3mons")
    
    tea1.courses = [python,linux]
    stu1.course = python
    
    # tea,stu  # 超级对象
    
    # stu1.course.tell()
    for course_obj in tea1.courses:
        course_obj.tell()

    二、多态

    同一种事物有多种形态
    例如:动物这种事物有多种形态,如人狗猪
    特性: 我们可以在不考虑某一个对象具体类型的前提下,直接使用该对象

    1、父类有的功能,子类一定有
    # import abc
    #
    # class Animal(metaclass=abc.ABCMeta):
    #     @abc.abstractmethod
    #     def speak(self):
    #         pass
    #
    #     @abc.abstractmethod
    #     def run(self):
    #         pass
    #
    # # Animal()  # 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.jiao()
    # d1.giao()
    # p1.heheng()
    
    
    
    
    # peo1.speak()
    # d1.speak()
    # p1.speak()
    
    # def speak(animal):
    #     animal.speak()
    #
    # speak(peo1)
    # speak(d1)
    #
    # speak(p1)
    2、鸭子类型: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()
    class Cpu:
        def read(self):
            pass
    
        def write(self):
            pass
    
    class Process:
        def read(self):
            pass
    
        def write(self):
            pass
    
    class Disk:
        def read(self):
            pass
    
        def write(self):
            pass

    三、一切皆对象

    数据类型 == 类
    # x = 11  # x=int(11)
    # print(int)
    
    # class Foo:
    #     pass
    # print(Foo)
    
    x = [1,2,3]  # list([1,2,3])
    y = [111,222]  # list([1,2,3])
    # x.append(4)
    # y.append(3333)
    
    # list.append(x,4)
    # list.append(y,333)
    
    # print(x)
    # print(y)
    
    print(type(x))

    四、面向对象高级

    1、内置函数

    is      isinstance    issubclass
    x = 111
    
    print(type(x) is int)
    
    print(isinstance(x,int))
    
    class Bar:
        pass
    
    class Foo(Bar):
        pass
    print(issubclass(Foo,Bar))

    2、内置方法

    内置方法都是在满足某种条件下自动触发的
    (1)__str__
    class People:
        def __init__(self, name, age):
            self.name = name
            self.age = age
    
        def __str__(self):
            # print('===>')
            return "<%s:%s>" %(self.name,self.age)
    
    obj = People("egon", 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('运行完毕...') 

    五、反射
    dir(对象)

    hasattr
    getattr
    setattr
    delattr

    class Foo:
        def __init__(self, x, y):
            self.x = x
            self.y = y
    
        def f1(self):
            print('from f1')
    
    
    obj = Foo(111, 222)
    
    
    
    #反射
    # res = dir(obj)
    # print(res)
    # print(obj.__dict__[res[-1]])
    
    # print(obj.__dict__['x'])
    # import re
    #
    # for attr in dir(obj):
    #     if not re.search("^__.*__$",attr):
    #         res=getattr(obj,attr)
    #         print(res)
    
    
    # print(hasattr(obj,'x'))
    # print(getattr(obj,'xxx',None))  # obj.xxx
    # setattr(obj,"xxx",1111)  # obj.xxx = 1111
    #
    # delattr(obj,"xxx")  # del obj.xxx
    
    
    # m=__import__("time")
    # m.sleep(3)
    
    # getattr(m,'sleep')(3)
  • 相关阅读:
    iPhone应用程序开发基础之一: IBOutlet与IBAction
    Swift实战-小QQ(第1章):QQ登录界面
    Swift实战-QQ在线音乐(AppleWatch版)
    iOS苹果官方Demo合集
    Git--Submodule使用
    线程审查生产者和消费者
    Lichee(三) Android4.0该产品的目标文件夹,Lichee链接---extract-bsp
    curl转让query string逃生参数
    ERROR 2003 (HY000): Can&#39;t connect to MySQL server on &#39;10.16.115.101&#39; (111)
    吐槽一下Activiti用户手册和一本书
  • 原文地址:https://www.cnblogs.com/guojieying/p/13454981.html
Copyright © 2011-2022 走看看