From e1404b03dfab69855dd543dd24e2ef29bb9becd4 Mon Sep 17 00:00:00 2001 From: yunyunyun <18883994582@163.com> Date: Fri, 8 Jan 2021 17:42:00 +0800 Subject: [PATCH 1/2] =?UTF-8?q?[0108=E4=BD=9C=E4=B8=9A=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../3-1-04-calculator.py" | 147 +++++++++--------- .../4-2homework/4-2-001.py" | 75 +++++++++ .../4-2homework/4-2-002.py" | 43 +++++ .../4-2homework/4-2-003.py" | 81 ++++++++++ .../4-2homework/4-2-lianxi.py" | 0 5 files changed, 269 insertions(+), 77 deletions(-) rename "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2543\345\221\2501228-0103/3-1-\344\275\234\344\270\232/3-1-04.py" => "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2543\345\221\2501228-0103/3-1-\344\275\234\344\270\232/3-1-04-calculator.py" (86%) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2544\345\221\2500104-0110/4-2homework/4-2-001.py" create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2544\345\221\2500104-0110/4-2homework/4-2-002.py" create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2544\345\221\2500104-0110/4-2homework/4-2-003.py" create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2544\345\221\2500104-0110/4-2homework/4-2-lianxi.py" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2543\345\221\2501228-0103/3-1-\344\275\234\344\270\232/3-1-04.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2543\345\221\2501228-0103/3-1-\344\275\234\344\270\232/3-1-04-calculator.py" similarity index 86% rename from "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2543\345\221\2501228-0103/3-1-\344\275\234\344\270\232/3-1-04.py" rename to "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2543\345\221\2501228-0103/3-1-\344\275\234\344\270\232/3-1-04-calculator.py" index ea4e2dd5..ecfbd90c 100644 --- "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2543\345\221\2501228-0103/3-1-\344\275\234\344\270\232/3-1-04.py" +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2543\345\221\2501228-0103/3-1-\344\275\234\344\270\232/3-1-04-calculator.py" @@ -1,78 +1,71 @@ -# 作业3 -# 在第二周-第一节课我们实现的简单计算器的基础上, 对参数进行检查, -# 如果报错就抛出我们自定义异常`ParamsError` - -def add(a, b): - print("做加法,结果:") - return a + b - -def sub(a, b): - print("做减法,结果:") - return a - b - -def mul(a, b): - print("做乘法,结果:") - return a * b - -class ParamsError(Exception): - pass -def div(a, b): - try: - print("做除法,结果:") - return a / b - except ZeroDivisionError: - raise ParamsError("除法中,分母不能为0") - finally: - print("div end") - - -def intdiv(a, b): - try: - print("整除结果:") - return a // b - except ZeroDivisionError: - raise ParamsError("除法中,分母不能为0") - finally: - print("intdiv end") - - - -def yu(a, b): - print("余数:") - return a % b - -def sqrt(a): - print("平方根计算结果是:") - return float(a ** (1/2)) - - -if __name__=="__main__": - print("1加法,2减法,3乘法,4除法,5整除,6取余,7平方根") - try: - n = int(input("请输入对应数字,选择计算方法:")) - except Exception as e: - print(e) - print("您选择的计算方法有误。!") - else: - iList = [1, 2, 3, 4, 5, 6, 7] - if n in iList: - if n == 7: - m1 = int(input("请输入一个数字:")) - print(sqrt(m1)) - else: - m1 = int(input("请输入第一个数字:")) - m2 = int(input("请输入第二个数字:")) - if n == 6: - print(yu(m1, m2)) - elif n == 5: - print(intdiv(m1, m2)) - elif n == 4: - print(div(m1, m2)) - elif n == 3: - print(mul(m1, m2)) - elif n == 2: - print(sub(m1, m2)) - elif n == 1: - print(add(m1, m2)) - else: +# 作业3 +# 在第二周-第一节课我们实现的简单计算器的基础上, 对参数进行检查, +# 如果报错就抛出我们自定义异常`ParamsError` + +def add(a, b): + print("做加法,结果:") + return a + b + +def sub(a, b): + print("做减法,结果:") + return a - b + +def mul(a, b): + print("做乘法,结果:") + return a * b + +class ParamsError(Exception): + pass + + + +def intdiv(a, b): + try: + print("整除结果:") + return a // b + except ZeroDivisionError: + raise ParamsError("除法中,分母不能为0") + finally: + print("intdiv end") + + + +def yu(a, b): + print("余数:") + return a % b + +def sqrt(a): + print("平方根计算结果是:") + return float(a ** (1/2)) + + +if __name__=="__main__": + print("1加法,2减法,3乘法,4除法,5整除,6取余,7平方根") + try: + n = int(input("请输入对应数字,选择计算方法:")) + except Exception as e: + print(e) + print("您选择的计算方法有误。!") + else: + iList = [1, 2, 3, 4, 5, 6, 7] + if n in iList: + if n == 7: + m1 = int(input("请输入一个数字:")) + print(sqrt(m1)) + else: + m1 = int(input("请输入第一个数字:")) + m2 = int(input("请输入第二个数字:")) + if n == 6: + print(yu(m1, m2)) + elif n == 5: + print(intdiv(m1, m2)) + elif n == 4: + print(div(m1, m2)) + elif n == 3: + print(mul(m1, m2)) + elif n == 2: + print(sub(m1, m2)) + elif n == 1: + print(add(m1, m2)) + else: print("您选择的计算方法有误。-------") \ No newline at end of file diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2544\345\221\2500104-0110/4-2homework/4-2-001.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2544\345\221\2500104-0110/4-2homework/4-2-001.py" new file mode 100644 index 00000000..8d9b1442 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2544\345\221\2500104-0110/4-2homework/4-2-001.py" @@ -0,0 +1,75 @@ +# 用类封装一个`MyMath`类, 实现加, 减, 乘, 除, 幂, 开方 +class ParamsError(Exception): + pass + +class MyMath: + def add(self,a, b): + print("做加法,结果:") + return a + b + def sub(self,a, b): + print("做减法,结果:") + return a - b + def mul(self,a, b): + print("做乘法,结果:") + return a * b + def div(self,a, b): + try: + print("做除法,结果:") + return a / b + except ZeroDivisionError: + raise ParamsError("除法中,分母不能为0") + finally: + print("div end") + + def intdiv(self,a, b): + try: + print("整除结果:") + return a // b + except ZeroDivisionError: + raise ParamsError("除法中,分母不能为0") + finally: + print("intdiv end") + + def yu(self,a, b): + print("余数:") + return a % b + + def sqrt(self,a): + print("平方根计算结果是:") + return float(a ** (1/2)) + + + + + +if __name__=="__main__": + print("1加法,2减法,3乘法,4除法,5整除,6取余,7平方根") + m=MyMath() + try: + n = int(input("请输入对应数字,选择计算方法:")) + except Exception as e: + print(e) + print("您选择的计算方法有误。!") + else: + iList = [1, 2, 3, 4, 5, 6, 7] + if n in iList: + if n == 7: + m1 = int(input("请输入一个数字:")) + print(m.sqrt(m1)) + else: + m1 = int(input("请输入第一个数字:")) + m2 = int(input("请输入第二个数字:")) + if n == 6: + print(m.yu(m1, m2)) + elif n == 5: + print(m.intdiv(m1, m2)) + elif n == 4: + print(m.div(m1, m2)) + elif n == 3: + print(m.mul(m1, m2)) + elif n == 2: + print(m.sub(m1, m2)) + elif n == 1: + print(m.add(m1, m2)) + else: + print("您选择的计算方法有误。-------") diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2544\345\221\2500104-0110/4-2homework/4-2-002.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2544\345\221\2500104-0110/4-2homework/4-2-002.py" new file mode 100644 index 00000000..df2040b9 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2544\345\221\2500104-0110/4-2homework/4-2-002.py" @@ -0,0 +1,43 @@ +# 自由课题, 大家按自己的想法, 将身边的事物抽象出一个类, 并创建多个实例 +class Animal(object): + subject='animal' + def __init__(self,obj): + self.p=obj + + def show_type(self): + print("i am animal") + +print(Animal) # + +ani = Animal("anima1") +print(ani) #<__main__.Animal object at 0x000002419E826400> +print(ani.p) + +ani2 = Animal("anima2") +print(ani2) #<__main__.Animal object at 0x000002419E826400> +print(ani2.p) + + +class Dog(Animal): + def __init__(self,name,calls): + self.name=name + self.calls = calls + def displayCall(self): + print("my calls is:", self.calls) + + +class Cat(Animal): + def __init__(self,name,calls): + self.name=name + self.calls = calls + def displayCall(self): + print("my calls is:" ,self.calls) + +dog=Dog("dog","wangwang") +cat=Cat("cat","miaomiao") +print(dog.name) +print(cat.name) +dog.displayCall() +cat.displayCall() +dog.show_type() + diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2544\345\221\2500104-0110/4-2homework/4-2-003.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2544\345\221\2500104-0110/4-2homework/4-2-003.py" new file mode 100644 index 00000000..db9ddb96 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2544\345\221\2500104-0110/4-2homework/4-2-003.py" @@ -0,0 +1,81 @@ + + + +# !!!!!! +class GoGamer(object): + subject = 'go' +print(GoGamer) +# +kejie = GoGamer() +print(kejie) +# <__main__.GoGamer object at 0x0000020DA3AE6400> + +class GoGamer(object): + subject = 'go' #属性 + def __init__(self, obj): + self.p = obj #属性 + +kejie = GoGamer("金勺子") +print(f"柯洁含着{kejie.p}出生") #kejie.p +baoyu = GoGamer("宝玉") +print(f"宝玉含着{kejie.p}出生") #kejie.p +# 柯洁含着金勺子出生 +# 宝玉含着金勺子出生 +class ChineseGoGamer(GoGamer):#中国 + nation = 'cn' + +class KoreaGoGamer(GoGamer): #韩国 + nation = 'kr' +kejie=ChineseGoGamer("金勺子") +print(kejie.subject) +print(kejie.nation) +# go +# cn + +class A: + def __init__(self): + print("init A") + +class B: + def __init__(self): + print("init B") + + +# class C(A, B): +# pass + +class C(A, B): + def __init__(self): + super(A, self).__init__() + +c=C() #init A +print(C.__mro__) +#(, , , ) + +print(C.mro()) +# [, , , ] + +class A: + def __init__(self): + print("init A") + +class B: + def __init__(self): + print("init B") + + +class C(A, B): + def __init__(self): #覆盖,达到自定义的目的; + print("init C") + + +class D(A, B): + def a(self,a): + return a + def a(self,a,b): #自动去选择相对应的方法 + return a**b + + +def my_power(a,b): + res=a**b + return res diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2544\345\221\2500104-0110/4-2homework/4-2-lianxi.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2544\345\221\2500104-0110/4-2homework/4-2-lianxi.py" new file mode 100644 index 00000000..e69de29b -- Gitee From 0cef4bb70bcbdbc77e4ed96751aec5a65ac97348 Mon Sep 17 00:00:00 2001 From: yunyunyun <18883994582@163.com> Date: Fri, 8 Jan 2021 17:45:33 +0800 Subject: [PATCH 2/2] =?UTF-8?q?[0108-=E7=AC=94=E8=AE=B0=E6=B7=BB=E5=8A=A0]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...250_\347\254\2542\350\212\202(0107yun).md" | 225 ++++++++++++++++++ 1 file changed, 225 insertions(+) create mode 100644 "\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2544\345\221\2500104-0110/4-2homework/4-2-\347\254\2544\345\221\250_\347\254\2542\350\212\202(0107yun).md" diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2544\345\221\2500104-0110/4-2homework/4-2-\347\254\2544\345\221\250_\347\254\2542\350\212\202(0107yun).md" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2544\345\221\2500104-0110/4-2homework/4-2-\347\254\2544\345\221\250_\347\254\2542\350\212\202(0107yun).md" new file mode 100644 index 00000000..e3f0ae5a --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/5\347\217\255/5\347\217\255_\344\272\221/\347\254\2544\345\221\2500104-0110/4-2homework/4-2-\347\254\2544\345\221\250_\347\254\2542\350\212\202(0107yun).md" @@ -0,0 +1,225 @@ +# 第4周_第2节(0107周四) + +## 类的创建, 实例化, 初始化 + +- 什么是类 + + 类就是拥有相同功能或者相同属性的对象集合 + +- 类的创建 + + - `object`是所有类的基类 + + ```python + class GoGamer(object): + subject = 'go' + print(GoGamer) + # + ``` + +- 类的实例化 + + 实例就是抽象概念的具象化 + + ```python + kejie = GoGamer() + print(kejie) + # <__main__.GoGamer object at 0x0000020DA3AE6400> + ``` + +- 类的初始化 + + 类创建一个新实例的时候,会默认调用`__init__`这样一个特殊方法 + + ```python + + class GoGamer(object): + subject = 'go' #属性 + def __init__(self, obj): #初始化时候进行一些绑定; self指代还未实例化的实例,self换成a也是可以 + self.p = obj #属性 + + kejie = GoGamer("金勺子") + print(f"柯洁含着{kejie.p}出生") #kejie.p + baoyu = GoGamer("宝玉") + print(f"宝玉含着{kejie.p}出生") #kejie.p + # 柯洁含着金勺子出生 + # 宝玉含着金勺子出生 + ``` + + - 关于`self` + + 指代还未实例化的实例 + +## 面向对象 + +- 面向过程 + + - `程序=数据结构+算法` + + ```python + def my_power(a,b): + res=a**b + return res + ``` + + + + - 强调的是一个实现的细节 + +- 面向对象 + + 完成对越来越庞大项目代码以及对外公开接口,的归类和重用, 是一种更高级的抽象. + + - 通过什么手段来完成上述目的? 继承、多态(覆盖)、封装; + + - 继承 + + ```python + class ChineseGoGamer(GoGamer):#中国 + nation = 'cn' + + class KoreaGoGamer(GoGamer): #韩国 + nation = 'kr' + + kejie=ChineseGoGamer("金勺子") + print(kejie.subject) + print(kejie.nation) + # go + # cn + ``` + + - 处理多继承冲突 + + - 查看`MRO(mehotd resolution order)` 方法执行顺序 + + ```python + + class A: + def __init__(self): + print("init A") + + class B: + def __init__(self): + print("init B") + + + class C(A, B): + pass + + c=C() #init A + print(C.__mro__) + #(, , , ) 从后往前读 + + ``` + + - 指定类方法的调用顺序(想要是b,找前一个,那就是a) + + ```python + + class C(A, B): + def __init__(self): + super(A, self).__init__() + + c=C() #init B + print(C.__mro__) + #(, , , ) + + print(C.mro()) + # [, , , ] + + ``` + + - `super`函数源码 + + ```python + def super(cls, inst): + mro = inst.__class__.mro() + return mro[mro.index(cls) + 1] + + def super(类, 实例): + # 获取当前实例的方法解析顺序 + mro = 实例.类.mro() + return mro[mro.index(类) + 1] + ``` + +- 多态 + + 方式为覆盖和重载; py中只有覆盖; + + - 覆盖(子类和父类之间的, 是垂直的关系) + + 子类可以继承父类的所有属性和方法, 但是同时子类也可以重写父类的属性和方法, 达到自定义的目的. + + ```python + + class A: + def __init__(self): + print("init A") + + class B: + def __init__(self): + print("init B") + + + class C(A, B): + def __init__(self): #覆盖,达到自定义的目的; + print("init C") + ``` + + - 重载 (类中的方法和方法之间的, 是水平关系) py中没有; + + Python中式没有重载, 但是可以用装饰器来实现该功能. + + ```python + + class D(A, B): + def a(self,a): + return a + def a(self,a,b): #自动去选择相对应的方法 + return a**b + ``` + + + +- 封装 + + ``` + usb接口:u盘、外部设备,鼠标,键盘; + type-C:充电 + hdmi:连接显示器 + ``` + + 把客观事物封装成抽象的类, 隐藏实现细节, 使得代码模块化. + +## 课后作业 + +- 用类封装一个`MyMath`类, 实现加, 减, 乘, 除, 幂, 开方 + + ```python + class MyMath: + def add(self, a, b): + return a + b + .... + ``` + +- 自由课题, 大家按自己的想法, 将身边的事物抽象出一个类, 并创建多个实例 + + ```python + 例: + class Book: + content + def __init__(self, sig): + self.sig = sig + ``` + +- 创建多个继承作业2父类的子类 + + ```python + class ToolBook(Book): + pass + ``` + + + +菜鸟:https://www.runoob.com/python/python-object.html + -- Gitee