1105Python面向对象

闭包讲解:

"""
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨,请访问:https://ceshiren.com/t/topic/15860'
"""

# # 函数引用
#
# def show():
#     print("Show Run ...")
#
# # 函数引用应用场景一般在函数的参数或返回值中使用
#
# def callback(func):
#     # 请求数据
#     def aa():
#         print("aa")
#     # 请求数据接收完成
#     func()
#     return aa
#
#
# callback(show)
#
# data = []


# 定义一个闭包函数

def outer():
    a = 100
    def inner():
        b = 200
        print("a= ",a)
        print("b= ",b)
    return inner

func = outer()  # func = inner

func()  # inner()

装饰器讲解

"""
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨,请访问:https://ceshiren.com/t/topic/15860'
"""
# # 装饰器就是一个特殊的闭包函数的定义形式
# # 1. 外函数中必须定义一个内函数
# # 2. 内函数必须要使用外函数的局部变量
# # 3. 外函数必须要返回内函数的引用
#
# def outer(func):
#     def inner():
#         print("被装饰函数执行前的装饰代码")
#         func()
#         print("被装饰函数执行后的装饰代码")
#     return inner
#
#
# @outer
# def show():
#     print("Show Run ....")
#
# # 装饰过程原理
# # show = outer(show)
# # show = inner
# # show = outer.<locals>.inner
#
# show()
# print(show)
#
#
#
# # <function show at 0x10cb29d80>
# # <function outer.<locals>.inner at 0x106695f30>
#
#

import time




# def mysum():
#     n = 0
#     start = time.time()
#     for i in range(10000001):
#         n += i
#     print(n)
#     stop = time.time()
#     print(f"耗时{stop - start} 秒")


# def count_time(func):
#     start = time.time()
#     func()
#     stop = time.time()
#     print(f"耗时{stop - start} 秒")
#
# def mysum():
#     n = 0
#     for i in range(10000001):
#         n += i
#     print(n)
#
#
# count_time(mysum)

def count_time(func):
    def inner(*args, **kwargs):
        start = time.time()
        result = func(*args, **kwargs) # 5000050000
        stop = time.time()
        print(f"耗时{stop - start} 秒")
        return result
    return inner

@count_time
def mysum():
    n = 0
    for i in range(100001):
        n += i
    return n


@count_time
def show(n):
    for i in range(n):
        print("show runn")

@count_time(123)
def show2(n, msg):
    for i in range(n):
        print(msg)


# print(mysum())
a = mysum()
print(a)
show(5)
show2(3, "hello")

面象对象讲解

"""
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨,请访问:https://ceshiren.com/t/topic/15860'
"""

# data.txt

a = 1
print(type(a))

s = "hello"
print(type(s))

data = [1,2,3,4]
print(type(data))

data = (1,2,3,4)
print(type(data))

class MyClass:
    pass

obj = MyClass()
print(obj)
print(type(obj))

# [].upper()

class MyString():
     # def __new__():
     #     self = allow()
     #     __init__(self)


    def __init__(self, name):
        # 实始化方法,自动没用
        # print("INIT run")
        self.name = name
        self.age = 1
        self.gender = "male"

    def upper(self):
        print("upper" + self.name)
        result = "a" + "b"
        result = result.upper()
        return result


    def lower(self):
        print("lower" + self.name)

    def __repr__(self):
        return f"My Name IIIIs {self.name}"
    #
    # def __str__(self):
    #     # 必须要返回一个字符串数据
    #     return f"My Name is {self.name}"

    def __str__(self):
        return self.__repr__()



ms1 = MyString("Tom")
ms2 = MyString("Jack")

# MyString.upper(ms1)
# MyString.lower(ms2)



print(MyString.__dict__)

# # ms1.name = "Tom"
# print(ms1.name)
# print(ms2.name)
#
# print(ms1)
# print(ms2)
#
# ms1.lower()
# ms2.lower()
#
# ms1.upper()
# ms2.upper()

data = [1,2,2,3]
print(data)

print(ms1)
print(ms2)

print(ms1.upper())
print(ms1.upper().title())


作业:
将学生管理系统作业使用面向对象实现