本文介绍了如何使变量的范围为全局(不使其实际为全局)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使String变量(在Java中)的作用域成为全局范围,以便从另一个函数访问它例如

How can I make scope of a String variable(In Java) global.So that it is accessed from another functionEg

//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

    }

任何帮助...谢谢

推荐答案

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

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).

作为一个例子,假设您绝对需要将其作为GLOBAL变量(如您在问题中所称).应用程序中的任何内容都可以访问具有Global作用域的变量.我将证明这是非常危险的.

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.

要创建具有全局作用域的变量(在Java中完全没有全局变量之类的东西),请创建一个具有静态变量的类.

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.

此外,请不要将变量命名为a,b,c.而且不要将变量命名为func1,func2.它不会使您的应用程序变慢,也不会杀死您键入一些额外的字母.

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.

这篇关于如何使变量的范围为全局(不使其实际为全局)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 15:14