python---面向对象3

1.类方法与静态方法

# 方法: 函数
# 属性: 变量名


class Date(object):
    def __init__(self, year, month, day):
        self.year = year
        self.month = month
        self.day =  day
    # echo普通方法, 默认情况下会传递对象给echo
    def echo(self):
        return  "%s %s %s" %(self.year, self.month, self.day)

    # 默认传递类本身给这个方法;
    @classmethod
    def as_string(cls, s):
        print(cls)
        month, day, year = s.split('/')
        d = cls(year, month, day)
        return d
    #  默认python解释器不会传递任何参数
    @staticmethod
    def is_vaild(s):
        # 批量将年月日转换成整形(列表生成式, map)
        # month, day, year = s.split('/')
        # month, day, year = [int(i) for i in s.split('/')]
        month, day, year = map(int, s.split('/'))
        return 0 < month <= 12 and 0 < day <= 31 and 1 < year < 9999
    #
# d = Date(2018, 10, 10)
# d.echo()


# s = '10/10/2018'
# # print(as_string('12/10/2019').echo())
#
# print(Date.as_string(s).echo())

s = '10/10/2018'
print(Date.is_vaild(s))


d = Date(2018, 10, 10)
print(d.is_vaild('13/10/2019'))


from datetime import  date

#
# """
# 类方法的官方使用案例: from datetime import date
#
#     @classmethod
#     def fromtimestamp(cls, t):
#         "Construct a date from a POSIX timestamp (like time.time())."
#         y, m, d, hh, mm, ss, weekday, jday, dst = _time.localtime(t)
#         return cls(y, m, d)
#
#     @classmethod
#     def today(cls):
#         "Construct a date from time.time()."
#         t = _time.time()
#         return cls.fromtimestamp(t)
#
#
#
#
# 类属性的官方使用案例: 显示年月日, 但是不能任意修改年月日
#
#
#     # Read-only field accessors
#     @property
#     def year(self):
#         """year (1-9999)"""
#         return self._year
#
#     @property
#     def month(self):
#         """month (1-12)"""
#         return self._month
#
#     @property
#     def day(self):
#         """day (1-31)"""
#         return self._day
# """



from datetime import  date

2.with语句

# # 上下文管理协议
# #   1). 当with语句开始运行的时候,执行什么方法;
# #   2). 当with语句执行结束之后出发某个方法的运行;
# with open('/etc/passwd') as f:
#     print(f.read())


class MyOpen(object):
    def __init__(self, filename, mode='r'):
        self._name = filename
        self._mode = mode
    # 当with语句开始运行的时候,执行什么方法;
    def __enter__(self):
        self.f = open(self._name, self._mode)
        return  self.f
    # 当with语句执行结束之后出发某个方法的运行;
    def __exit__(self, exc_type, exc_val, exc_tb):
        self.f.close()

    @property
    def name(self):
        return  self._name
    @property
    def mode(self):
        return  self._mode


with MyOpen('/etc/passwd')   as f:
    print(f.closed)
    print(f.read(5))


print(f.closed)
# with open('/etc/passwd') as f:

3.面向对象的反射机制

反射: 让对象告诉我们相关信息(对象拥有的属性和方法, 对象所属的类等....)

# 1). 如果知道对象拥有的属性和方法.
print(dir(str))
f = open('/tmp/passwd')
print(dir(f))


# 2). 判断对象所属的类
print(type('hello'))
class Student(object):
    """
    这是student类的帮助文档


    """
    def __init__(self, name, age):
        self.name = name
        self.__age = age


    def get_score(self):
        return  "score"

    def get_grade(self):
        return 'grade'


s1 = Student("fentiao", 10)
print(type(s1))
print(isinstance(s1, Student))
print(isinstance('hello', Student))


# 3). 跟据对象可以获取的内容
print(s1.__class__)
print(s1.__dict__)
print(s1.__doc__)


