Python Object-oriented foreplay
For Python, it can be done entirely using functional programming or object-oriented programming, and Java can only be object-oriented.
Object-oriented:
is just an alias
def foo (name,age,gender,content):
Print (name,age,gender,content)
Foo (Xiao Ming, 10 years old, male, go up the hill to chop Wood)
Foo (Xiao Ming, 10 years old, male, driving to Tohoku)
Class Bar:
def foo (self,name,age,gender,content):
Print (name,age,gender,content)
Obj=bar ()
Obj.foo (Xiao Ming, 10 years old, male, go up the hill to chop Wood)
Obj.foo (Xiao Ming, 10 years old, male, driving to northeast)
Object-oriented process:
A. Definition:
Function:
def function name (parameter)
Object-oriented:
Class Name:
def method Name (Self,args)
After the class has a method, the first parameter must write self
Two. Implementation
Function:
Function name (parameter)
Object-oriented:
O= class name () Create a man-in-the-middle O, called an object or instance
O. Method Name ()
class first_class(): def info(self): print("ok!") return Trueo=first_class()o.info()print(o.info())
Three. What does self mean
S1= "123" is equivalent to S1=str ("123") str itself is a class
Self equals the current execution object
class sec_class(): def info(self,args): print(self,args)obj1=sec_class()obj2=sec_class()print(obj1)# <__main__.sec_class object at 0x000000AF402EAAC8>print(obj1.info("haha"))# <__main__.sec_class object at 0x000000AF402EAAC8> haha# Noneprint(obj2)# <__main__.sec_class object at 0x00000065B76BAA20>print(obj2.info("wawa"))# <__main__.sec_class object at 0x00000065B76BAA20> wawa# None
Objects inside the store, can enter the method, could!
class third_class(): def info(self,args): print(self,self.name,args)obj3=third_class()obj3.name="jiaxin"print(obj3.info("kaka3"))# <__main__.third_class object at 0x0000004EB372AC88> jiaxin kaka3
14.1Python Object-oriented foreplay