博客
关于我
Java学习笔记面向对象(static final)
阅读量:560 次
发布时间:2019-03-09

本文共 689 字,大约阅读时间需要 2 分钟。

我的Java学习笔记

面向对象(static final)


文章目录


Java面向对象(static final)

1.Static关键字

​ static方法就是没有this的方法。在没有实例化对象之前就可以调用static属性和方法,这就是它的主要用途。

1.1 static关键字编写静态属性

​ 访问修饰符 static 数据类型 属性名;

​ 特点:无需实例化对象,可以通过类名直接操作。在内存中只有一个副本

1.2 static关键字编写静态方法

​ static修饰的方法称之为静态方法。静态方法是不依赖于对象的,通过类名直接调用。

​ 为什么静态方法不能使用this?

​ 答: 因为static修饰的方法无需实例化可以通过类名直接调用,因为这种机制的存在,有可能存在方法被调用时对象没有实例化这种场景,为了保证代码健壮性所以在静态方法不能使用this。

1.3 如何调用静态方法

​ 使用类名直接调用。

​ 调用的特点:static修饰的方法和属性可以被普通方法直接调用和操作。但是static修饰的方法不能调用和操作普通属性和方法

​ 为什么static修饰的方法不能调用普通方法?

​ static修饰的方法在类加载的时候已经存储在内存中,这时候对象可能还没有实例化。

1.4 static关键字编写静态代码块

​ staic{

​ 代码块题

​ }

​ 一个类中可以存在多个静态块,在类初次被加载的时候会按照staic的顺序来执行。并且只执行一次

1.5 在继承关系中静态块和构造方法执行的顺序

​ 1.父类的静态款

​ 2.子类的静态款

​ 3.父类的构造方法

​ 4.子类的构造方法

1.6 静态方法如何调用普通属性和方法

转载地址:http://prvpz.baihongyu.com/

你可能感兴趣的文章
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>