如何使变量的范围全局(而不使其实际上全局)
如何使字符串变量的范围(在Java中)全局。以便从另一个函数访问它 例如
//String b="null"; I don't want to do this... because if i do this, fun2 will print Null
public int func1(String s)
{
String b=s;
}
public int func2(String q)
{
System.out.println(b);//b should be accessed here and should print value of s
}
任何帮助...谢谢
How can I make scope of a String variable(In Java) global.So that it is accessed from another function
Eg
//String b="null"; I don't want to do this... because if i do this, fun2 will print Null
public int func1(String s)
{
String b=s;
}
public int func2(String q)
{
System.out.println(b);//b should be accessed here and should print value of s
}
Any Help... Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
OOP 中的基本概念之一是范围的概念:在几乎所有情况下,将变量的范围(即变量可见的位置)减小到其最小可行范围是明智的。
我假设您绝对需要在两个函数中使用该变量。因此,在这种情况下,最小可行范围将涵盖这两个功能。
根据情况,您必须评估变量所需的范围,并且必须了解增加范围的含义(更多访问 = 潜在更多依赖项 = 更难跟踪)。
举个例子,假设您绝对需要它成为一个全局变量(正如您在问题中所说的那样)。具有全局作用域的变量可以被应用程序中的任何内容访问。这是非常危险的,我将证明这一点。
要创建具有全局作用域的变量(确切地说,在 Java 中不存在全局变量之类的东西),您需要创建一个带有静态变量的类。
如果我要更改原始代码,它现在看起来像这样。
这可能非常强大,也非常危险,因为它可能会导致您意想不到的奇怪行为,并且您会失去面向对象编程赋予您的许多能力,因此请小心使用它。
始终评估变量的最小可行范围。不要让它变得比需要的更容易访问。
另外,请不要将变量命名为 a、b、c。并且不要将变量命名为 func1,func2。它不会让你的应用程序变慢,也不会因为输入一些额外的字母而让你丧命。
One of the fundamental concepts in OOP is the concept of scope: in almost all cases it is wise to reduce the scope of a variable (i.e. where it is visible from) to its minimum viable range.
I'm going to assume you absolutely require the use of that variable in both functions. Therefore, the minimum viable scope in this case would cover both functions.
Depending on the situation, you must assess the required scope of a variable, and you must understand the implications of increasing the scope (more access = potentially more dependencies = harder to keep track).
As an example, let's say you absolutely needed it to be a GLOBAL variable (as you call it in your question). A variable with Global scope can be accessed by anything within the application. This is exceptionally dangerous, which I will demonstrate.
To make a variable with global scope (there are no such things as global variables, exactly, in Java), you create a class with a static variable.
If I were to alter the original code, it would now look like this.
This can be exceptionally powerful, and exceptionally dangerous as it can lead to weird behaviour that you do not expect, and you lose many of the abilities that object oriented programming gives you, so use it carefully.
Always assess the minimum viable scope for your variables. Do not make it more accessible than it needs to be.
Also, please don't name your variables a,b,c. And don't name your variables func1,func2. It doesn't make your application any slower, and it won't kill you to type in a few extra letters.
唔。您显然需要一些面向对象编程的课程。在面向对象中,没有“全局”变量。但是定义为类中(方法外部)成员的任何变量在该类中都是全局的。
因此,仅当您先调用 func1 时,func2 才会输出 s 的值。
另外,为什么在您的示例中方法返回 int ?它们应该是无效的。
Hmm. You clearly need some lessons in object-oriented programming. In OO there is no "global" variable. But any variable defined as a member in a class (outside a method) is global within that class.
So func2 will output the value of s ONLY IF you call func1 first.
Also, why are the methods returning int in your example? They should be void.