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

面对对象编程

时间:2019-07-20 21:43:35      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:计算   obj   sel   实例   ima   weakref   特征   技能   缺点   

面向对象编程

面向过程编程:像流水线一样

优点:逻辑清晰(逻辑一步一步的,系统)

缺点:扩展性差(上一个函数的输出是下一个函数的输入)

面向对象编程

在python中一切皆对象

对象的概念:就是特征和技能的结合体

对象猪八戒有身高/体重/肥头大耳/三十六变

对象孙悟空有身高/体重/毛脸雷公嘴/七十二变

对象你有身高/体重/piao

对象计算机有重量/大小/编程

面向对象编程:定义出一个一个鲜明独特的对象,然后通过对象之间的交互编程。

优点:扩展性特别强

缺点:逻辑非常复杂

定义类和对象

类:分类/类别

按照学生和老师可以划分两类,一个是学生类,一个是老师类:按照学校划分分为一类,一个校 类

类的概念:一系列具有共同特征或技能的对象

学生类:

? 学生:oldboy

? 技能:选课

现实中,现有对象,再有类

在python中先有类,再有对象。

class 类名(必须得用驼峰体):
    #多个特征
    #多个技能
def f1():  #函数定义阶段,只检测语法,不执行代码
    school = 'oldboy'
    addr = 'shanghai'
    
class OldBoyStudent:  #类的定义阶段,会运行代码
    school = 'oldboy'    #对象
    addr = 'shanghai'
    
    def choose_course(self):
        print('is choosing course')
        
oldboystudent_dict = OldBoyStudent.__dict__  #双下划线开头的方法会在某种情况下自动触发   字典格式
#{'__module__': '__main__', 'school': 'oldboy', 'addr': 'shanghai', 'choose_course': <function OldBoyStudent.choose_course at 0x019A3A50>, '__dict__': <attribute '__dict__' of 'OldBoyStudent' objects>, '__weakref__': <attribute '__weakref__' of 'OldBoyStudent' objects>, '__doc__': None}
print(oldboystudent_dict['school'])  
oldboystudent_dict['choose_course'](1111)   #运行函数

stu1 = OldBoyStudent()   #造对象,实例化对象
print(1,stu1.school)     #1 oldboy
1,stu1.choose_course()   #is chooseing course    

stu2 = OldBoysStudent()
print(2,stu1.school)     #2 oldboy
stu3 =  OldBoyStudent()
print(3,stu1.school)     #3 oldboy

定义对象独有的特征

class OldboyStudent:
    school = 'oldboy'
    count = 0
    
    def __init__(self,id,name,age=15):#self是实例化的对象,自动传值
        self.id = id
        self.name = name
        self.age = age 
        OldBoyStudent.count +=1
        
    def choose_course(self):
        print('is choosing course')
        
stu1 = OldBoyStudent(222,'朱瑞星')  #每次实例化对象的时候都会自动调用————init方法
print(OldBoyStudent.__dict__)
print(stu1.id,stu1.name,stu1.age)  

stu2 = OldBoyStudent(66666,'孔悟空',19)
print(stu2.id,stu2.name,stu2.age)

print(OldBoyStudent.count)
 
{'__module__': '__main__', 'school': 'oldboy', 'count': 1, '__init__': <function OldBoyStudent.__init__ at 0x05CB6150>, 'choose_course': <function OldBoyStudent.choose_course at 0x05CB6198>, '__dict__': <attribute '__dict__' of 'OldBoyStudent' objects>, '__weakref__': <attribute '__weakref__' of 'OldBoyStudent' objects>, '__doc__': None}
222222 朱瑞星 15
666666 孔悟空 19
3    

属性的查找顺序

属性的查找顺序:先从对象本身查找,对象中没有,再去类中查找,类中也没有,则会报错。

class OldBoyStudent:
    school = 'oldboy'
    name = 'xxx'               #类中有
    
    def __init__(self,id,name,age=15):
        self.id = id           #对象本身没有
        self.age = age
    def choose_course(self):
        print('is choosing course')
print(OldBoyStudent.__init__)

stu1 = OldBoyStudent(2222,'朱瑞星')
print(stu1.name)     #先从对象本身查找,再从类中查找
print(stu1.__dict__)

stu2 = OldBoyStudent(6666,'孔悟空',19)
print(stu2.__dict__)

