静态方法的使用:将内部函数与类属性隔断 所有可以不需要加self参数,相当于一个独立的函数,基本用不到。1
2
3
4
5
6
7
8
9
10
11
12
13# Author: Diedline
class Dog(object):
def __init__(self,name):
self.name = name
def eat(self):
print("%s is eating %s "%(self.name,"dd"))
d = Dog("李小刀")
d.eat(d) #但是可以通过调用类对象来继续类的关联
类装饰器是只能访问类变量,不能访问实例变量。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15# Author: Diedline
class Dog(object):
name = "wch" #只能在此处或全局调用参数
def __init__(self,name):
self.name = name
def eat(self):
print("%s is eating %s "%(self.name,"dd"))
d = Dog("李小刀")
d.eat() #但是可以通过调用类对象来继续类的关联
属性方法:@property
把一个方法变成静态属性但在下面可以调用@xxx.setter 来添加属性并且可以通过@xxx.deleter来删除属性,未来很多场景不是你能通过定义一个静态属性来实现的1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26# Author: Diedline
class Dog(object):
name = "wch"
def __init__(self,name):
self.name = name
self.__food =None
# @classmethod #类变量是不能调用类内部方法的变量的,只能在方法外部写属性
def eat(self):
print("%s is eating %s "%(self.name,self.__food))
def eat(self,food):
print("set for %s"%food)
def eat(self):
del self.__food
print("删完了")
d = Dog("李小刀")
d.eat #但是可以通过调用类对象来继续类的关联
d.eat = "包子"
del d.eat
飞机航班使用属性方法例子1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27# Author: Diedline
class Flight(object):
def __init__(self,name):
self.flight_name = name
def checking_status(self):
print("checking flight %s status " % self.flight_name)
return 0
def flight_status(self):
status = self.checking_status()
if status == 0 :
print("flight got canceled...")
elif status == 1 :
print("flight is arrived...")
elif status == 2:
print("flight has departured already...")
else:
print("cannot confirm the flight status...,please check later")
def flight_status(self,status):
print("flight %s has changed status to %s" %(self.flight_name,status))
f = Flight("CA980")
f.flight_status
f.flight_status = 2
特殊方法:
doc
是打印你写在类下面的注释信息的1
2
3
4class Dog(object):
''' 这个类是描述狗这个对象的'''
d = Dog("李小刀")
print(d.__doc__)
输出这个类是描述狗这个对象的
model方法 输出模块
class方法 输出类1
2
3
4
5
6
7# Author: Diedline
from 软件测试作业.dd import c
obj = c()
print(obj.__module__) #输出软件测试作业.dd 即: 输出模块输出
print(obj.__class__) #输出<class '软件测试作业.dd.c'>
Call方法 给对象加个括号就调用了1
2
3
4
5
6
7
8
9
10
11
12
13# Author: Diedline
class Dog(object):
''' 这个类是描述狗这个对象的'''
name = "wch"
def __init__(self,name):
self.name = name
self.__food =None
def __call__(self, *args, **kwargs):
print("%s is being called"%args)
d = Dog("wch")
d("wch")
输出
wch is being called
dict没实例化的时候以字典的形式打印类中所有的方法,实例化后以字典的形式方法显示类中所有属性1
2
3
4
5
6
7
8
9
10
11
12
13
14# Author: Diedline
class Dog(object):
''' 这个类是描述狗这个对象的'''
name = "wch"
def __init__(self,name):
self.name = name
self.__food =None
def __call__(self, *args, **kwargs):
print("%s is being called"%args)
print(Dog.__dict__) #没实例化的时候
d = Dog("wch")
print(d.__dict__) # 输出{'name': 'wch', '_Dog__food': None}
str是传返回值的1
2
3
4
5
6
7
8
9
10
11
12
13# Author: Diedline
class Dog(object):
''' 这个类是描述狗这个对象的'''
name = "wch"
def __init__(self,name):
self.name = name
self.__food =None
def __str__(self):
return "<obj %s>"%self.name
d = Dog("wch")
print(d)
init , getitem,setitem,delitem 将类的实例变成字典1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27# Author: Diedline
"""
将实例变成字典
可以封装成字典
"""
class Foo(object):
def __init__(self):
self.data = {}
def __getitem__(self, key):
print('__getitem__', key)
return self.data.get(key)
def __setitem__(self, key, value):
print('__setitem__', key, value)
self.data[key] =value
def __delitem__(self, key):
print('__delitem__', key)
obj = Foo()
obj['name'] = "alex" #像字典一样赋值
# print(obj['name'])
# print(obj.data)
del obj["sdfdsf"]
# result = obj['k1'] # 自动触发执行 __getitem__
# obj['k2'] = 'alex' # 自动触发执行 __setitem__
# del obj['k1']
神奇的类的构造方法1
2
3
4
5
6
7
8
9
10def __init__(self,name,age):
self.name = name
self.age = age
def coo(self):
print("hello wch!")
Peal = type("Peal",(object,),{"coo":coo, #类生命的起源
"__init__":__init__}) #创建了一个类 object 里面是个元组的形式,所以必须加逗号
f = Peal("wch",55) #对类进行实例化
f.coo()
print(type(Peal))