zoukankan      html  css  js  c++  java
  • 类和对象(重点)

    • 分类/类别
    • 上述的代码(张三,李四,王五,小明):按照学生和老师可以分为两类,而按照学校则可以划分为一类
      • 所以类是抽象的,并不是固定的,是一种思想
    • 类的概念:一系列具有共同特征(属性)或技能(方法)的对象
    • 类的作用:
      • 在现实生活中,是先有对象,再根据对象划分出类,但在Python中,是先定义类,再实例化对象(根据抽象的类,生成具体的对象)
    • 类的定义:
      • class 类名(使用大驼峰):
        • 特征(属性)
        • 技能(方法)

    定义类和对象

    # 造函数
    def f1():
        # 1/0		    # 不报错,函数定义阶段只检验语法,不执行代码
        school = 'a中'
        
      
    # 造类(经典类,新式类)
    class Student:	#不要写小括号,小括号是继承
        # 1/0			# 报错,类的定义阶段会运行代码
        school = 'a中'
        def eat(self):
            print('吃饭')
    
    print(Student.__name__)			# 打印类名
    student_dict = Student.__dict__		# 双下划线开头的方法会在某种情况下自动触发
    print(student_dict)
    print(student_dict['school'])
    student_dict['eat'](111)
    
    Student
    {'__module__': '__main__', 'school': 'a中', 'eat': <function Student.eat at 0x0000022334A4E8C8>, '__dict__': <attribute '__dict__' of 'Student' objects>, '__weakref__': <attribute '__weakref__' of 'Student' objects>, '__doc__': None}
    
    a中
    吃饭
    

    实例化:

    # 造对象(实例化对象)
    stu1 = Student()
    print(1, stu.school)
    print(stu1.__class__)		# 也可以通过这个方法来修改这个对象所绑定的类
    
    stu2 = Student()
    print(2, stu2.school)
    print(stu2.__class__)
    
    1 a中
    <class '__main__.Student'>
    2 a中
    <class '__main__.Student'>
    

    __dict__ 和__class__

    1. 类和对象中的属性,都是存放在__dict__中
    2. 对象的__dict__可以直接进行修改,如stu1.__dict__ = {'num': 1,'name': '张三'}但是类的__dict__属性是只读的,不能进行修改
    3. __class__可以用来查看对象所绑定的类,也可以通过它来进行修改绑定在哪个类上

    创建对象时的底层运作

    1560855082430

    定义对象独有的特征

    print(Student.__dict__)
    print('stu1', stu1.__dict__)
    # 以下两种定义方式等价
    # stu1.__dict__['num'] = 1		# 使用字典的方法
    stu1.num = 1		# 使用对象的方法
    print(stu1.__dict__)
    print(Student.__dict__)
    
    
    {'__module__': '__main__', 'school': 'a中', 'eat': <function Student.eat at 0x00000236C327E8C8>.......}
    stu1 {}
    {'num': 1}
    {'__module__': '__main__', 'school': 'a中', 'eat': <function Student.eat at 0x00000236C327E8C8>.......}
    

    对象的方法是独有的,且对类没有影响

    class Student:
        school = 'a中'
        count = 0
    
    stu1 = Student()
    stu1.count += 1
    stu2 = Student()
    stu2.count +=1
    stu3 = Student()
    stu3.count += 1
    print(Student.count)
    
    0		# 加的都是对象中的,并没有影响到类
    

    给对象添加属性

    # 把特征(特征)都加上去
    stu1.name = '张三'
    stu1.age = 19
    print(stu1.num, stu1.name, stu1.age)
    
    # 李四的特征
    stu2.num = 2
    stu2.name = '李四'
    stu2.age = 20
    print(stu2.num, stu2.name, stu2.age)
    # 这样加太繁琐了,如果对象多会很麻烦
    
    1 张三 19
    2 李四 20
    

    init

    # 用一个函数把这个重复定义的过程封装起来
    def init(obj, num, name, age):
        obj.num = num
        obj.name = name
        obj.age = age
        
    # 和上面的代码效果等价
    init(stu1, 1, '张三', 19)
    init(stu2, 2, '李四', 20)
    print(stu1.__dict__)
    print(stu2.__dict__)
    
    {'num': 1, 'name': '张三', 'age': 19}
    {'num': 2, 'name': '李四', 'age': 20}
    
    • 那么我们把这个init函数写到类里去
    • 每次实例化对象的时候都会自动调用__init__方法,且优先级最高
    class Student:
        school = 'a中'
        count = 0
        
        # self就是实例化的对象,是约定俗成的写法,和上面的obj是一样的, __init__在实例化的时候会自动传值
        def __init__(self, num, name, age):			# self = stu1   self = stu2
            self.num = num							
            self.name = name
            self.age = age
            Student.count += 1
            
        def ear(self):
            print('吃饭')
            
    stu1 = Student(1, '张三', 19)			# 每次实例化对象的时候都会自动调用
    print(stu1.num, stu1.name, stu1.age)
    
    stu2 = Student(2, '李四', 20)
    print(stu2.num, stu2.name, stu2.age)
    
    print(Student.count)
            
    
    1 张三 19
    2 李四 20
    2		# 每次实例化的时候都自动运行了init中的代码
    

    _slots_(了解)

    • 限制该类实例化的对象中可添加的属性
    class Student:
        __slots__ = ['num']		# 只能添加num
        pass
    
    stu1 = Student()
    stu1.num = 1		# 添加成功
    stu1.name = '张三'	# 报错
    

    给对象添加属性时的底层运作

    1560859905861

  • 相关阅读:
    写在noi之前
    雅礼集训 Day8
    雅礼集训 Day6
    雅礼集训 Day5
    2017雅礼集训 Day4
    2017雅礼集训 Day2
    2017雅礼集训 Day1
    洛谷 P3426 [POI2005]SZA-Template
    Codeforces Round #368 DIV2 C.
    Educational Codeforces Round 16 D&E.
  • 原文地址:https://www.cnblogs.com/lucky75/p/11047810.html
Copyright © 2011-2022 走看看