面向对象式编程
#函数式编程:
def fetch(host, username, password, sql):
pass
def create(host, username, password, sql):
pass
def remove(host, username, password, sql):
pass
def modify(host, username, password, sql):
pass
...
fetch(...)
#面向对象编程
class SQLHelper:
def fetch(self, sql):
pass
def create(self, sql):
pass
def remove(self, sql):
pass
def modify(self, sql):
pass
obj = SQLHelper()
obj.hhost = 'c1.salt.com'
obj.uusername = 'alex'
obj.pwd = "123"
obj.fetch("select * from A")
#__init__(self) 构造方法
#面向对象编程
class SQLHelper:
def __init__(self, a1, a2, a3)
self.hhost = a1
self.uusername = a2
self.pwd = a3
def fetch(self, sql):
pass
def create(self, sql):
pass
def remove(self, sql):
pass
def modify(self, sql):
pass
obj = SQLHelper('c1.salt.com','alex','123')
obj.fetch("select * from A")
~
#类的引用
class c1:
def __init__(self, name, obj):
self.name = name
self.obj = obj
class c2:
def __init__(self, name, age):
self.name = name
self.age = age
def show(self):
print(self.name)
c2_obj = c2('aa',11)
c1_obj = c1('alex', c2_obj) #c1类传入了c2_obj
print(c1_obj.obj.age)
“封装,继承,多态”
#继承
class F1: #父类,基类
def show(self):
print('show')
def foo(self):
print(self.name)
class F2(F1): #子类,派生类
def __init__(self, name):
self.name = name
def bar(self):
print('bar')
def show(self):
print ('F2.show')
obj = F2('alex')
obj.foo()