zoukankan      html  css  js  c++  java
  • 反射的使用

    根据输入的内容执行对应的操作

    采用逻辑判断方式:

     1 class CName():
     2     def __init__(self,s):
     3         self.s = s
     4 
     5     def say_Z(self):
     6         print('I am ZZZ')
     7 
     8     def say_0(self):
     9         print('I am 000')
    10 
    11     def say_other(self):
    12         print('other')
    13 
    14     def say(self):  # 此处举例使用的是很简单的代码,如果代码逻辑很复杂,采用if...else...的逻辑判断结构会导致代码冗余,不易阅读;且需要吧所有的可能都考虑进来,需要罗列出很多判断
    15         if self.s == '0':
    16             self.say_0()
    17         elif self.s == 'Z':
    18             self.say_Z()
    19         else:
    20             self.say_other()
    21 
    22 if __name__ == '__main__':
    23     while True:
    24         s = input('请输入要说的内容:')
    25         o = CName(s)
    26         o.say()
    27         o.say()
    if...else...

    执行结果:

     1 C:UserslwjAppDataLocalProgramsPythonPython37python.exe D:/02Project/pacho/08day/反射/st_反射.py
     2 请输入要说的内容:0
     3 I am 000
     4 I am 000
     5 请输入要说的内容:c
     6 other
     7 other
     8 请输入要说的内容:Z
     9 I am ZZZ
    10 I am ZZZ
    11 请输入要说的内容:
    查看执行结果

    采用字典的方式——待补充,根据key获取到value,然后执行对应的方法

    采用反射的方式:

     1 # 根据输入的内容选择处理的逻辑
     2 class CName(object):
     3     def __init__(self, s):
     4         self.s = 'say_'+str(s)
     5 
     6     def say_Z(self):
     7         print('I am ZZZ')
     8 
     9     def say_0(self):
    10         print('I am 000')
    11 
    12     def say_other(self):
    13         print('other')
    14 
    15     def say(self):
    16         if hasattr(self,self.s):  # 如果有该属性
    17             f = getattr(self,self.s)  # 则获取该属性,此处若要获取该属性的返回值则使用f = getattr(self,self.s())
    18             f()  # 若要获取该属性的返回值则使用 return f
    19         else:
    20             setattr(self,self.s,self.say_other)  # 如果没有该属性,则设置该属性为say_other
    21 
    22 if __name__ == '__main__':
    23     while True:
    24         s = input('请输入要说的内容:')
    25         o = CName(s)
    26         o.say()
    27         o.say()
    反射hasattr

    执行结果:

     1 C:UserslwjAppDataLocalProgramsPythonPython37python.exe D:/02Project/pacho/08day/反射/st_反射.py
     2 请输入要说的内容:0
     3 I am 000
     4 I am 000
     5 请输入要说的内容:Z
     6 I am ZZZ
     7 I am ZZZ
     8 请输入要说的内容:c
     9 other
    10 请输入要说的内容:0
    11 I am 000
    12 I am 000
    13 请输入要说的内容:
    查看执行结果

    注意看,采用反射,当找不到属性时,我们为该属性设置了方法。所以第二次调用的时候才会生效,如果想要直接生效可以再次执行。

    代码如下:

     1 # 根据输入的内容选择处理的逻辑
     2 class CName(object):
     3     def __init__(self, s):
     4         self.s = 'say_'+str(s)
     5 
     6     def say_Z(self):
     7         print('I am ZZZ')
     8 
     9     def say_0(self):
    10         print('I am 000')
    11 
    12     def say_other(self):
    13         print('other')
    14 
    15     def say(self):
    16         if hasattr(self,self.s):  # 如果有该属性
    17             f = getattr(self,self.s)  # 则获取该属性,此处若要获取该属性的返回值则使用f = getattr(self,self.s())
    18             f()  # 若要获取该属性的返回值则使用 return f
    19         else:
    20             setattr(self,self.s,self.say_other)  # 如果没有该属性,则设置该属性为say_other
    21             f_1 = getattr(self,self.s)
    22             f_1()
    23 
    24 
    25 if __name__ == '__main__':
    26     while True:
    27         s = input('请输入要说的内容:')
    28         o = CName(s)
    29         o.say()
    30         o.say()
    设置并执行方法

    执行结果:

     1 C:UserslwjAppDataLocalProgramsPythonPython37python.exe D:/02Project/pacho/08day/反射/st_反射.py
     2 请输入要说的内容:0
     3 I am 000
     4 I am 000
     5 请输入要说的内容:Z
     6 I am ZZZ
     7 I am ZZZ
     8 请输入要说的内容:c
     9 other
    10 other
    11 请输入要说的内容:0
    12 I am 000
    13 I am 000
    14 请输入要说的内容:
    查看执行结果
  • 相关阅读:
    STL中set底层实现方式? 为什么不用hash?
    main 主函数执行完毕后,是否可能会再执行一段代码?(转载)
    计算机网络(转载)
    2014! 的末尾有多少个0
    最常见的http错误
    内存分配(转载)
    delphi中指针操作符^的使用
    虚拟方法virtual的用法
    调用父类方法
    指针
  • 原文地址:https://www.cnblogs.com/wjlv/p/11650620.html
Copyright © 2011-2022 走看看