# 4). hasattr, getattr, setattr, delattr
# hasattr: 判断对象是否包含对应的属性或者方法名;
print(hasattr(s1, 'name'))
print(hasattr(s1, '__age'))  # 私有属性, 私有方法, 是不能判断的;
print(hasattr(s1, 'score'))

print(hasattr(s1, 'get_score'))
print(hasattr(s1, 'set_score'))


# getattr: 用于返回对象的属性值或者方法名对应的方法体;
print(getattr(s1, 'name'))
print(getattr(s1, '__age', 'no attr'))
print(getattr(s1, 'get_score', 'no method'))  # 获取方法名, 如果要执行方法, 直接调用即可
print(getattr(s1, 'set_score', 'no method'))  # 获取方法名, 如果要执行方法, 直接调用即可



# setattr:
# 修改某个属性的值
setattr(s1, 'name', 'westos')
print(getattr(s1, 'name'))

# 添加某个属性及对应的值;
setattr(s1, 'score', 100)
print(getattr(s1, 'score'))

# 修改方法
def get_score1():
    return "这是修改的方法内容"
setattr(s1, 'get_score', get_score1)
print(getattr(s1, 'get_score')())

def set_score():
    return  "这是添加的方法"
# 添加方法
setattr(s1, 'set_score', set_score)
print(getattr(s1, 'set_score')())


# delattr
delattr(s1, 'name')
print(hasattr(s1, 'name'))

print(hasattr(s1, 'set_score'))
delattr(s1, 'set_score')
print(hasattr(s1, 'set_score'))

4.反射机制的应用场景_动态方法调用


 https://www.csdn.net/nav/newarticles
 https://www.csdn.net/nav/watchers
 https://www.csdn.net/nav/news
 https://www.csdn.net/nav/ai
 https://www.csdn.net/bbs/newarticles
 https://www.csdn.net/bbs/watchers
 https://www.csdn.net/bbs/news
 https://www.csdn.net/bbs/ai


class Web(object):
    def newarticles(self):
        return  "<h1>newarticles</h1>"

    def watchers(self):
        return  "<h1>watchers</h1>"

    def news(self):
        return "<h1>news</h1>"

    def ai(self):
        return "<h1>ai</h1>"



flask = Web()
def run():
    url = input("url:").split('/')[-1]
    # if url == 'news':
    #     return  flask.news()
    # elif url == 'ai':
    #     return  flask.ai()
    # else:
    #     return  "<h1>404</h1>"
    if hasattr(flask, url):  # ai
        return getattr(flask, url)()
    else:
        return "<h1>404</h1>"
if __name__ == "__main__":
    while True:
        print(run())

5.反射机制与动态倒入模块


# 动态导入模块
/bbs/login
/bbs/index
/blog/login
/blog/index


def run():
    # '/bbs/index'  ; modules='bbs', func=‘index’
    modules, func  = input("url:").split('/')[-2:]
    # 导入一个包含变量的模块名, 其中obj是模块的别名
    obj = __import__('lib.'+ modules)

    # 判断模块中是否有指定的方法, 如果有, 则执行代码, 如果没有, 404报错;
    print(obj)
    # print(modules)
    # print(obj.index())

    if hasattr(obj, func):
        fun = getattr(obj, func)
        return  fun()
    else:
        return  "404: 页面找不到"



    # import  lib.bbs
    # import  lib.bbs as obj
    # obj.index()
    # lib.bbs.index()
    # if hasattr(flask, url):  # ai
    #     return getattr(flask, url)()
    # else:
    #     return "<h1>404</h1>"
if __name__ == "__main__":
    while True:
        print(run())

6.倒入模块import理解

# 注意: __import__内置函数用于动态加载类和函数.
#  如果一个模块经常变化, 就可以使用__import__实现动态导入.

import  lib.bbs as obj
print(obj.index())

obj = __import__('lib.bbs')
print(obj.index())

猜你喜欢

转载自blog.csdn.net/suifengOc/article/details/82353352