#!/usr/bin/env python#-*-coding:utf-8-*-class Foo: #定义类 def Mail (self,email,message): #定义类的方法 print (' Send mail to%s! letter Interest:%s '% (email,message)) return trueobj = Foo (), #创建对象obj. Mail (' [email protected] ', ' Test message Information 123 ') #对象调用方法 the self parameter is not assigned Value, the system automatically assigns a # class object to the wrapper and call class Sqlhelper:def fetch (SELF,SQL): #这里的self refers to an object that is instantiated later, obj, all of which can be assigned the value of the Obj object Obj.hhost,obj.uuserane , obj.pwd print (self.hhost) print (self.uuserane) print (self.pwd) print (sql) obj = SQLHelper () obj . hhost= "c1.salt.com" obj.uuserane= "Alex" obj.pwd= "123" Obj.fetch ("SELECT * from user") #========> when to use object-oriented? When some functions in a function have the same parameters, you can use an object-oriented approach to encapsulate the value of the parameter once to the object, and then # go to the object to value the public private =====>class of the #=====> class person:name= "small Crucian carp" #公有变量 __ Home= "CQ" #两个下划线 represents the private variable of the class Def take (self): print (Self.message) P=person () print (p.name) #公有变量访问p. message= "I am the Person class" p . Take () #====> class inheritance ====>class man: #从Person类继承 Passm=man () m.message= "I Am Man" m.take () #从父类继承了take方法 #=== The combination of = = Class ====> to combine several classes that cannot be inherited into a class # The following example combines turtles and fishes into a pond class ClasS turtle:def __init__ (self,x): Self.num=xclass fish:def __init__ (self,y): Self.num=yclass pool:d EF __init__ (self,x,y): self.x = Turtle (x) self.y = Fish (y) def print_num (self): Print ("The pool has%d fish and%d Turtle "% (self.x.num,self.y.num)) pool = Pool (1,10) pool. Print_num ()
Python Basic Learning 1-Object oriented