• python------面向对象


    反射--实例

    class Dog(object):
    
        def __init__(self,name):
            self.name = name
    
        def eat(self,food):
            print("%s is eating...%s"%(self.name,food))
    
    def bulk(self):
    
        print("%s is yelling..."%self.name)
    
    
    d  = Dog("小光")
    #根据用户输入调用方法
    choice = input(">>:").strip()
    # hasattr(obj,name_str) 根据一个对象obj里是否有相应的name_str字符串的方法
    #getattr(obj,name_str) 根据字符串去获取obj对象里的对应的方法内存地址
    if hasattr(d,choice):
        func = getattr(d,choice)
        func("")
    else:
    
        setattr(d,choice,bulk)
        func = getattr(d,choice)
        func(d)
        #d.talk(d)
    View Code

    继承---实例

    #class People : 经典类
    class People(object):#新式类
        def __init__(self,name,age,address):
            self.name = name
            self.age = age
            #私有化变量加俩个下划线
            self.__address = address
    
    
        def sleep(self):
                print("%s is sleeping"%self.name)
    
    class MakeFriend(object):
    
        def make_friends(self,obj):
            print("%s is makeing friends with %s"%(self.name,obj.name))
    
    #多继承方法
    class Man(People,MakeFriend):
    
        #构造函数重构,在子类中重写init方法,添加新属性,并且不影响父类
        def __init__(self,name,age,address,money):
            #俩种方法格式重构父类,推荐使用super的方法
            #People.__init__(self,name,age,address)
            super(Man,self).__init__(name,age,address) #新式类写法。
            self.money = money
        #重写父类方法,添加新方法
        def sleep(self):
            People.sleep(self)
            print("man is sleeping")
    
    m1 = Man("张三",15,"中国",11)
    w2 = Man("李四",16,"中国",1)
    m1.make_friends(w2)
    View Code
  • 相关阅读:
    Memcached:高性能的分布式内存缓存服务器
    MySQL数据库Query的优化
    MySQL数据库的锁定机制及优化
    系统架构及实现对性能的影响(一)
    Mysql数据库的基本结构和存储引擎简介
    Spring事务管理的回滚
    穷举算法实例
    在写完全二叉树的构建及遍历
    Inotify
    Rsync扩展
  • 原文地址:https://www.cnblogs.com/xiangrikuidebuluo/p/9554490.html
Copyright © 2020-2023  润新知