当前位置:网站首页>2021-11-05类变量和类方法的理解
2021-11-05类变量和类方法的理解
2022-07-27 19:07:00 【小唐学抓娃】
= = = = = = = = = 类变量和类方法 = = = = = = =
= = = = = = = = = = = = = 一、类变量 = = = = = = = = = = = = = **
1、什么是类变量
类变量也叫静态变量/静态属性**,是该类的所有对象共享的变量,任何一个该类的对象去访问它时,取到的都是相同的值同样任何一个该类的对象去修改它时,修改的也是同一个变量。
2、类变量的内存布局
static变量是对象共享,不管static变量在哪里,
共识(1) static.变量是同一个类所有对象共享(2) static类变量,在类加载的时候就生成了.
3、如何定义、访问类变量
①定义语法;
访问修饰符 static 数据类型 变量名; [推荐]
或 static 访问修饰符 数据类型 变量名;
②访问
类名.类变量名
或者对象名.类变量名【静态变量的访问修饰符的访问权限和范围和普通属性是一样的。】推荐使用:类名.类变量名;
4、类变量的使用细节及注意事项
1.什么时候需要用类变量
当我们需要让某个类的所有对象都共享一个变量时,就可以考虑使用类变量(静态变量):比如:定义学生类,统计所有学生共交多少钱。Student (name, staticfee)
2.类变量与实例变量(普通属性)区别
类变量是该类的所有对象共享的,而实例变量是每个对象独享的。
3.加上static称为类变量或静态变量,否则称为实例变量/普通变量/非静态变量
4.类变量可以通过类名.类变量名或者对象名.类变量名来访问,国ava设计者推荐我们使用类名.类变量名方式访问。【前提是满足访问修饰符的访问权限和范围】
5.实例变量不能通过类名.类变量名方式访问。
6.类变量是在类加载时就初始化了,也就是说,即使你没有创建对象,只要类加载了就可以使用类变量了。【案例演示]
7.类变量的生命周期是随类的加载开始,随着类消亡而销毁。
**= = = = = = = = = = = = = 二、类方法 = = = = = = = = = = = = = **
1、定义及使用
①类方法也叫静态方法。形式如下:
访问修饰符 static 数据返回类型 方法名(){}【推荐】
static 访问修饰符 数据返回类型 方法名(){ }
②使用方式:类名.类方法名或者对象名.类方法名【前提是满足访问修饰符的访问权限】
2、注意事项和细节
当方法中不涉及到任何和对象相关的成员,则可以将方法设计成静态方法,提高开发效率.比如:工具类中的方法
1)类方法和普通方法都是随着类的加载而加载,将结构信息存储在方法区∶
类方法中无this的参数
普通方法中隐含着this的参数
2)类方法可以通过类名调用,也可以通过对象名调用。
3)普通方法和对象有关,需要通过对象名调用,比如对象名.方法名(参数),不能通过类名调用。
4)类方法中不允许使用和对象有关的关键字,比如this和super。普通方法(成员方法)可以。
5)类方法(静态方法)中只能访问静态变量或静态方法。普通成员方法,既可以访问非静态成员,也可以访问静态成员。
小结:静态方法,只能访问静态的成员,非静态的方法,可以访问静态成员和非静态成员(必须遵守访问权限)
边栏推荐
- Yyds dry inventory learn how to write function overloads in typescript
- 除了「加机器」,其实你的微服务还能这样优化
- puzzle(021)消除问题
- Daily news on July 15, 2022: meta announced the launch of make-a-scene: AI image generation can be controlled based on text and sketches
- 中英文说明书丨人甲胎蛋白(AFP)ELISA定量试剂盒
- Thinking about SLA of cloud computing
- Software test interview question: please say who is the best person to complete these tests, and what is the test?
- Troubleshooting and resolution of program operation problems: an instance of 'std:: Logic_ error‘what(): basic_ string::_ M_ construct null not valid
- 技术管理 - 一定要抓大放小
- PostgreSQL source code (65) analysis of the working principle of globalvis, a new snapshot system
猜你喜欢
随机推荐
Acwing3715. Minimum exchange times (simulation idea of bubble sorting method)
Box model and element positioning
Who is the sanctity of the six Chinese enterprises newly sanctioned by the United States?
软件测试面试题:假设有一个文本框要求输入10个字符的邮政编码,对于该文本框应该怎样划分等价类?
ECCV 2022 | 中科大&京东提出:数据高效的Transformer目标检测器
Software test interview question: does software acceptance test include formal acceptance test, alpha test and beta test?
puzzle(021)消除问题
Daily news on July 15, 2022: meta announced the launch of make-a-scene: AI image generation can be controlled based on text and sketches
IDEA连接MySQL数据库并执行SQL查询操作
华为成立全球生态发展部:全力推进HMS全球生态建设
Worthington磷脂酶A2研究丨磷脂酰胆碱2-乙酰水解酶
简单手动实现Map
面向3nm及以下工艺,ASML新一代EUV光刻机曝光
How to check whether there is Tsinghua source / delete Tsinghua source and keep the default source
The solution that the laptop can connect to WiFi but the browser cannot open the web page
如何实现一个好的知识管理系统?
屏蔽自动更新描述文件(屏蔽描述文件)
Commercial delay of self-developed 5g chip? Apple iPhone will adopt Qualcomm 5g chip in the next four years
Worthington plasma amine oxidase (PAO) instructions
University of Tilburg, Federal University of the Netherlands | neural data to text generation based on small datasets: comparing the added value of two semi supervised learning approvals on top of a l








