• 类(一)类的增删改查


    一.类的组成

    class Who(object):
        what = 'what'
        def __init__(self,name,age,gender):
            self.name = name
            self.age = age
            self.gender =gender
        def information(self):
            print(self.name,self.age,self.gender)
    p1 = Who('张三',18,'男')
    p1.information()
    

    1.1基本组成部分

    • 由class定义
    • 数据属性:由__init__()定义的部分
    • 函数属性(方法属性):如上的def information(self)部分
    • 实例化:p1 = Who('张三',18,'男')  
    • 实例化方法:p1.information()
    • 其中的what是什么?【类属性】

      1>类的属性字典:print(Who.__dicit__)

      {'__module__': '__main__', 'what': 'what', '__init__': <function Who.__init__ at 0x00000132B95692F0>, 'information': <function Who.information at 0x00000132D04CAA60>, '__dict__': <attribute '__dict__' of 'Who'                objects>, '__weakref__': <attribute '__weakref__' of 'Who' objects>, '__doc__': None}

      从类的属性字典中,可以看出:数据属性和函数属性是以内存地址的方式存在的

      2>实例化的属性字典:print(p1.__dict__)

      {'name': '张三', 'age': 18, 'gender': '男'}

      从实例化的属性字典中,可以看出:已经存储了具体的数据信息

      3>实例化方法的实参传递

      实例化调用类的方法函数:p1.information()

      过程:实例化数据字典{'name': '张三', 'age': 18, 'gender': '男'}传递给类方法函数information(self)中的self;而self.name,self.age,self.gender实质上是在调用字典中的值 。

      4>作用域问题:【类属性】

      print(Who.what) #what
      print(p1.what)  #what

      不论是实例化,还是类调用what变量,都能够取得对应的值。如同函数的作用于一样,在类的数据属性或实例化的数据属性中找不到what变量,就会到上一层去找。

    1.2类数据的【增,删,改,查】

      1.2.1数据【增】:Who.brother = 'jack'

      {'__module__': '__main__', 'what': 'what', '__init__': <function Who.__init__ at 0x000001DFC85C92F0>, 'information': <function Who.information at 0x000001DFDF54AA60>, '__dict__': <attribute '__dict__' of 'Who' objects>,             '__weakref__': <attribute '__weakref__' of 'Who' objects>, '__doc__': None, 'brother': 'jack'}

      我们看到,增加的数据属性,增加到__init__的同一级而不是在其内,可以理解为what=‘what’一样的性质

      同以下代码性质相同:

    class Who(object):
        what = 'what'
        brother = 'jack'
        def __init__(self,name,age,gender):
            self.name = name
            self.age = age
            self.gender =gender
        def information(self):
            print(self.name,self.age,self.gender)
    

      {'__module__': '__main__', 'what': 'what', 'brother': 'jack', '__init__': <function Who.__init__ at 0x000001BAA07792F0>, 'information': <function Who.information at 0x000001BAB76AAA60>, '__dict__': <attribute '__dict__' of            'Who' objects>, '__weakref__': <attribute '__weakref__' of 'Who' objects>, '__doc__': None}

      1.2.2数据【删】:del Who.brother

      1.2.3数据【改】:who.what = 'change'

      1.2.4数据【查】:print(Who.what)

    1.3函数的【增,改】

      1.3.1增加类的方法函数:先定义一个函数,再增加

    def sister(self):
        print(self.name,'is an only child of family')
    Who.sister = sister
    print(Who.__dict__)

      {'__module__': '__main__', 'what': 'change', '__init__': <function Who.__init__ at 0x0000023683EE92F0>, 'information': <function Who.information at 0x000002369AE4AA60>, '__dict__': <attribute '__dict__' of 'Who' objects>,            '__weakref__': <attribute '__weakref__' of 'Who' objects>, '__doc__': None, 'sister': <function sister at 0x0000023683B72EA0>}类的属性字典中增加了一个sister的方法函数

      1.3.1更改类的方法函数:先定义一个函数,再更改

    def brother(self):
        print(self.name,'have one little brother')
    Who.sister = brother
    print(Who.__dict__

      更改前:{'__module__': '__main__', 'what': 'change', '__init__': <function Who.__init__ at 0x0000020BF10692F0>, 'information': <function Who.information at 0x0000020BFFFCAA60>, '__dict__': <attribute '__dict__' of 'Who' objects>,  '__weakref__': <attribute '__weakref__' of 'Who' objects>, '__doc__': None, 'sister': <function sister at 0x0000020BEF1E2EA0>}

         更改后: {'__module__': '__main__', 'what': 'change', '__init__': <function Who.__init__ at 0x0000020BF10692F0>, 'information': <function Who.information at 0x0000020BFFFCAA60>, '__dict__': <attribute '__dict__' of 'Who' objects>,  '__weakref__': <attribute '__weakref__' of 'Who' objects>, '__doc__': None, 'sister': <function brother at 0x0000020BFFFCAAE8>}

    这里我们发现,类的属性字典的key并没有变化,但是key(sister)对应的函数(地址)已经更改了。

    1.4实例化的数据属性【增,删,改,查】【注意,这里没有实例化的函数的增删改查】

      1.4.1实例化的数据属性【增】

    p1.height = 173
    print(p1.__dict__)

      增加前:{'name': '张三', 'age': 18, 'gender': '男'}

      增加后:{'name': '张三', 'age': 18, 'gender': '男', 'height': 173}

      注意:这里仅仅增加了实例化的属性字典,而类的属性字典并没有发生变化

      1.4.2实例化的数据属性【删】:del p1.height

      1.4.3实例化的数据属性【改】:del p1.age = 21

      1.4.4实例化的数据属性【查】:print(del p1.age)

    总结:

     注意:类属性字典中的数据属性和函数属性都是存储的内存地址

    '__init__': <function Who.__init__ at 0x0000022E412A92F0>, 'information': <function Who.information at 0x0000022E581FAA60>

    这里需要特别注意,下面的内容,与之相关。

  • 相关阅读:
    什么是ROR
    Struts2中使用Session的两种方法
    js的时间操作方法
    产生4位包含大小字母与数字的验证码
    Java关键字this、super使用总结
    java 反射的实例
    Java语言中定义常量注意事项
    java 静态方法和实例方法的区别
    多线程的例子
    java 中的内省机制
  • 原文地址:https://www.cnblogs.com/liuhuacai/p/12595963.html
Copyright © 2020-2023  润新知