如何使变量的范围全局(而不使其实际上全局)

发布于 2024-12-07 06:19:16 字数 366 浏览 1 评论 0原文

如何使字符串变量的范围(在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不回头走下去 2024-12-14 06:19:16

OOP 中的基本概念之一是范围的概念:在几乎所有情况下,将变量的范围(即变量可见的位置)减小到其最小可行范围是明智的。

我假设您绝对需要在两个函数中使用该变量。因此,在这种情况下,最小可行范围将涵盖这两个功能。

public class YourClass
{
   private String yourStringVar;

   public int pleaseGiveYourFunctionProperNames(String s){
      this.yourStringVar = s;
   }
   public void thisFunctionPrintsValueOfMyStringVar(){
      System.out.println(yourStringVar);
   }
}

根据情况,您必须评估变量所需的范围,并且必须了解增加范围的含义(更多访问 = 潜在更多依赖项 = 更难跟踪)。

举个例子,假设您绝对需要它成为一个全局变量(正如您在问题中所说的那样)。具有全局作用域的变量可以被应用程序中的任何内容访问。这是非常危险的,我将证明这一点。

要创建具有全局作用域的变量(确切地说,在 Java 中不存在全局变量之类的东西),您需要创建一个带有静态变量的类。

public class GlobalVariablesExample
{
   public static string GlobalVariable;
}

如果我要更改原始代码,它现在看起来像这样。

public class YourClass
{
   public int pleaseGiveYourFunctionProperNames(String s){
      GlobalVariablesExample.GlobalVariable = s;
   }
   public void thisFunctionPrintsValueOfMyStringVar(){
      System.out.println(GlobalVariablesExample.GlobalVariable);
   }
}

这可能非常强大,也非常危险,因为它可能会导致您意想不到的奇怪行为,并且您会失去面向对象编程赋予您的许多能力,因此请小心使用它。

public class YourApplication{
    public static void main(String args[]){
        YourClass instance1 = new YourClass();
        YourClass instance2 = new YourClass();

        instance1.pleaseGiveYourFunctionProperNames("Hello");
        instance1.thisFunctionPrintsValueOfMyStringVar(); // This prints "Hello"

        instance2.pleaseGiveYourFunctionProperNames("World");
        instance2.thisFunctionPrintsValueOfMyStringVar(); // This prints "World"
        instance1.thisFunctionPrintsValueOfMyStringVar(); // This prints "World, NOT Hello, as you'd expect"
    }
}

始终评估变量的最小可行范围。不要让它变得比需要的更容易访问。

另外,请不要将变量命名为 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.

public class YourClass
{
   private String yourStringVar;

   public int pleaseGiveYourFunctionProperNames(String s){
      this.yourStringVar = s;
   }
   public void thisFunctionPrintsValueOfMyStringVar(){
      System.out.println(yourStringVar);
   }
}

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.

public class GlobalVariablesExample
{
   public static string GlobalVariable;
}

If I were to alter the original code, it would now look like this.

public class YourClass
{
   public int pleaseGiveYourFunctionProperNames(String s){
      GlobalVariablesExample.GlobalVariable = s;
   }
   public void thisFunctionPrintsValueOfMyStringVar(){
      System.out.println(GlobalVariablesExample.GlobalVariable);
   }
}

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.

public class YourApplication{
    public static void main(String args[]){
        YourClass instance1 = new YourClass();
        YourClass instance2 = new YourClass();

        instance1.pleaseGiveYourFunctionProperNames("Hello");
        instance1.thisFunctionPrintsValueOfMyStringVar(); // This prints "Hello"

        instance2.pleaseGiveYourFunctionProperNames("World");
        instance2.thisFunctionPrintsValueOfMyStringVar(); // This prints "World"
        instance1.thisFunctionPrintsValueOfMyStringVar(); // This prints "World, NOT Hello, as you'd expect"
    }
}

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.

昔梦 2024-12-14 06:19:16

唔。您显然需要一些面向对象编程的课程。在面向对象中,没有“全局”变量。但是定义为类中(方法外部)成员的任何变量在该类中都是全局的。

public class MyClass {

    private String myVar; // this can be accessed everywhere in MyClass

    public void func1(String s) {
        myVar = s;
    }

    public void func2(String q) { // why is q needed here? It's not used
        System.out.println(myVar);
    }

}

因此,仅当您先调用 func1 时,func2 才会输出 s 的值。

final Myclass myClass = new MyClass();
myClass.func1("value");
myClass.func2("whatever"); // will output "value"

另外,为什么在您的示例中方法返回 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.

public class MyClass {

    private String myVar; // this can be accessed everywhere in MyClass

    public void func1(String s) {
        myVar = s;
    }

    public void func2(String q) { // why is q needed here? It's not used
        System.out.println(myVar);
    }

}

So func2 will output the value of s ONLY IF you call func1 first.

final Myclass myClass = new MyClass();
myClass.func1("value");
myClass.func2("whatever"); // will output "value"

Also, why are the methods returning int in your example? They should be void.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文