静态方法与否?全局变量? [英] Static Methods or Not? Global variables?

查看:58
本文介绍了静态方法与否?全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道哪种方式更有效.

I want to know what way is more efficient.

  1. 没有全局变量,通过参数传递变量,所有方法都是静态的

  1. No global variables, passing variables through parameters, having all methods static

没有全局变量,只有 main 方法 static 并在 main 中创建类对象来访问方法

No global variables, having only main method static and creating class object in main to access methods

只使用全局变量,只有 main 方法 static 并在 main 中创建类对象来访问方法

Use only global variables, having only main method static and creating class object in main to access methods

我目前正在使用方法 3,但我想知道哪种方法更有效.这个类不会被它之外的任何其他类使用,它几乎是独立的.

I am currently using method 3 but I want to know what is more efficient. This class will not be used by any other class outside of it, it pretty much stands alone.

我的代码结构示例:

public class myClass {
   private int globalVariable;

   public static void main(String args[]) {
      myClass c;
      c.someMethod(); // Changes global variable from method
      System.out.println(someMethod); // Prints solution
   }

   public void someMethod() {...}
}

推荐答案

  • 没有一个班级是一座孤岛.
  • 没有灵丹妙药,至少在编程中非常正确.
  • 过早的优化是万恶之源.
  • 在 Java 中,我们没有全局变量.我们只有类变量、实例变量和方法变量.
  • 我想在这里解释我的最后一点.事实上,将下面评论中正在进行的讨论带到实际帖子中.

    I am trying to explain here my last point. In fact, bringing the discussion, that is going-on in comments below, to the actual post.

    首先看看这个,C# 的一个 SO 线程.还有人提出同样的建议,那就是,

    First look at this, an SO thread of C#. There folks are also suggesting the same thing, which is,

    • There are no global variables in C#". A variable is always locally-scoped. The fundamental unit of code is the class, and within a class you have fields, methods, and properties
    • I would personally recommend erasing the phrase "global variable" from your vocabulary (this is in the comment section of the original question)

    那么,我们开始吧.

    反驳: 类是全局范围的,因此所有类变量都是全局范围的.因此应该称为全局.

    retort: Classes are globally scoped, and thus all class variables are globally scoped. Hence should be called global.

    反驳:并非所有类都是全局范围的.一个类可以是package-private.因此,里面的 static 变量在包外是不可见的.因此,不应称为全局.此外,类可以嵌套,因此也可以是 private 并且肯定可以有一些 static 变量,但那些不会被称为全局变量.

    counter-retort: Not all classes are globally scoped. A class can be package-private. Therefore, the static variables in there will not be visible outside the package. Hence, should not be called as global. Furthermore, classes can be nested, thus can be private as well and definitely can have some static variables but those wouldn't be called global.

    反驳: public 类是全局范围的,因此所有类变量都是全局范围的.

    retort: public classes are globally scoped, and thus all class variables are globally scoped.

    反驳:不完全是.我想把前面的论点移到这里,但在一个可变的层面上.无论类本身是否是public.其中的变量可以是protectedpackage-privateprivate.因此,在这种情况下,static 变量将不是全局变量.

    counter-retort: Not exactly. I would like to move the previous argument here but on a variable level. No matter if the class itself is public. The variables in there can be protected, package-private and private. Hence, static variables will not be global in that case.

    现在,如果您想将 public static 类中的 public static 变量作为全局变量调用,则可以通过任何方式调用它.但是考虑到这一点,当您创建一个新的 ClassLoader(作为引导程序 ClassLoader 的子项)并加载一个您已经加载的类时.然后会产生一个非常新的类副本".-- 用它自己的新 static 集完成.确实非常非全球".然而,我们在 Java 中不使用 global 这个词,因为它容易混淆事物,然后我们需要提供大量的解释来使一切都清楚.人们很喜欢用static变量来解释Java中global变量的特性.这没有问题.如果您在任何其他语言中遇到一些问题/代码并且使用全局变量并且您需要将该代码转换为 Java,那么您很可能使用 static 变量作为替代.

    Now, if you like to call public static variable in public static class, as global then call it by any means. But consider this, when you create a new ClassLoader (as a child of the bootstrap ClassLoader) and load a class that you've already loaded. Then that results in a "very new copy of the class" -- complete with its own new set of statics. Very "un-global", indeed. However, we don't use the word global in Java because it tends to confuse the things and then we need to come with whole lot of explanations just to make everything clear. Folks rightly like to explain the feature of global variables in Java by static variables. There is no problem in that. If you have some problem/code in any other language and that is using global variables and you need to convert that code to Java, then you most likely make use of static variable as an alternative.

    我喜欢在这里呈现的几个例子

    1. 当我开始 Java 时,导师喜欢解释传递对象类型变量和原始变量的区别.他们经常使用术语对象是pass-by-reference,而原语是pass-by-value.学生们发现这个解释相当混乱.因此,我们提出了 Java 中的所有内容都是按值传递的概念.我们解释了对象引用是按值传递的.它变得更加清晰和简单.

    1. When I started Java, instructors like to explain the difference of passing object type variable and primitive variables. And they constantly use the term objects are pass-by-reference, whereas primitives are pass-by-value. Students found this explanation quite confusing. So, we came up with the notion that everything in Java is pass-by-value. And we explain that for objects references are pass-by-value. It becomes much more clear and simple.

    同样,也有支持多重继承的语言.但是 Java 没有,同样可以说.但是人们倾向于使用 interface 来解释该功能.他们通过实现许多接口的类来解释它,并称之为多重继承.这完全没问题.但是这个类实际上是通过继承许多接口来接收的.坦白说,没什么.为什么?

    Similarly, there are languages which support multiple-inheritance. But Java doesn't, again arguably speaking. But folks tend to explain that feature using interfaces. They explain it by class implementing many interfaces, and call it multiple-inheritance. That's perfectly fine. But what the class, actually, receives by inheriting a number of interfaces. Frankly speaking, nothing. Why?

    .因为接口中的所有变量都是隐式的publicfinalstatic,这显然意味着它们属于类,任何人都可以访问它们.现在我们可以说,也许接口中会有一些inner class,那么实现接口的类就会有它.但同样,这将是 static 隐式并且属于接口.因此,该类将获得的所有内容都是方法.并且不要忘记定义和约定,实现这个接口的类必须提供所有方法的实现或声明自己abstract".因此,那个班级只会得到责任而没有什么.但这以一种绝妙的方式解决了我们的问题.

    . Because all the variables in interfaces are implicitly public, final and static, which apparently means those belongs to the class and anyone can access those. Now we can say that perhaps there would be some inner class in the interface, then the class implementing the interface will have it. But again that will be static implicitly and will belong to the interface. Therefore, all what the class will get are methods. And don't forget just the definition and the contract which says, "the class implementing this interface must provide the implementation of all methods or declare itself abstract". Hence, that class will only get responsibilities and nothing much. But that solves our problems in a brilliant way.

    底线

    因此,我们说

    • Java 中没有全局变量
    • Java 不支持多重继承,但可以通过实现多个接口来实现.这确实有效
    • Java 中没有传递引用,但引用是传递值

    现在我喜欢多选几个地方

    • Java 不支持全局的、可普遍访问的变量.您可以使用具有静态变量的类获得相同类型的效果 [Ref]
    • 然而,ObjectiveC 中的 extern 并不能替代 Java 中的类作用域静态变量,实际上它更像是一个全局变量……所以要谨慎使用.[参考]
    • 代替 C/C++ 中的全局变量,Java 允许将类中的变量声明为静态 [参考]
    • 此外,过度使用静态成员可能会导致与支持全局变量和全局函数的 C 和 C++ 等语言中遇到的问题类似的问题.[参考]
    • Java does not support global, universally accessible variables. You can get the same sorts of effects with classes that have static variables [Ref]
    • However, extern in ObjectiveC is not an alternative to a class-scoped static variable in Java, in fact it is more like a global variable … so use with caution. [Ref]
    • In place of global variables as in C/C++, Java allows variables in a class to be declared static [Ref]
    • Furthermore, the overuse of static members can lead to problems similar to those experienced in languages like C and C++ that support global variables and global functions. [Ref]

    所有这些都在推断一个相同的想法.哪个是 Java 不支持全局变量.

    见鬼,我写了这么多.对不起各位.

    Hell, I wrote that much. Sorry folks.

    这篇关于静态方法与否?全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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