zoukankan      html  css  js  c++  java
  • day 18 初级面向对象的 作用域与命名空间


    三。面向对象的 命名空间和作用域
        
    class Person:
    #     role = 'person'   # 静态属性
    #     def __init__(self,name,sex,hp,ad):
    #         self.name = name     # 对象属性 属性
    #         self.sex = sex
    #         self.hp = hp
    #         self.ad = ad
    #     def attack(self):
    #         self.hobby = 'girl'
    #         print('%s发起了一次攻击'%self.name)
    #
    # alex = Person('a_sb','不详',1,5)
    # alex.attack()   # Person.attack(alex)
    # alex.age = 81
    # # alex --> Person
    # # Person实例化了alex
    # print(alex.__dict__)

      1.alex.name   alex 指向自己的内存空间,在自己的内存空间中找name

      2.alex.attack   alex先找到自己的内存空间,然后再找到类对象指针,通过类对象指针找到类,再通过类找到attack

      3.对象的内存空间里,只储存对象的属性,不能储存静态属性和方法

    方法和静态属性都存储在类的内存空间中
    # 为了节省内存,让多个对象去共享类中的资源
    
    
     class Person:
    #     role = 'person'   # 静态属性
    #     def __init__(self,name,sex,hp,ad):
    #         self.name = name     # 对象属性 属性
    #         self.sex = sex
    #         self.hp = hp
    #         self.ad = ad
    #         self.attack = 'hahaha'
    #     def attack(self):
    #         print('%s发起了一次攻击'%self.name)
    #
    # alex = Person('a_sb','不详',1,5)
    # boss_jin = Person('金老板','女',50,20)
    # print(alex.role)
    # print(boss_jin.role)
    # alex.role = 'dog'
    # print(alex.role)
    # print(boss_jin.role)    # dog or ? person
    
    
    
    # class Person:
    #     role = 'person'   # 静态属性
    #     def __init__(self,name,sex,hp,ad):
    #         self.name = name     # 对象属性 属性
    #         self.sex = sex
    #         self.hp = hp
    #         self.ad = ad
    #         self.attack = 'hahaha'
    #     def attack(self):
    #         print('%s发起了一次攻击'%self.name)
    #
    # alex = Person('a_sb','不详',1,5)
    # boss_jin = Person('金老板','女',50,20)
    # print(alex.role)
    # print(boss_jin.role)
    # alex.role = 'dog'
    # print(alex.role)
    # 模拟人生
    # class Person:
    #     money = 0
    #     def __init__(self,name):
    #         self.name = name
    #     def work(self):
    #         print(self.name,'工作,赚了1000块钱')
    #         Person.money += 1000
    #
    # father = Person('father')
    # mother = Person('mother')
    # mother.work()
    # father.work()
    # print(Person.money)
    
    
    # 写一个类 完成一个功能 : 可以统计这个类有几个对象
    # class Foo:
    #     count = 0
    #     def __init__(self):
    #         Foo.count += 1
    # f1 = Foo()
    # f2 = Foo()
    # f3 = Foo()
    # f4 = Foo()
    # f5 = Foo()
    # print(Foo.count)  #
    
    
    
    # class Person:
    #     money = [1]
    #     def __init__(self,name):
    #         self.name = name
    #     def work(self):
    #         print(self.name,'工作,赚了1000块钱')
    #         self.money[0] += 1000
    #
    # father = Person('father')
    # mother = Person('mother')
    # mother.work()
    # father.work()
    # print(Person.money)   # 2000 or 0?
    
    # class Person:
    #     money = [0]
    #     def __init__(self,name):
    #         self.name = name
    #     def work(self):
    #         print(self.name,'工作,赚了1000块钱')
    #         Person.money =  [Person.money[0] + 1000]
    #
    # father = Person('father')
    # mother = Person('mother')
    # mother.work()
    # father.work()
    # print(Person.money)
    
    
    
    
    对象属性是独有的,静态属性和方法是共享的
    # 对象使用名字:先找自己内存空间中的,再找类的内存空间中的
    # 类名.静态变量名 :对于静态属性的修改,应该使用类名直接修改








































  • 相关阅读:
    【图文并茂,点赞收藏哦!】重学巩固你的Vuejs知识体系
    RabbitMQ概念及控制台介绍
    RabbitMQ入门介绍及环境搭建
    C# 利用PdfSharp生成Pdf文件
    利用pdf.js在线展示PDF文档
    Python办公自动化之Excel转Word
    C#利用ServiceStack.Redis访问Redis
    Redis基础之事务
    Redis基础之配置文件
    第0天 | 12天搞定Pyhon,前言
  • 原文地址:https://www.cnblogs.com/zsdbk/p/8798635.html
Copyright © 2011-2022 走看看