面向对象

Posted 2722127842qq-123

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了面向对象相关的知识,希望对你有一定的参考价值。

一:面向过程 与 面向对象

面向过程:

核心是‘过程’二字
过程的终极奥义:将程序流程化
过程是‘流水线’,用来分步骤解决问题的

面向对象:

核心是‘对象’二字
对象的终极奥义:将程序‘整合’
对象是‘容器’,用来盛放 数据 与 功能的

程序 = 数据 + 功能

二:实现方法

初级版

# 学生的数据
stu_name = xxq
stu_age = 18
stu_gender = male


# 学生的功能
def tell_stu_info():
    print(f学生信息 - 名字:{stu_name} 年龄:{stu_age} 性别:{stu_gender})


# 课程的数据
course_name = python
course_period = 6month
course_score = 10


# 课程的功能
def tell_course_info():
    print(f课程信息 - 名称:{course_name} 周期:{course_period} 学分:{course_score})


def set_info():
    global stu_name
    global stu_age
    global stu_gender

    stu_name = egon
    stu_age = 80
    stu_gender = female

    tell_stu_info()


set_info()

 

进阶版

# 学生的功能
def tell_stu_info(stu_obj):
    print(学生信息 - 名字:%s 年龄:%s 性别:%s % (
        stu_obj[stu_name],
        stu_obj[stu_age],
        stu_obj[stu_gender]
    ))


def set_info(stu_obj, x, y, z):
    stu_obj[stu_name] = x
    stu_obj[stu_age] = y
    stu_obj[stu_gender] = z
    # stu_name = ‘egon‘
    # stu_age = 80
    # stu_gender = ‘female‘


stu_obj = {
    stu_name: xxq,
    stu_age: 18,
    stu_gender: male,
    tell_stu_info: tell_stu_info,
    set_info: set_info,
}

print(stu_obj)

 

高级版

# 学生的功能
def tell_stu_info(stu_obj):
    print(学生信息 - 名字:%s 年龄:%s 性别:%s % (
        stu_obj[stu_name],
        stu_obj[stu_age],
        stu_obj[stu_gender]
    ))


def set_info(stu_obj, x, y, z):
    stu_obj[stu_name] = x
    stu_obj[stu_age] = y
    stu_obj[stu_gender] = z
    # stu_name = ‘egon‘
    # stu_age = 80
    # stu_gender = ‘female‘


stu_obj = {
    stu_school: oldboy,
    stu_name: xxq,
    stu_age: 18,
    stu_gender: male,
    tell_stu_info: tell_stu_info,
    set_info: set_info,
}

stu1_obj = {
    stu_school: oldboy,
    stu_name: qwe,
    stu_age: 18,
    stu_gender: female,
    tell_stu_info: tell_stu_info,
    set_info: set_info,
}

print(stu_obj)

 

 

实现面向对象

 

Python中的面向对象语法

类 是对象相似数据 与 功能的 集合体

所以,类中最常见的是变量 与 函数 的定义,但是 类体中 其实是可以包含任意其他代码的

注意:类体代码 是在 类定义阶段就会立即执行的

class Student:
    # 1.变量的定义
    stu_school = oldboy

    # 2.功能的定义
    def tell_stu_info(stu_obj):
        print(学生信息 - 名字:%s 年龄:%s 性别:%s % (
            stu_obj[stu_name],
            stu_obj[stu_age],
            stu_obj[stu_gender]
        ))

    def set_info(stu_obj, x, y, z):
        stu_obj[stu_name] = x
        stu_obj[stu_age] = y
        stu_obj[stu_gender] = z

    # print(‘=======>‘)
    
# 属性访问的语法
# 1.访问数据属性
# 2.访问函数属性
# print(Student.__dict__)         # {‘__module__‘: ‘__main__‘, ‘stu_school‘: ‘oldboy‘, ‘tell_stu_info‘: <function Student.tell_stu_info at 0x035B8418>, ‘set_info‘: <function Student.set_info at 0x035B83D0>, ‘__dict__‘: <attribute ‘__dict__‘ of ‘Student‘ objects>, ‘__weakref__‘: <attribute ‘__weakref__‘ of ‘Student‘ objects>, ‘__doc__‘: None}
# print(Student.stu_school)       # oldboy

# 再调用类产生对象
stu1_obj = Student()
stu2_obj = Student()
stu3_obj = Student()

# stu1_obj.__dict__[‘stu_name‘] = ‘eogn‘
# stu1_obj.__dict__[‘stu_age‘] = 16
# stu1_obj.__dict__[‘stu_gender‘] = ‘male‘
# print(stu1_obj.__dict__)        # {‘stu_name‘: ‘eogn‘, ‘stu_age‘: 16, ‘stu_gender‘: ‘male‘}

stu1_obj.stu_name = eogn
stu1_obj.stu_age = 16
stu1_obj.stu_gender = male
print(stu1_obj.__dict__)        # {‘stu_name‘: ‘eogn‘, ‘stu_age‘: 16, ‘stu_gender‘: ‘male‘}

 

 
属性查找

一:属性查找

