diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_7/1\347\217\255_7-\347\254\254\345\233\233\345\221\250/.keep" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_7/1\347\217\255_7-\347\254\254\345\233\233\345\221\250/.keep" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_7/1\347\217\255_7-\347\254\254\345\233\233\345\221\250/lesson4-1.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_7/1\347\217\255_7-\347\254\254\345\233\233\345\221\250/lesson4-1.py" new file mode 100644 index 0000000000000000000000000000000000000000..5fc7bf1dd10883a259967b4d70988bc34f0d1719 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_7/1\347\217\255_7-\347\254\254\345\233\233\345\221\250/lesson4-1.py" @@ -0,0 +1,45 @@ + +# datetime、str、time 之间的转换 + +import datetime +now = datetime.datetime.now() + +# datetime转str +time_str = now.strftime("%Y-%m-%d %H:%M:%S") + +# str转datetime +str_time = datetime.datetime.strptime(time_str,"%Y-%m-%d %H:%M:%S") + +# datetime转timestamp +time_smp = datetime.datetime.timestamp() + +# timestamp转datetime +smp_time = datetime.datetime.fromtimestamp(time_smp) + + + +# 封装一个函数get_date(day_delta), 如果传入的是-1 , 输出就是字符串日期 + +import datetime +def get_date(): + n = input('若想获取当前日期,请输入数字-1:,退出请按 0') + if n == '-1': + day_delta = datetime.datetime.now() + day_delta = day_delta.strftime('%Y-%m-%d') + print(day_delta) + elif n != '0': + n = input('请重新输入:') + get_date() + else: + exit + return +get_date() + + + + + + + + + diff --git "a/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_7/1\347\217\255_7-\347\254\254\345\233\233\345\221\250/lesson4-2.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_7/1\347\217\255_7-\347\254\254\345\233\233\345\221\250/lesson4-2.py" new file mode 100644 index 0000000000000000000000000000000000000000..4fe3c37f82e500ba2f251825e2b52e9f73b2724d --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_7/1\347\217\255_7-\347\254\254\345\233\233\345\221\250/lesson4-2.py" @@ -0,0 +1,48 @@ +# 用类封装一个MyMath类, 实现加, 减, 乘, 除, 幂, 开方 +class MyMath(object): + # 加法 + def add(self,a,b): + return a + b + # 减法 + def minus(self,a,b): + return a - b + + # 乘法 + def multi(self,a,b): + return a * b + + # 除法 + def divi(self,a,b): + return a / b + +math = MyMath() + +print(math.add(3,4)) + + +# 将身边的事物抽象出一个类, 并创建多个实例 +class Human(object): + def __init__(self,name,gender): + self.name = name + self.gender = gender + +xiaoming = Human('小明','男人') + +def man(self): + print('男人') + +def woman(self): + print('女人') + +print(f'这个{xiaoming.gender}是{xiaoming.name}') + + +# 创建多个继承作业2父类的子类 +class Chinese(Human): + nation = 'China' + +class Japanese(Human): + nation = 'Japan' + +class American(Human): + nation = 'USA' \ 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/1\347\217\255/1\347\217\255_7/1\347\217\255_7-\347\254\254\345\233\233\345\221\250/lesson4-3.py" "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_7/1\347\217\255_7-\347\254\254\345\233\233\345\221\250/lesson4-3.py" new file mode 100644 index 0000000000000000000000000000000000000000..eed59b7d5c3f1bf52f424c259558ed674ad1e680 --- /dev/null +++ "b/\347\254\254\344\272\214\346\234\237\350\256\255\347\273\203\350\220\245/1\347\217\255/1\347\217\255_7/1\347\217\255_7-\347\254\254\345\233\233\345\221\250/lesson4-3.py" @@ -0,0 +1,92 @@ +# 将之前封装的MyMath类中的实例方法改为静态方法, 体会两者的区别. +class MyMath: + @staticmethod + # 加法 + def add(a,b): + return a + b + # 减法 + def minus(a,b): + return a - b + # 乘法 + def multi(a,b): + return a * b + # 除法 + def divi(a,b): + return a / b + +print(MyMath.add(3,4)) + +print(MyMath.minus(3,4)) + +print(MyMath.multi(3,4)) + +print(MyMath.divi(3,4)) + + +# 为上节课自定义类添加以下功能: + +# 添加类属性 +class Human: + def __init__(self,name,gender): + self.name = name + self.gender = gender + +xiaoming = Human('小明','男人') + +def man(self): + print('男人') + +def woman(self): + print('女人') + +print(f'这个{xiaoming.gender}是{xiaoming.name}') + + +# 添加类私有属性 +class Info(): + __person = ['小明','男','17'] + +print(Info._Info__person) + +# 添加类方法或者类的私有属性 +class Human: + def __init__(self,name,gender): + self.name = name + self.gender = gender + + @classmethod + def __new__(cls, *args, **kwargs): + print('new_method has added') + return super().__new__(cls) + +d = Human('xiaoming','male') + + +# 在__init__方法中初始化实例属性 +class Human: + def __init__(self,name,gender,age): + self.name = name + self.gender = gender + self.age = age + +xiaohong = Human('小红','女','18') +print(xiaohong.name) + +# 在__init__方法中绑定私有实例属性 +class Human: + def __init__(self,name,gender,age,shortcoming): + self.name = name + self.gender = gender + self.age = age + self.__short = shortcoming + +xiaohong = Human('小红','女','18','懒惰') +print(xiaohong._Human__short) + +# 在自定义类中实现__str__, 自定义输出格式 +class People(Human): + def __str__(self): + return f'个人信息:{self.name,self.gender,self.age}' + +xiaoqiang = People('小强','男生','16','肥胖') +print(xiaoqiang) \ No newline at end of file