码迷,mamicode.com
首页 > 其他好文 > 详细

07_面向对象

时间:2016-06-21 01:04:19      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:

1. 初识面向对象

1. python支持 函数式+面向对象 两种编程

2. 函数式编程,面向对象编程实现:发邮件的功能

函数式:

def mail(email, message):
    print(“去发吧”)
    return True

# 调用函数
mail("alex3714@126.com", "好人")

面向对象:先创建类,再创建对象

# 创建类
class Foo:

    # 方法(函数放在类里)
    def mail(self, email, message):
    print(“去发吧”)
    return True

# 调用
# 1.创建对象,类名()
obj = Foo()

# 2.通过对象去执行方法
obj.mail("alex3714@126.com", "好人")

3. 类和对象

a) 创建类

class 类名
def 方法名(self, xxxx)
pass

b) 创建对象

对象 = 类名()

c) 通过对象执行方法

对象.方法名(123)

4. 数据库示例

函数式

def fetch(host, username, password, sql):
    pass

def create(host, username, password, sql):
    pass

def remove(host, username, password, nid):
    pass

def modify(host, username, password, name):
    pass

面向对象1

class SQLHelper:
    def fetch(self, host, username, password, sql):
        pass
    def create(self, host, username, password, sql):
        pass
    def remove(self, host, username, password, nid):
        pass
    def modify(self, host, username, password, name):
        pass
obj = SQLHelper()

面向对象2

class SQLHelper:
    def fetch(self, sql):
        # 连接数据库
        print(self.hhost)
        print(self.uusername)
        print(self.pwd)
        print(sql)
    def create(self, sql):
        pass
    def remove(self, nid):
        pass
    def modify(self, name):
        pass
obj = SQLHelper()
obj.hhost = "c1.salt.com"
obj.uusername = "alex"
obj.pwd = "123"

obj.fetch("select * from A")

小结

1. 类和对象的创建与执行

# 创建类
class 类名:
def 方法名(self, xxxx):
pass

# 创建对象
对象 = 类名()

# 通过对象执行方法
对象.方法名(123)

2. 什么时候用面向对象

当某一些函数具有参数时,可以使用面向对象的方式,将参数值一次性的封装到对象,以后去对象中取值即可。

2. self是什么鬼

self是一个Python自动会给传值的参数

 

哪个对象执行方法,self就是谁。

obj1.fetch("select * from A")    self == obj1

obj2.fetch("select * from A")    self ==obj2

 

3. 构造方法__init__

类中有一个特殊的方法__init__,类()自动被执行

class SQLHelper:
    # 构造方法__init__,a1,a2,a3为参数,接收对象参数
    def __init__(self, a1, a2, a3):
        print("自动执行init")
        self.hhost = a1
        self.uusername = a2
        self.pwd =a3

    def fetch(self, sql):
        pass
    def create(self, sql):
        pass
    def remove(self, nid):
        pass
    def modify(self, name):
        pass

# 对象,对象参数
obj1 = SQLHelper("c1.salt.com", ‘alex‘, ‘123‘)
# 执行对象
obj1.fetch("select * from A") 

4. 面向对象三大特性之封装

面向对象三大特性:封装、继承、多态

封装:初级

class Person:
    def __init__(self, name, age, money):
        self.name = name
        self.age = age
        self.money = money

    def shopping(self):
        self.money = self.money - 2

long = Person(‘龙‘, 18, -1)
hu = Person(‘虎‘, 18, 2)
bao = Person(‘豹‘, 18, 10)

bao.shopping()
print(bao.money)

封装对象

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_obj = c2(‘aa‘, 11)
# 创建对象c1,参数2为对象c2_obj,c2_obj的name,age
c1_obj = c1("alex", c2_obj)
# c1_obj里的obj下的age
print(c1_obj.obj.age, c1_obj.obj.name, c1_obj.name)

封装:终级

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)
class c3:
    def __init__(self, a1):
        self.money = 123
        self.aaa = a1

c2_obj = c2(‘c2‘, 11)
# c2_obj是c2类型
# - name = "aa"
# - age = 11
c1_obj = c1("c1", c2_obj)
# c1_obj是c1类型
# - name = "c1"
# - obj = c2_obj
c3_obj = c3(c1_obj)
print(c3_obj.aaa.obj.name)
# 使用c2_obj执行show方法
c3_obj.aaa.obj.show()
# print(c3.aaa)

5. 面积对象三大特性之继承

Python可以多继承

5.1 单继承

单继承练习1:

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.show()
obj.foo()

  

 

单继承练习2:

class S1:
    def F1(self):
        self.F2()
    def F2(self):
        pass
class S2(S1):
    def F3(self):
        self.F1()
    def F2(self):
        pass

obj = S2()
obj.F3()
# 执行结果:S2的F2

obj = S1()
obj.F1()
# 执行结果:S1的F2

小结:只要出现self,就回到原点从下向上查找

 

1.5.2 多继承

多继承练习1:简单格式

class C1:
    def f1(self):
        print("C1 f1")
        pass

class C2:
    def f2(self):
        print("C2 f2")
        pass
class C3(C2,C1):
    def f3(self):
        print("C3 f3")
        pass

obj = C3()
obj.f3()1
obj.f2()
obj.f1()

# 输出结果
C3 f3
C2 f2
C1 f1

  

 

多继承:多层父类

class C0():
    def f2(self):
        print("C0.f2")
class C1(C0):
    def f1(self):
        print("C1.f1")
        pass
class C2:
    def f2(self):
        print("C2.f2")
        pass
class C3(C1,C2):
    def f3(self):
        print("C3.f3")
        pass

obj = C3()
obj.f2()


# 输出结果
C0.f2

  

 

class C0():
    def f2(self):
        print("C0.f2")
class C1(C0):
    def f1(self):
        print("C1.f1")
        pass
class C2:
    def f2(self):
        print("C2.f2")
        pass
class C3(C2,C1):
    def f3(self):
        print("C3.f3")
        pass

obj = C3()
obj.f2()

小结:

多继承如有多层父类,且顶级类不唯一时,查找顺序为从左向右,由下至上。

 

 

 

 

有共同顶级父类时查找顺序

class C_2():
    def f2(self):
        print("C_2.f2")
class C_1(C_2):
    def f1(self):
        print("C_1.f1")
class C0(C_2):
    def f1(self):
        print("C0.f1")
class C1(C0):
    def f1(self):
        print("C1.f1")
        pass
class C2(C_1):
    def f2(self):
        print("C2.f2")
        pass
class C3(C1,C2):
    def f3(self):
        print("C3.f3")
        pass

obj = C3()
obj.f2()


# 输出结果
c2.f2

技术分享

 

07_面向对象

标签:

原文地址:http://www.cnblogs.com/liangdalong/p/5602227.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!