字段:
静态字段
普通字段
PS:静态字段代码加载时候,已经创建
方法
所有的方法属于类
1、普通方法:至少一个self,对象执行
2、静态方法:任意参数, 类执行(对象执行)
3、类方法 :至少一个cls, 类执行(对象执行)
属性
不论不类的东西
具有方法的写作形式,具有字段访问形式
def func(arg):
print(arg)
func(1)
func("alex")
func([11,22,33])
C
def func(int arg):
print(arg)
func(123)
func("alex")
如何实现?
class A:
pass
class B(A):
pass
class C(A):
pass
def func(A arg):
print(arg)
obj = B()
obj = C()
obj = A()
func(obj)
字段
class Foo:
CC = 123
def __init__(self):
= 'alex'
def show(self):
print()
字段:
一般情况下,自己访问自己的字段
规则:
普通字段只能用对象访问
静态字段用类访问(万不得已的时候才可以使用对象访问)
方法:
普通方法 至少一个self, 对象执行
静态方法 任意参数, 类执行 (对象执行)
类方法 至少一个cls 类执行 (对象执行)
@staticmethod
def f1(a1,a2):
print(a1,a2)
def f2(cls):
print(cls)
obj = Province('beijing')
obj.show
属性
class Pager:
def __init__(self, all_count):
self.all_count = all_count
@property
def all_pager(self):
a1, a2 = divmod(self.all_count, 10)
if a2 == 0:
return a1
else:
return a1 +1
@all_pager.setter
def all_pager(self, value):
print(value)
@all_pager.deleter:
print('del all_pager')
p = Pager(101)
print(p.all_count)
p.all_count = 102
del p.all_count
ret = p.all_pager
print(ret)
p.all_pager = 111
del p.all_pager
属性2
class Pager:
def __init__(self, all_count):
self.all_count = all_count
def f1(self):
pass
def f2(self):
pass
def f3(self):
pass
foo = property(fget=f1,fset=f2,fdel=f3)
p = Pager(101)
result = p.foo
print(result)
p.foo = 'alex'
del p.foo
成员修饰符
私有:
只能类自己本身成员内部访问
公有
pass
class Foo:
def __init__(self, name, age)
= name
self.age = age
def __del__(self):
pass
def __call__(self):
print('call')
def __str__(self):
return "%s - %s" %(, self.age)
obj = Foo()
obj()
obj1 = Foo('alex',77)
ret = str(obj1)
print(ret)
ret = obj1.__dict__
print(ret)
def __getitem__(self, item):
return 123
def __setitem__(self, key, value):
print('setitem')
def __delitem__(self, key):
print('del item')
dic = {"k1":123}
dic['k1']
dic['k1'] = 123
del dic['k1']
class Foo:
def __init__(self, name,age):
= name
self.age = age
def __del__(self):
pass
def __call__(self):
print('call')
def __str__(self):
return "%s - %d" %(, self.age)
def __add__(self, other):
temp = "%s - %d" %(,other.age)
return temp
def __getitem__(self, item):
print(type(item))
return 123
def __setitem__(self, key, value):
print(type(key),type(value))
def __delitem__(self, key):
print(type(key))
obj = Foo('alex', 73)
ret2 = obj[1:4:2]
obj[1:4] = [11,22,33,44,66]
del obj[1:4]
obj = Foo()
obj()
obj1 = Foo('alex', 73)
obj2 = Foo('eric', 84)
ret = obj1.__dict__
print(ret)
class C1:
def f1(self):
print('c1,f1')
return 123
class C2(C1):
def f1(self):
ret = super(C2, self).f1()
print('c2.f1')
return ret
obj = C2()
obj.f1()
class MyDict(dict):
def __init__(self):
self.li = []
super(MyDict, self).__init__()
def __setattr__(self, key, value):
self.li.append(key)
super(MyDict, self).__setattr__()
def __str__(self):
temp_list = []
for key in self.li:
value = self.get(key)
temp_list.append("'%s':%s'" %(key,value,))
temp_str = "{" + ",".join(temp_list) + "}"
return temp_str
obj = MyDict()
obj['k1'] = 123
obj['k2'] = 456
print(obj)
"""
class Foo:
instance = None
def __init__(self,name):
= name
@classmethod
def get_instance(cls):
#cls类名
if cls.instance:
return cls.instance
else:
obj = cls('alex')
cls.instance = obj
return obj
obj1 = Foo.get_instance()
print(obj1)
obj2 = Foo.get_instance()
print(obj2)