面向对象式编程

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()



这篇关于面向对象式编程的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程