17.面向对象【一】

【一】面向过程 与 面向对象

1)面向过程

  • 关键在于过程,即流水线,用到哪个功能写哪个
  • 可将复杂的问题流程化,进而简单

2)面向对象

  • 关键在于对象,将程序进行整合,成为一个整体
  • 解决了程序的扩展性

【二】类与对象

对应程序:先有类才能有对象

1)类

即类别、种类,是面向对象分析和设计的基石

  • 关键字class声明一个类,类名建议驼峰体
class Eg(object):
    num = '1'
    ...(可直接定义数据,函数)
# object:父类
# 也可简写成(class Eg:)
eg =Eg()
print(eg)	# <__main__.Eg object at 0x0000020827CF7590>
print(eg.num)	# 1

【三】面向对象编程

# 定义类
class School(object):
    school_name = 'xx学校'

    def read(self):
        print('read book')

1)查看类的名称空间

查看类这个容器内盛放的东西

print(School.__dict__)
# {'__module__': '__main__', 'school_name': 'xx学校', 'read': <function School.read at 0x0000027229A14400>, '__dict__': <attribute '__dict__' of 'School' objects>, '__weakref__': <attribute '__weakref__' of 'School' objects>, '__doc__': None}

2)对象的实例化

得到的对象每一次都是新的

student1 = School()
student2 = School()

3)修改对象的属性

1.通过类的名称空间修改

st1 = School()
st2 = School()
st1.__dict__['name'] = 'a'
st1.__dict__['age'] = 18
print(st1.__dict__)  # {'name': 'a', 'age': 18}
print(st1.name)  # a
st2.__dict__['name'] = 'b'
st2.__dict__['age'] = 20
print(st2.__dict__)  # {'name': 'b', 'age': 20}
print(st2.age)  # 20

2.封装成函数

st1 = School()
st2 = School()
def into(st, name, age):
    st.name = name
    st.age = age
into(st=st1, name='a', age=18)
print(st1.__dict__)  # {'name': 'a', 'age': 18}
into(st2, 'b', 20)
print(st2.__dict__)  # {'name': 'b', 'age': 20}

3.封装成类

class School(object):
    school_name = 'xx学校'
    def read(self):
        print('read book')
    def into(st, name, age):
        st.name = name
        st.age = age
st1 = School()
st2 = School()
School.into(st=st1, name='a', age=18)
School.into(st2, 'b', 20)
print(st1.__dict__)  # {'name': 'a', 'age': 18}
print(st2.__dict__)  # {'name': 'b', 'age': 20}

4.类的__init__方法

class School(object):
    school_name = 'xx学校'
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def read(self):
        print('read book')
st1 = School(name='a', age=18)
st2 = School('b', 20)
print(st1.__dict__)  # {'name': 'a', 'age': 18}
print(st2.__dict__)  # {'name': 'b', 'age': 20}

4)属性访问

1.类的属性

  • 类中定义的名字都是类的属性(数据属性,函数属性)

    class School(object):
        school_name = 'xx学校'
        def read(self):
            print('read book')
    print(School.__dict__)
    # {'__module__': '__main__', 'school_name': 'xx学校', 'read': <function School.read at 0x000001B02C874400>, '__dict__': <attribute '__dict__' of 'School' objects>, '__weakref__': <attribute '__weakref__' of 'School' objects>, '__doc__': None}
    print(School.school_name)
    # xx学校
    print(School.read)
    # <function School.read at 0x000001B02C874400>
    

2.对象属性

class School(object):
    school_name = 'xx学校'
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def read(self):
        print(f'{self.name}read book')
st1 = School(name='a', age=18)
# 查看
print(st1.__dict__)  # {'name': 'a', 'age': 18}
print(st1.name)  # a
# 新增
st1.course = "Python"
print(st1.__dict__)  # {'name': 'a', 'age': 18, 'course': 'Python'}
# 修改
st1.age = 20
print(st1.__dict__)  # {'name': 'a', 'age': 20, 'course': 'Python'}
# 删除
del st1.course
print(st1.__dict__)  # {'name': 'a', 'age': 20}

5)属性查找

1.类中定义的变量

  • 类中定义的变量是类的数据属性,是共享给所有对象用的,指向相同的内存地址

    class School(object):
        school_name = 'xx学校'
        def __init__(self, name, age):
            self.name = name
            self.age = age
        def read(self):
            print(f'{self.name}read book')
    st1 = School('a',10)
    st2 = School('b',20)
    print(id(School.school_name),
          id(st1.school_name),
          id(st2.school_name))
    # id输出相同
    

2.类中定义的函数

  • 类中定义的函数是类的函数属性,类可以使用,但必须遵循函数的参数规则,有几个参数需要传几个参数

    • 类的定义的函数主要给对象使用,且与对象绑定

    • 虽然所有对象指向的都是相同的功能,但绑定在不同对象就是不同的绑定方法,内存地址各不相同

      print(id(School.read),
            id(st1.read),
            id(st2.read))
      # id 各不相同
      
    • 绑定给谁就由谁来调用,就会将’谁’本身当做第一个参数自动传入

      st1.read()
      # a read book
      st2.read()
      # b read book
      

3.查找顺序

  • 在自己的对象中查找
  • 父类里面找
  • 基类里面找
  • object 里面找
  • 找不到则报错

【四】类的属性扩展

  • 类名.__name__ :类的名字(字符串)
  • 类名.__doc__:类的文档字符串
  • 类名.__base__:类的第一个父类(在讲继承时会讲)
  • 类名.__bases__:类所有父类构成的元组(在讲继承时会讲)
  • 类名.__dict__:类的字典属性
  • 类名.__module__:类定义所在的模块
posted on 2024-04-30 15:29  晓雾-Mist  阅读(15)  评论(0)    收藏  举报