python_反射:应用

class User(object):

    def denglu(self):
        print('欢迎来到登录页面!')

    def zhuce(self):
        print('欢迎来到注册页面!')

    def youke(self):
        print('欢迎来到游客页面!')

u = User()
while True:
    q = input('请输入内容(dengli或zhuce或youke):')
    if hasattr(u,q):
        func = getattr(u,q)
        func()

程序会根据输入的字符串实现调用不同函数的功能。

猜你喜欢

转载自www.cnblogs.com/wangdianchao/p/12003788.html