java 如何从另一个类获取数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36167856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get an array from another class
提问by CJ.
I can't seem to find an answer for this, I have a lot of different arrays, and I want to stuff all of them into one class, to make my main one neater.
我似乎无法找到答案,我有很多不同的数组,我想将所有数组都塞到一个类中,以使我的主要数组更整洁。
Testy obj = new Testy();
But i have no clue on how to get an array from here. Sorry if this is a bad question, this is my first time posting on the site. I'm just trying to get an array called testArray from the class called Testy.
但我不知道如何从这里获取数组。对不起,如果这是一个不好的问题,这是我第一次在网站上发帖。我只是想从名为 Testy 的类中获取一个名为 testArray 的数组。
Code inside Testy:
Testy 中的代码:
public class Testy {
public static void ArrayTesting(){
int testArray[]={56,1,4,175};
}}
回答by Keno Clayton
The standard way to get any variables from another class is to create a getter for that particular variable. After that, you can call the getter in order to get the value of whatever variable you want.
从另一个类中获取任何变量的标准方法是为该特定变量创建一个 getter。之后,您可以调用 getter 以获取您想要的任何变量的值。
Example:
例子:
class MyClass
{
//Variable I want to return
private int[] numArray = {1, 2, 3};
public int[] getNumArray() {
return numArray.clone();
}
}
and you can call it by saying
你可以这样称呼它
MyClass newClassObj = new MyClass();
int[] secondArray = newClassObj.getNumArray();