面向对象式编程
2021/5/21 20:25:48
本文主要是介绍面向对象式编程,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
#函数式编程:
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()
这篇关于面向对象式编程的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-22项目:远程温湿度检测系统
- 2024-12-21《鸿蒙HarmonyOS应用开发从入门到精通(第2版)》简介
- 2024-12-21后台管理系统开发教程:新手入门全指南
- 2024-12-21后台开发教程:新手入门及实战指南
- 2024-12-21后台综合解决方案教程:新手入门指南
- 2024-12-21接口模块封装教程:新手必备指南
- 2024-12-21请求动作封装教程:新手必看指南
- 2024-12-21RBAC的权限教程:从入门到实践
- 2024-12-21登录鉴权实战:新手入门教程
- 2024-12-21动态权限实战入门指南