class Student:
    # 1、变量的定义
    stu_school = oldboy
    count = 0

    # 空对象,‘egon‘,18,‘male‘
    def __init__(self, x, y, z):
        Student.count += 1

        self.stu_name = x  # 空对象.stu_name=‘egon‘
        self.stu_age = y  # 空对象.stu_age=18
        self.stu_gender = z  # 空对象.stu_gender=‘male‘
        # return None

    # 2、功能的定义
    def tell_stu_info(self):
        print(学生信息:名字:%s 年龄:%s 性别:%s % (
            self.stu_name,
            self.stu_age,
            self.stu_gender
        ))

    def set_info(self, x, y, z):
        self.stu_name = x
        self.stu_age = y
        self.stu_gender = z

    def choose(self, x):
        print(正在选课)
        self.course = x


stu1_obj = Student(egon, 18, male)  # Student.__init__(空对象,‘egon‘,18,‘male‘)
stu2_obj = Student(lili, 19, female)
stu3_obj = Student(jack, 20, male)

# print(stu1_obj.count)
# print(stu2_obj.count)
# print(stu3_obj.count)

 

二:类中存放的是对象共有的数据与功能

1.类可以访问:

# 1.类的数据属性
print(Student.stu_school)       # oldboy

# 2.类的函数属性
print(Student.tell_stu_info)        # <function Student.tell_stu_info at 0x00F78460>
print(Student.set_info)             # <function Student.set_info at 0x01728418>

 

2.但其实类中的东西是给对象用的

# 1.类的数据属性是共享给所有对象用的,大家访问的地址都一样
print(id(Student.stu_school))        # 23914752

print(id(stu1_obj.stu_school))       # 23914752
print(id(stu2_obj.stu_school))       # 23914752
print(id(stu3_obj.stu_school))       # 23914752

Student.stu_school=OLDBOY
stu1_obj.stu_school=OLDBOY
print(stu1_obj.stu_school)              # OLDBOY

print(Student.stu_school)               # OLDBOY
print(stu2_obj.stu_school)              # OLDBOY
print(stu3_obj.stu_school)              # OLDBOY

 

3.类中定义的函数主要是给对象使用的,而且是绑定给对象的,虽然所有对象指向的都是相同的功能,但是绑定到不同的对象就是不同的绑定方法,内存地址各不相同

# 类调用自己的函数属性必须严格按照函数的用法来
print(Student.tell_stu_info)        # <function Student.tell_stu_info at 0x00A58460>
print(Student.set_info)             # <function Student.set_info at 0x00A58418>

Student.tell_stu_info(stu1_obj)     # 学生信息:名字:egon 年龄:18 性别:male
Student.tell_stu_info(stu2_obj)     # 学生信息:名字:lili 年龄:19 性别:female
Student.tell_stu_info(stu3_obj)     # 学生信息:名字:jack 年龄:20 性别:male

Student.set_info(stu1_obj,EGON,19,MALE)
Student.tell_stu_info(stu1_obj)     # 学生信息:名字:EGON 年龄:19 性别:MALE


# 绑定方法的特殊之处在于:谁来调用绑定方法就会将谁当做第一个参数自动传入
print(Student.tell_stu_info)        # <function Student.tell_stu_info at 0x01948460>
print(stu1_obj.tell_stu_info)       # <bound method Student.tell_stu_info of <__main__.Student object at 0x0194B0B8>>
print(stu2_obj.tell_stu_info)       # <bound method Student.tell_stu_info of <__main__.Student object at 0x0194B070>>
print(stu3_obj.tell_stu_info)       # <bound method Student.tell_stu_info of <__main__.Student object at 0x0194B250>>

stu1_obj.tell_stu_info()  # tell_stu_info(stu1_obj)     # 学生信息:名字:egon 年龄:18 性别:male
stu2_obj.tell_stu_info()  # tell_stu_info(stu2_obj)     # 学生信息:名字:lili 年龄:19 性别:female
stu3_obj.tell_stu_info()  # tell_stu_info(stu3_obj)     # 学生信息:名字:jack 年龄:20 性别:male


stu1_obj.choose(python全栈开发)
print(stu1_obj.course)  
# 正在选课
# python全栈开发

stu2_obj.choose(linux运维)
print(stu2_obj.course)  
# 正在选课
# linux运维

stu3_obj.choose(高级架构师)
print(stu3_obj.course)  
# 正在选课
# 高级架构师
l1 = [aa, bb, cc]     # l=list([1,2,3])
l2 = [111, 222, 333]        # l=list([1,2,3])
print(l1.append)            # <built-in method append of list object at 0x0114D048>
print(list.append)          # <method ‘append‘ of ‘list‘ objects>

l1.append(dd)
l2.append(dd)
print(l1)                   # [‘aa‘, ‘bb‘, ‘cc‘, ‘dd‘]
print(l2)                   # [111, 222, 333, ‘dd‘]
l1 = [aa, bb, cc]  # l=list([1,2,3])
l2 = [111, 222, 333]  # l=list([1,2,3])

list.append(l1, dd)
list.append(l2, dd)
print(l1)       # [‘aa‘, ‘bb‘, ‘cc‘, ‘dd‘]
print(l2)       # [111, 222, 333, ‘dd‘]

 

 

以上是关于面向对象的主要内容,如果未能解决你的问题,请参考以下文章

面向面试编程代码片段之GC

PHP面向对象之选择工厂和更新工厂

Java中面向对象的三大特性之封装

python之路之前没搞明白4面向对象(封装)

Scala的面向对象与函数编程

Python面向对象学习之八,装饰器