java中声明方法的顺序 [英] Order of declaring methods in java

查看:37
本文介绍了java中声明方法的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C/C++ 中,我们必须在调用函数之前声明它们.在 Javascript 中有一个提升变量和函数.我找不到有关 Java 的信息.是否也有方法的提升?

解决方案

在 java 中,函数/过程被称为方法.唯一的区别是函数返回值.不,没有像 JS 那样的提升(感谢上帝).对变量的唯一要求是您必须在使用它们之前创建它们.就像C一样.但是方法是对象的一部分.所以它们被附加到对象上,你可以在它们的声明之上调用它们(虚拟方法,一切都是虚拟的:)).因为调用它们实际上涉及到 .method() 并且 Class 在执行之前就已经被编译和加载了.(一些反射可以绕过或改变这种行为).

编译器可以相对自由地重新排序,但例如 volatile 可以禁止这种行为.顺便说一句:提升和重新排序是同一个东西吗?>

In C/C++ we have to declare functions before invoking them. In Javascript there is a hoisting variables and functions. I cannot find info about Java. Is there a hoisting of methods too?

解决方案

In java functions/procedures are called methods. Only difference is that function returns value. No , there is no hoisting like JS(thank god). Only requirement for variables is that you have to create them before you use them. Just like C. But methods are part of an object. So they are attached to object and you can call them above their declaration (virtual method, everything is virtual:) ). Because calling them actually involves <Class>.method() And Class is already compiled and loaded before the time are executing it. (some reflections can bypass or change this behavior tho).

Compiler is relatively free to reorder things, but for example volatile can forbid this behaviour. By the way : Are hoisting and reordering the same thing?

这篇关于java中声明方法的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