Java 让构造函数访问静态变量
现在我有两个.java文件。 Main.java:Java 让构造函数访问静态变量,java,constructor,static-variables,Java,Constructor,Static Variables,现在我有两个.java文件。 Main.java: public class Main { static int integer = 15; NeedInteger need = new NeedInteger(); } 以及NeedInteger.java public class NeedInteger { System.out.println(integer); } 这当然非常简单,但是有什么方法可以实现吗?将变量传递给类构造函数 数组引用只是一个引用 或者您可
public class Main {
static int integer = 15;
NeedInteger need = new NeedInteger();
}
以及NeedInteger.java
public class NeedInteger {
System.out.println(integer);
}
这当然非常简单,但是有什么方法可以实现吗?将变量传递给类构造函数 数组引用只是一个引用
或者您可以传入类本身,或者使用静态meh。将变量传入类构造函数 数组引用只是一个引用 或者您可以传入类本身,或者使用静态meh。向NeedInteger添加构造函数,如果您还需要存储它,还可以选择添加成员:
public class NeedInteger {
private int integer;
public NeedInteger(int integer) {
this.integer = integer;
System.out.println(integer);
}
}
然后在创建实例时传递值:
public class Main {
static int integer = 15;
NeedInteger need = new NeedInteger(integer);
}
向NeedInteger添加构造函数,如果还需要存储,还可以选择添加成员:
public class NeedInteger {
private int integer;
public NeedInteger(int integer) {
this.integer = integer;
System.out.println(integer);
}
}
然后在创建实例时传递值:
public class Main {
static int integer = 15;
NeedInteger need = new NeedInteger(integer);
}
您必须执行一些糟糕的juju操作,例如使用全局变量或将其传递给构造函数 注意:您的
public class NeedInteger {
System.out.println(integer);
}
没有办法。我建议将所有这些内容改写如下:
public Class NeedInteger {
NeedInteger(int integer) {
System.out.println(integer);
}
}
如果你真的想在建筑上完成工作
编辑:从你的评论上面
相反,将类组织为:
public Class NeedStringArray {
NeedStringArray(String[][][] stringArr) {
//work with String array here
}
}
这没有实际的额外开销,因为不会传递实际数组,而只传递对它的引用。您可能希望将数组设置为final或其他形式,以避免在NeedStringArray构造函数中对其进行编辑。您必须执行一些错误的juju操作,例如使用全局变量或将其传递给构造函数 注意:您的
public class NeedInteger {
System.out.println(integer);
}
没有办法。我建议将所有这些内容改写如下:
public Class NeedInteger {
NeedInteger(int integer) {
System.out.println(integer);
}
}
如果你真的想在建筑上完成工作
编辑:从你的评论上面
相反,将类组织为:
public Class NeedStringArray {
NeedStringArray(String[][][] stringArr) {
//work with String array here
}
}
这没有实际的额外开销,因为不会传递实际数组,而只传递对它的引用。您可能希望将数组设置为final或其他形式,以避免在NeedStringArray构造函数中对其进行编辑。integer是私有的,因此NeedInteger无法访问它。您必须将其公开,或者使用setter或getter,并且需要使用Main.integer,因为它是静态的
通常,您在构造函数中设置。integer是私有的,因此NeedInteger无法访问它。您必须将其公开,或者使用setter或getter,并且需要使用Main.integer,因为它是静态的
通常,您可以在构造函数中进行设置。根据您的评论,我认为您可以在
或者像其他人建议的那样,让第二个类接受构造函数中对数组的引用。然后,您可以使用依赖注入框架,例如,根据您的评论将它们连接起来。我想说,您可以在
或者像其他人建议的那样,让第二个类接受构造函数中对数组的引用。然后,您可以使用依赖注入框架,例如,将它们连接起来,正如许多人回答的那样,正确的方法是将值传递给新类的构造函数 如果由于某种原因您不能这样做,那么您可以在Main中使用publicstaticaccessor方法来访问该值,这比仅仅公开字段要好一些 例如
正如许多人回答的那样,正确的方法是将值传递给新类的构造函数 如果由于某种原因您不能这样做,那么您可以在Main中使用publicstaticaccessor方法来访问该值,这比仅仅公开字段要好一些 例如
将变量传递给类构造函数。您需要更具体地了解您的实际需要。假设我有大约2000个第二个类的实例,它们都需要访问在主类中创建的三维字符串数组。我假设您指的不是字符串[][][][]数组=Main.threedimensionalstringaray;您是否询问如何从其他类或其他对象引用静态变量?请将该变量传递给类构造函数。您需要更具体地了解您的实际需要。假设我有大约2000个第二个类的实例,它们都需要访问在主类中创建的三维字符串数组。我假设您指的不是字符串[][][][]数组=Main.threedimensionalstringaray;你是在问如何引用其他类或其他类的静态变量吗?这似乎是个好主意,但这不会在类的每个实例中存储我的巨大数组吗?因为数组是引用,它只复制引用,而不是实际内容。所以这不是问题。@user1098680不,如果你传递一个数组引用,它将有一个数组引用,而不是数组的副本。这似乎是一个好主意,但这不会在类的每个实例中存储我的巨大数组吗?因为数组是引用,它只复制引用,而不是实际内容。所以我
这不是问题。@user1098680否,如果传递数组引用,它将具有数组引用,而不是数组的副本。因此,如果我使用公共静态整数,我的新实例将能够使用Main.integer访问它?是的。静态意味着只有一个实例,所以您可以使用Main.integer。如果它不是变量,您可以使用Main i=新Main;i.integer.Perfect这似乎是最简单的解决方案。-关于静态变量的更多信息。通常最好使用公共访问器方法,而不是将字段本身公开。因此,如果我使用公共静态整数,我的新实例将能够使用Main.integer访问它?是的。静态意味着只有一个实例,所以您可以使用Main.integer。如果它不是变量,您可以使用Main i=新Main;i.integer.Perfect这似乎是最简单的解决方案。-关于静态变量的更多信息。通常最好使用公共访问器方法,而不是将字段本身公开。