benym的知识笔记 benym的知识笔记
🦮首页
  • Java

    • Java-基础
    • Java-集合
    • Java-多线程与并发
    • Java-JVM
    • Java-IO
  • Python

    • Python-基础
    • Python-机器学习
  • Kafka
  • Redis
  • MySQL
  • 分布式事务
  • Spring

    • SpringIOC
    • SpringAOP
🦌设计模式
  • 剑指Offer
  • LeetCode
  • 排序算法
🐧实践
  • Rpamis

    • Utils
    • Exception
    • Security
  • 归档
  • 标签
  • 目录
🦉里程碑
🐷关于
GitHub (opens new window)

benym

惟其艰难,才更显勇毅🍂惟其笃行,才弥足珍贵
🦮首页
  • Java

    • Java-基础
    • Java-集合
    • Java-多线程与并发
    • Java-JVM
    • Java-IO
  • Python

    • Python-基础
    • Python-机器学习
  • Kafka
  • Redis
  • MySQL
  • 分布式事务
  • Spring

    • SpringIOC
    • SpringAOP
🦌设计模式
  • 剑指Offer
  • LeetCode
  • 排序算法
🐧实践
  • Rpamis

    • Utils
    • Exception
    • Security
  • 归档
  • 标签
  • 目录
🦉里程碑
🐷关于
GitHub (opens new window)
  • Python-基础

    • assert语句的运用
    • list(列表)、tuple(元组)、dict(字典)的回顾
    • Python中的Docstring
    • Python中的多态
    • Python中的集合
    • Python中的列表
    • Python中的序列以及切片的解释
    • Python中的引用和切片
    • Python中的元组
    • Python中对列表和元组的切片操作
    • Python中完整for循环的实际运用
    • Python中字典(key-value)
    • Python中字符串的一些方法回顾(拆分与合并)
    • Python中字符串的一些方法回顾(切片回顾)
    • Python中字符串的一些方法回顾(文本对齐、去除空白)
    • Python中字符串的一些方法回顾
    • Python中字符串的一些基本操作
    • 多种方法快速交换两个变量的值
    • 利用Python进行文件的自动备份
    • 利用Python进行文件的自动备份(第二版)
    • 利用Python进行文件的自动备份(第三版和第四版)
    • 列表推导
    • 在函数中接受元组与字典
    • 装饰器
    • finally异常处理
    • Python的__name__ = '__main__' 的作用
    • Python的pickle模块
    • Python对象的实例化
    • Python日志模块
    • Python中的__new__方法的重写
    • Python中的lambda函数
    • Python中的静态方法、实例方法、类方法的区别
    • Python中的正则表达式
    • Python中的正则表达式match和search
    • Python中面向对象比较简单的内部函数
    • with open异常处理
    • 单例设计模式
    • 继承的运用
    • 简单的异常处理
    • 类变量与对象变量
      • 代码
      • 运行结果
    • 输入输出——简单的回文判断
    • 输入输出——回文字串的判断(加强版)
    • 文件操作
    • 用户自己引发的异常处理
    • 正则表达式检索与替换
    • 正则表达式中的compile函数
    • 正则表达式中的compile函数(二)
  • Python-机器学习

    • Numpy库的首次使用
    • kNN(k-近邻算法)
    • kNN识别手写图像
    • LogisticRegression(逻辑回归)
    • Ndarray对象
    • Numpy中的数组维度
    • Numpy中花式索引和shape用法
    • turtle绘图库
    • 第一个使用Tensorflow的程序
    • 将下载下来的MNIST手写数字数据集转化成为图片
    • Tensorflow交互式使用
    • 使用k-近邻算法改进约会网站的配对效果
    • Numpy数据类型和arange方法、astype方法的使用
    • 一些TensorFlow的基本操作
  • Python
  • Python-基础
benym
2018-07-23
目录

类变量与对象变量

# 类变量与对象变量

# 代码

# coding=UTF-8,类变量与对象变量
class Robot:
    """表示有一个带有名字的机器人。"""
    # 一个类变量,用来计数机器人的数量
    population = 0  # 这是一个类变量,属于Robot类

    def __init__(self, name):  # name变量属于一个对象(通过self分配),因此它是一个对象变量
        """初始化数据"""
        self.name = name
        print("(Initializing {})".format(self.name))

        # 当有人被创建时,机器人会增加人口数量
        '''
       除了 Robot.popluation ,我们还可以使用 self.__class__.population ,因为每个对象都通过
       self.__class__ 属性来引用它的类。 
       '''
        Robot.population += 1

    def die(self):
        """我挂了。"""
        print("{} is being destroyed!".format(self.name))

        Robot.population -= 1

        if Robot.population == 0:
            print("{} was the last one.".format(self.name))
        else:
            print("There are still {:d} robots working.".format(Robot.population))

    def say_hi(self):
        """来自机器人的诚挚问候

        没问题,你做得到"""
        print("Greetings , my masters call me {}".format(self.name))

    '''
    classmethod 修饰符对应的函数不需要实例化,不需要 self 参数,
    但第一个参数需要是表示自身类的 cls 参数,可以来调用类的属性,类的方法,实例化对象等。
    '''

    # how_many实际上是一个属于类而非属于对象的方法
    # classmethod(类方法)或是一个staticmethod(静态方法)
    @classmethod  # 装饰器,等价于how_many = classmethod(how_many)
    def how_many(cls):
        """打印出当前的人口数量"""
        print("We have {:d} robots.".format(cls.population))


droid1 = Robot("R2-D2")
droid1.say_hi()
Robot.how_many()

droid2 = Robot("C-3PO")
droid2.say_hi()
Robot.how_many()
print("\nRobots can do some work here.\n")
print("Robots have finished their work. So let's destroy them.")
droid1.die()
droid2.die()

Robot.how_many()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

# 运行结果

(Initializing R2-D2)
Greetings , my masters call me R2-D2
We have 1 robots.
(Initializing C-3PO)
Greetings , my masters call me C-3PO
We have 2 robots.

Robots can do some work here.

Robots have finished their work. So let's destroy them.
R2-D2 is being destroyed!
There are still 1 robots working.
C-3PO is being destroyed!
C-3PO was the last one.
We have 0 robots.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
编辑 (opens new window)
#Python面向对象
上次更新: 2022/12/31, 16:52:27
简单的异常处理
输入输出——简单的回文判断

← 简单的异常处理 输入输出——简单的回文判断→

最近更新
01
SpringCache基本配置类
05-16
02
DSTransactional与Transactional事务混用死锁场景分析
03-04
03
Rpamis-security-原理解析
12-13
更多文章>
Theme by Vdoing | Copyright © 2018-2024 benym | MIT License
 |   |   | 
渝ICP备18012574号 | 渝公网安备50010902502537号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式