Field:
Static fields
Normal field
PS: Static field code is loaded when it has been created
Method
All methods belong to the class
1. Common method: At least one self, object execution
2, static method: Arbitrary parameters, class execution (object execution)
3. Class method : At least one CLS, class execution (object execution)
Property
No matter what kind of thing
A form of writing with a method, with a field access form
def func (ARG):
Print (ARG)
Func (1)
Func ("Alex")
Func ([11,22,33])
C#/java
def func (int arg): #指定类型
Print (ARG)
Func (123)
Func ("Alex") #报错
How is it implemented?
Class A:
Pass
Class B (A):
Pass
Class C (A):
Pass
#arg参数, must be a subtype of type A or a
def func (A Arg):
Print (ARG)
obj = B ()
obj = C ()
obj = A ()
Func (obj)
Field
Class Foo:
CC = 123 #静态字段
def __init__ (self):
Self.name = ' Alex ' #普通字段
Def show (self):
Print (Self.name)
Field:
In general, you access your own fields
Rules:
Normal fields can only be accessed with objects
Static fields are accessed using classes (object access is only a last resort)
Method:
Normal method At least one self, object execution
static method arbitrary arguments, class execution (object execution)
class method At least one of the CLS classes executed (object execution)
@staticmethod
def f1 (A1,A2):
Print (A1,A2)
def f2 (CLS):
Print (CLS) #cls类名, () Create object
obj = Province (' Beijing ')
Obj.show
Property
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)
#p. all_count# Fields
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
Property 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
Member modifiers
Private:
can only be accessed within the class itself members
Public
Pass
Class Foo:
#构造方法
def __init__ (self, name, age)
Self.name = Name
Self.age = Age
#析构方法
def __del__ (self):
Pass
def __call__ (self):
Print (' call ')
def __str__ (self):
Return "%s-%s"% (Self.name, self.age)
obj = Foo ()
Obj () #对象 () executes the call method
#Foo () ()
Obj1 = Foo (' Alex ', 77)
ret = str (obj1) #str method
Print (ret)
#__dict__获取对象中封装的数据
RET = obj1.__dict__
Print (ret)
def __getitem__ (self, item):
#item. Start,item.stop,item.step
Return 123
def __setitem__ (self, Key, value):
Print (' SetItem ')
def __delitem__ (self, key):
Print (' del item ')
DIC = {"K1": 123} #dic = Dict (k1=123)
dic[' K1 '] #dic ()
dic[' k1 '] = 123
Del dic[' K1 ']
#!/usr/bin/env python
#-*-Coding:utf-8-*-
# Author:alex Li
Class Foo:
# construction Method
def __init__ (self, name,age):
Self.name = Name
Self.age = Age
# destructor Method
def __del__ (self):
Pass
def __call__ (self):
Print (' call ')
def __str__ (self):
Return "%s-%d"% (Self.name, self.age)
def __add__ (self, Other):
temp = "%s-%d"% (self.name,other.age)
Return Temp
def __getitem__ (self, item):
# Print (Type (item), item)
# Item.Start Item.stop Item.step
Print (Type (item))
Return 123
def __setitem__ (self, Key, value):
# Key.start Key.stop Key.step
Print (Type (key), type (value))
def __delitem__ (self, key):
# Key.start Key.stop Key.step
Print (Type (key))
obj = Foo (' Alex ', 73)
# obj () # Call
# syntax Correspondence relationship
# Ret1 = obj[' ad ')
Ret2 = Obj[1:4:2]
Obj[1:4] = [11,22,33,44,66]
Del Obj[1:4]
# Print (ret)
# obj[' k1 ') = 111
# del obj[' K1 ')
# dic = {' K1 ': 123} # dic = Dict (k1=123)
# dic[' K1 ' # dic ()
# dic[' k1 ') = 123
# del dic[' K1 ')
obj = Foo ()
Obj () # object () performs a call
# Foo () ()
Obj1 = Foo (' Alex ', 73)
Obj2 = Foo (' Eric ', 84)
# Get the data encapsulated in the object
RET = obj1.__dict__
Print (ret)
# Print (foo.__dict__)
# Print (OBJ1)
# Print (OBJ2)
# ret = str (OBJ1)
# Print (ret)
# ret = obj1 + obj2
# Print (ret)
# __dict__
Class C1:
def F1 (self):
Print (' C1,f1 ')
Return 123
Class C2 (C1):
def F1 (self):
# actively executes the parent class F1 method
ret = Super (C2, self). F1 ()
Print (' C2.f1 ')
return ret
# C1.F1 (self)
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):
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)
Members of classes in object-oriented