首页 > 编程语言 >[python3]: python --【class】类变量(类属性)

[python3]: python --【class】类变量(类属性)

时间:2024-02-07 15:11:41浏览次数:26  
标签:name python self object number -- icount class

[python3]:  python  --【class】类变量(类属性)

 

 

 

 

一、说明:

 

  1、类变量:类变量,定义在【类内】且【函数外】。

 1 class  object:
 2 
 3     #  class_variable
 4     icount    =    0
 5 
 6 
 7     def  __init__(self):
 8         #    using  class_variable
 9         object.icount    =    object.icount  +  1
10 
11 
12     def  objnum(self):
13         print(f"object_number := { object.icount }")

 

  2、类变量的使用方法:class_name.class_variable

    2.1、object.icount::

      2.1.1:class_name = object

      2.1.2:class_variable  =  icount

 

 

 

 

二、代码

 1 #!/usr/bin/env python3
 2 
 3 
 4 class object:
 5 
 6     #  variable of class(class_variable)
 7     #  using variable of class:  object.icount (class_name.class_variable)
 8     icount = 0
 9 
10     def __init__(self, name):
11         self.icount = 1
12         self.__objname =  name
13         object.icount = object.icount + 1
14         print(f"[constructor]#  object_name:{ self.__objname },    object.icount = { object.icount } ")
15 
16     def __del__(self):
17         object.icount = object.icount - 1
18         print(f"[destructor]#  object_name:{ self.__objname },    object.icount = { object.icount } ")
19 
20     def object_number(self):
21         print(f"\nobject_number := { object.icount }")
22         print(f"self_icount := { self.icount } \n")
23 
24     def msg_name(self):
25         print(f"name := { self.__objname }")
26 
27 
28 if __name__ == '__main__':
29     x1 = object("f1")
30     x2 = object("f2")
31     x3 = object("f3")
32     x4 = object("f4")
33 
34     x1.object_number()
35     x2.object_number()
36     x3.object_number()
37     x4.object_number()

 

 

三、运行结果

 1 [constructor]#  object_name:f1,    object.icount = 1 
 2 [constructor]#  object_name:f2,    object.icount = 2 
 3 [constructor]#  object_name:f3,    object.icount = 3 
 4 [constructor]#  object_name:f4,    object.icount = 4 
 5 
 6 object_number := 4
 7 self_icount := 1 
 8 
 9 
10 object_number := 4
11 self_icount := 1 
12 
13 
14 object_number := 4
15 self_icount := 1 
16 
17 
18 object_number := 4
19 self_icount := 1 
20 
21 [destructor]#  object_name:f1,    object.icount = 3 
22 [destructor]#  object_name:f2,    object.icount = 2 
23 [destructor]#  object_name:f3,    object.icount = 1 
24 [destructor]#  object_name:f4,    object.icount = 0 

 

 

四、参考内容

 

  1、  Python3 面向对象  --  https://www.runoob.com/python3/python3-class.html

 

标签:name,python,self,object,number,--,icount,class
From: https://www.cnblogs.com/lnlidawei/p/18010944

相关文章

  • Spring 接点、切点、切面、引入、织入、通知 概念
    importcom.github.pagehelper.PageHelper;importorg.aspectj.lang.ProceedingJoinPoint;importorg.aspectj.lang.annotation.Around;importorg.aspectj.lang.annotation.Aspect;importorg.aspectj.lang.annotation.Pointcut;importorg.springframework.context.ann......
  • kubernetes集群核心概念---pod
    1.pod介绍参考链接:https://kubernetes.io/zh/docs/concepts/workloads/pods/1.1.查看pod[root@centos~]#kubectlgetpod#pod或pods都可以,不指定namespace,默认是名为default的namespace [root@centos~]#kubectlgetpod-nkube-system1.2.pod的yaml资源清单格式......
  • Oracle 19c enterprise manager express username password
    *[Oracle19centerprisemanagerexpressusernamepassword-Search](https://cn.bing.com/search?q=Oracle+19c+enterprise+manager+express+username+password&qs=n&form=QBRE&sp=-1&lq=0&pq=oracle+19c+enterprise+manager+express+username+passw......
  • Puzzle hunt 工具
    写在前面   做一下破解网站的汇总,方便调出来用,省的老在收藏夹韩信点兵()关于古典密码相关的网站指路古典密码篇→古典密码汇总-Tey729-博客园(cnblogs.com) 正文1.Qat-可定位字母找单词 Qat(quinapalus.com) 2.河马-搭配Qat食用,单词更多也更不靠谱 Th......
  • Jenkins在jdk17的Tomcat上运行报错
    Jenkins在jdk17的Tomcat上运行报错一、环境宝塔:tomcat8.0jdk:jdk17二、保存项目时报错​Unabletomakefieldprotectedtransientintjava.util.AbstractList.modCountaccessible:modulejava.basedoesnot"opensjava.util"tounnamedmodule@6d15ca84​查看local......
  • 【驾驶知识】科目一、科目四知识点总结大全!!!
    前言:驾考宝典知识点整理、科目一总结、科目四总结、驾照科知识点总结。科目一和科目四的主要知识点,自己在准备时整理的笔记,仅供参考,希望大家都能顺利通关~原文持续更新中:https://www.cnblogs.com/MrFlySand/p/18010913戳下方链接,后台回复【230813驾照】获取更多驾考知识http......
  • 面试经典 150 题 (十三)
    先来个大的classRandomizedSet{privateHashSet<Integer>hashSet;publicRandomizedSet(){hashSet=newHashSet<Integer>();}publicbooleaninsert(intval){if(hashSet.contains(val)){returnfa......
  • Java与sql中的字符串表示
    在Java中,双引号""用于表示字符串字面量,而单引号''用于表示字符字面量。这意味着在Java中,您可以使用双引号来包围包含任意数量字符的字符串,包括零个字符(空字符串)和多个字符。例如,在Java中:StringemptyString="";//空字符串StringsingleChar='a';/......
  • ES的安装
    1.Elasticsearch的安装环境安装步骤:1.需要安装JAVA环境,是因为ElasticSearch是用java开发的。2.安装ES和KibanaES的图形化界面是Kibana。注意安装时,ES和Kibana的版本一定要匹配,最好安装相同的版本号。3.安装ES的分词库IK注意安装时,ES和IK分词器的版本一定要匹......
  • AtCoder-ABC-Ex乱写
    ABC233ExManhattanChristmasTree先将\((x,y)\)变成\((x+y,x-y)\),也就是曼哈顿转切比雪夫,之后曼哈顿距离\(\lek\)的在切比雪夫坐标系下就是一个正方形。用主席树做矩形和,外层套一个二分即可,时间复杂度\(\mathcal{O}(n\log^2n)\)。ABC233Ex#include<bits/stdc++.h......