面向对象编程day2

一个面向对象简化过程的例子

HOST=‘127.0.0.1’PORT=3306DB=‘db1’CHARSET=‘utf8’

def exc1(host,port,db,charset):
conn=connect(host,port,db,charset)
conn.execute(sql)
return xxx


def exc2(host,port,db,charset,proc_name):
conn=connect(host,port,db,charset)
conn.call_proc(sql)
return xxx

def func():
pass

# exc1('127.0.0.1',3306,'db1','utf8','select * from tb1;')
# exc1('127.0.0.1',3306,'db1','utf8','select * from tb2;')
# exc1('127.0.0.1',3306,'db1','utf8','select * from tb3;')
#
# exc2('127.0.0.1',3306,'db1','utf8','p1')
# exc2('127.0.0.1',3306,'db1','utf8','p2')
# exc2('127.0.0.1',3306,'db1','utf8','p3')

exc1(HOST,PORT,DB,CHARSET,'select * from tb1;')
exc1(HOST,PORT,DB,CHARSET,'select * from tb1;')
exc1(HOST,PORT,DB,CHARSET,'select * from tb1;')

exc2(HOST,PORT,DB,CHARSET,'存储过程的名字')
exc2(HOST,PORT,DB,CHARSET,'存储过程的名字')
exc2(HOST,PORT,DB,CHARSET,'存储过程的名字')
定义成类
# class Mysql:
# def __init__(self,host,port,db,charset):
# self.host=host
# self.port=port
# self.db=db
# self.charset=charset
#
# def exec1(self,sql):
# conn = connect(self.host, self.port, self.db, self.charset)
# conn.execute(sql)
# return xxx
#
# def exc2(self, proc_name):
# conn = connect(self.host, self.port, self.db, self.charset)
# conn.call_proc(proc_name)
# return xxx

1. 什么是继承?
在程序中继承是一种新建子类的方式,新创建的类称之为子类派生类,被继承的类称之为父类基类超类
继承描述的是一种遗传关系,子类可以重用父类的属性

2. 为何用继承?
减少类与类之间代码冗余的问题

3. 如何继承
先抽象再继承
# python2与python3在继承上的区别
# 新式类:但凡继承object类的子类,以及该子类的子子类,...都称之为新式类
# 经典类:没有继承object类的子类,以及该子类的子子类,...都称之为经典类

# 只有在python2中才区分新式类与经典类

在子类派生出的新功能中如何重用父类的功能:
方式一: 指名道姓地访问某一个类中的函数,与继承无关(需要手动传对象)

class OldboyPeople:
school = 'Oldboy'
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender


class OldboyStudent(OldboyPeople):
# def __init__(self, name, age, gender):
# self.name = name
# self.age = age
# self.gender = gender

def choose_course(self):
print('%s is choosing course' %self.name)

class OldboyTeacher(OldboyPeople):
# tea, 'egon', 18, 'male', 10, 3000
def __init__(self, name, age, gender,level,salary):
# self.name = name
# self.age = age
# self.gender = gender
OldboyPeople.__init__(self, name, age, gender)
self.level=level
self.salary=salary

def score(self,stu,num):
stu.num=num
print('老师%s给学生%s打分%s' %(self.name,stu.name,num))

在单继承背景下,无论新式类还是经典类属性查找顺序都一样
obj----类------父类---------

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

在多继承背景下,如果一个子类继承了多个分支,但是多个分支没有汇聚到一个非object类,无论是新式类还是经典类属性查找顺序都一样:
会按照从左到右的顺序一个分支一个分支的查找下去.

在多继承背景下,如果一个子类继承了多个分支,但是多个分支最终汇聚到一个非object类(菱形继承问题)
新式类:广度优先查找:obj->A->B->E->C->F->D->G->object
经典类:深度优先查找:obj->A->B->E->G->C->F->D

方式二: super(OldboyTeacher,self),在python3中super可以不传参数,调用该函数会得到一个特殊的对象,该对象是专门用来访问父类中属性,
强调:super会严格参照类的mro列表依次查找属性

class OldboyPeople:
school = 'Oldboy'
def __init__(self, name, age, gender):
self.name = name
self.age = age
self.gender = gender

class OldboyTeacher(OldboyPeople):
# tea, 'egon', 18, 'male', 10, 3000
def __init__(self, name, age, gender,level,salary):
super(OldboyTeacher,self).__init__(name, age, gender)(严格按照mro列表的关系,不用传第一个参数)
self.level=level
self.salary=salary

def score(self,stu,num):
stu.num=num
print('老师%s给学生%s打分%s' %(self.name,stu.name,num))

tea=OldboyTeacher('egon',18,'male',10,3000) #__init___(tea,'egon',18,'male',10,3000)
print(tea.__dict__)
# print(stu.school)
原文地址:https://www.cnblogs.com/endlesswaltz/p/9839511.html