今日内容:
1、
---------------------------------------------------------------------------------------------------------------------
1、
class Name(object):
def __init__(self,a,b,c,d): # 定义四条边
self.a = a
self.b = b
self.c = c
self.d = d
@staticmethod
def is_valid(a,b,c,d):
for i in [b,c,d]:
if i !=a: # if判断四条边是否相等
return False
else:
return True
def area(self):
if res == True:
area_ = self.a * self.b #求正方形面积
return area_
def main():
name = Name(2,2,2,2)
res = name.is_valid(2,2,2,2)
if res == True:
print(name.area())
main()
---------------------------------------------------------------------------------------------------------------------------------
2)
# @classmethod:获取自身类(cls)中的属性,并且可以更改.
# classmethod 修饰符对应的函数不需要实例化,不需要 self 参数,但第一个参数需要是表示自身类的 cls 参数,可以来调用类的属性,类的方法,实例化对象等
from time import time, localtime, sleep
class Clock(object):
def __init__(self, hour=0, minute=0, second=0):
self._hour = hour
self._minute = minute
self._second = second
@classmethod
def now(cls):
ctime = localtime(time())
return cls(ctime.tm_hour, ctime.tm_min, ctime.tm_sec)
def run(self): """走字"""
self._second += 1
if self._second == 60:
self._second = 0
self._minute += 1
if self._minute == 60:
self._minute = 0
self._hour += 1
if self._hour == 24:
self._hour = 0
def show(self): """显示时间"""
return '%02d:%02d:%02d' %
(self._hour, self._minute, self._second)
def main(): # 通过类方法创建对象并获取系统时间
clock = Clock.now()
while True:
print(clock.show())
sleep(1)
clock.run()
if __name__ == '__main__':
main()
---------------------------------------------------------------------------------------------------------------------
3、继承
用子类对象去替换掉一个父类对象,示例如下:
案例三:
---------------------------------------------------------------------------------------------------------------------
class A(object):
def __init__(self,a1):
self.a = 1000
self.a1 = a1
def sum(self):
sum_ = self.a+self.a1
return sum_
class B(A):
def __init__(self,a1,b1):
self.b1 =b1
A.__init__(self,a1)
def Print(self):
res =self.sum()
print(res)
def main():
b=B(100,'111')
b.Print()
main()
---------------------------------------------------------------------------------------------------------------------
4、嵌套函数
python允许创建嵌套函数,也就是说可以在函数里面定义函数。示例如下:
例1:
例2:
5、装饰器
python装饰器就是用于拓展原来函数功能的一种函数,目的是在不改变原函数名(或类名)的情况下,给函数增加新的功能。示例如下:
例1:
--------------------------------------------------------------------------------------------------------------------
'''
定义了一个函数deco,函数无参
'''
def deco(func):
print("HELLO1")
def warp():
return func()
return warp
@deco
def Joker():
print('hello')
Joker()
--------------------------------------------------------------------------------------------------------------------
例2:
--------------------------------------------------------------------------------------------------------------------
'''
定义了一个函数deco,函数有参
'''
def deco(func):
def warp(H):
print("Joker")
return func(H)
return warp
@deco
def Joker(name):
print('hello %s'%name)
Joker('huwang')
------------------------------------------------------------------------------------------------------------------