{'__module__': '__main__', 'school': 'oldboy', 'name': 'xxx', '__init__': <function OldBoyStudent.__init__ at 0x01983C90>, 'choose_course': <function OldBoyStudent.choose_course at 0x01983A08>, '__dict__': <attribute '__dict__' of 'OldBoyStudent' objects>, '__weakref__': <attribute '__weakref__' of 'OldBoyStudent' objects>, '__doc__': None}
xxx
{'id': 222222, 'age': 15}
{'id': 666666, 'age': 19}
class OldBoyStudent:  # 类的定义阶段,会运行代码(牢记)
    # 1 / 0
    school = 'oldboy'
    name = 'xxx'

    def __init__(self, id, name, age=15):  # (******)  # self是实例化的对象,自动传值  # self=stu1  # self=stu2
        self.id = id  # stu1.id = id
        self.name = name  # stu1.name = name   
        self.age = age # stu1.age = age

    def choose_course(self):
        # 1 / 0
        print('is choosing course')

print(OldBoyStudent.__dict__)

stu1 = OldBoyStudent(222222, '朱瑞星')  # 每次实例化对象的时候都会自动调用__init__方法
print(stu1.name)           #先从对象本身查找
print(stu1.__dict__)

stu2 = OldBoyStudent(666666, '孔悟空', 19)
print(stu2.__dict__)

{'__module__': '__main__', 'school': 'oldboy', 'name': 'xxx', '__init__': <function OldBoyStudent.__init__ at 0x01753C90>, 'choose_course': <function OldBoyStudent.choose_course at 0x01753A08>, '__dict__': <attribute '__dict__' of 'OldBoyStudent' objects>, '__weakref__': <attribute '__weakref__' of 'OldBoyStudent' objects>, '__doc__': None}
朱瑞星
{'id': 222222, 'name': '朱瑞星', 'age': 15}
{'id': 666666, 'name': '孔悟空', 'age': 19}

类与对象的绑定方法

针对类而言:choose_course里的 self就是传的参数

针对对象而言:choose_course里的self就是对象本身

class OldBoyStudent:  # 类的定义阶段,会运行代码(牢记)
    # 1 / 0
    school = 'oldboy'

    def __init__(self, id, name, age=15):  # (******)  # aaa是实例化的对象,自动传值  # self=stu1  # self=stu2
        self.id = id  # stu1.id = id
        self.name = name  # stu1.name = name
        self.age = age  # stu1.age = age

    def choose_course(self):  # 约定俗成为self
        # 1 / 0
        print(id(self))
        print(self.school)
        print(self.id)
        self.id += 1
        print(f'{self.name} is choosing course')


stu1 = OldBoyStudent(222222, '朱瑞星')  # 每次实例化对象的时候都会自动调用__init__方法(排在第一位的)
# OldBoyStudent.choose_course(1111)  # self = 1111
print('*' * 50)
stu1.choose_course()  # self = <__main__.OldBoyStudent object at 0x000001C5EC999860>  self是对象本身
print(stu1.id)

# 针对类而言:choose_course里的self就是传的参数
# 针对对象而言:choose_course里的self就是对象本身
stu2 = OldBoyStudent(666666, '孔悟空', 19)  # 每次实例化对象的时候都会自动调用__init__方法(排在第一位的)
# stu2.choose_course()
print(stu2.id)

类与数据类型

lis = list([1,2,3])
lis2 = list([1,2,3])
lis.append(4)   #list.append(lis,4)

[1, 2, 3, 4]

对象的高度整合

在类里面封装了一大堆特征/数据,然后又封装了一大堆方法(方法用来专门操作这些数据的),并且在这基础上对于不同对象,这些属性和方法有不一样。

class Animal:    #定义类别
    def __init__(self,attri,height,weight):
        self.attri = attri      #封装了数据
        self.height =height
        self.weight = weight
        
    def jiao(self):           #封装了一堆方法
        print(self.attri,'开始叫了')   
        
    def eat(self):           #封装了一堆方法
        print(self.attri,’开始吃饭了)
        
people = Animal('人类',180,140)  #传给对象特征数据
dog = Animal('狗',100,50)

people.eat()                     #类别里面的特征执行方法函数
dog.eat()

人类 开始吃饭了了
狗 开始吃饭了了

面对对象编程

标签:计算   obj   sel   实例   ima   weakref   特征   技能   缺点   

原文地址:https://www.cnblogs.com/zuihoudebieli/p/11219174.html

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