简体   繁体   English

我如何在整个Java包中使用一些变量?

[英]How do I have some variables can be used in the whole package of Java?

I will need to have some variables, for example type Identifier, type Declaration, and both of these types are type Expression. 我将需要一些变量,例如类型标识符,类型声明和这两种类型都是表达式类型。 And under the type Identifier, I want to have some variables, for example: ide, binder. 在Identifier类型下,我想要一些变量,例如:ide,binder。 They should have type Identifier, and under the type Declaration, I want to have: first, second, they should have type Declaration. 它们应该具有类型标识符,并且在类型声明下,我希望:第一,第二,它们应该具有类型声明。 What can I do to make it possible to refer to these variables later on without making an instance out of the class? 我怎样做才能使以后引用这些变量而又不使类脱离实例呢? Thank 谢谢

So I first create a class Expression: 所以我首先创建一个类Expression:

public abstract class Expression{   
}

And I have class Identifier: 我有类标识符:

public class Identifier extends Expression{
}

Finally I have class Declaration: 最后我有课声明:

public class Declaration extends Expression{
}

The simple answer is to declare the variables 'static' and without 'public' or 'protected' or 'private' access modifiers, leaving the variables package-private or "default" access: 简单的答案是将变量声明为“静态”,而没有“ public”或“ protected”或“ private”访问修饰符,而将变量保留为package-private或“ default”访问权限:

package com.yourdomain.whatever;
public class Identifier extends Expression {
  static Ide ide;
  static Binder binder;
  ...
}

The lack of access modifier means that the variables will be accessible to other types in the same package, but not outside the package. 缺少访问修饰符意味着同一包中的其他类型都可以访问变量,但包外部不能访问。

But two things are bad about this idea: you are exposing variables directly, and you are using 'static' variables, which are essentially global variables and therefore quite nasty. 但是这个想法有两个不好的地方:你直接暴露变量,而你正在使用'静态'变量,这些变量本质上是全局变量,因此非常讨厌。

You could make the variables more widely accessible by declaring them 'public' or 'protected', but that just makes matters worse. 您可以通过将变量声明为“公共”或“受保护”来更广泛地访问变量,但这只会使事情变得更糟。

So I ask why you want those variables to be directly accessible, and what is your reason not to instantiate the class? 所以我问为什么要直接访问这些变量,为什么不实例化该类呢? Neither of those seem like great ideas from here. 这些似乎都不是来自这里的好主意。

One option would be to make these variables static and final , effectively making them constants: 一种选择是使这些变量成为staticfinal ,从而有效地使它们成为常量:

public class Identifier extends Expression {
    public static final String ide = "IntelliJ";
    public static final String binder = "binder";

    // ...
}

And now these variables can be accessed directly using the class name: 现在,可以使用类名称直接访问这些变量:

String myIde = Identifier.ide;

To make a global variable, make it public and static . 要创建全局变量,请将其设置为publicstatic Making it public makes it so that the variable is accessible anywhere in the package. 使其公开,以便可以在包中的任何位置访问变量。 Making it static makes it so that you don't have to make an instance of the class to access the variable. 将其设置为静态使其免于必须创建类的实例来访问变量。 For example, 例如,

public class Identifier extends Expression{
    public static int globalVar;
    //...
}

Then it can be accessed anywhere else in the package in other classes by calling Identifier.globalVar for instance. 然后,通过调用Identifier.globalVar ,可以在其他类的包中的任何其他位置访问它。

EDIT: I should make note that the access identifier public makes it so that the variable is accessible anywhere in the Java program, whether it'd be inside or outside the package. 编辑:我应该注意,访问标识符public使得变量可以在Java程序中的任何地方访问,无论它是在包内还是在包外。 Access identifier protected makes it accessible in the class, package, and subclasses. protected访问标识符使它可以在类,包和子类中访问。 If you want the variable to be accessible in the same package only and not in subclasses or anywhere outside the package, then the proper choice is to have no access identifier. 如果您希望变量只能在同一个包中访问,而不能在子类或包外的任何地方访问,那么正确的选择是没有访问标识符。 However, if it doesn't matter, then any of the three choices discussed will suffice. 但是,如果没有关系,那么讨论的三个选择中的任何一个都足够。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM