2017-06-03 123 views
0

我真的需要你的帮助。唐t be scared of a title, cause I don吨真的知道如何描述一个问题。所以我有一个阵列从对象获取参数

public static final Server[] SERVERS = { 
     new Server(0, "Test0", "localhost", "00000", "client"), 
     new Server(1, "Test1", "localhost", "00001", "client"), 
     new Server(2, "Test2", "localhost", "00002", "client") 
}; 

如何从该数组中获得Test0,Test1和Test2?我想过服务器[2],但那会选择整个对象。我不知道如何得到。 我会非常感谢你的帮助。谢谢你在前进:3

P.S.For例如:

File f = new File("abc");

我怎么能说那个东西,这是在括号?我的意思是什么是“ABC”的专有名称?参数?变量?

+2

如果你有例如一个方法声明'void m(int x){...}',然后'x'是*参数*。如果你再调用方法'm(10)',那么'10'就是*参数*。这同样适用于构造函数,因此''“abc”'是['File'构造函数的参数](http://docs.oracle.com/javase/8/docs/api/java/io/File.html# File-java.lang.String-)参数'pathname'。 – Radiodef

+0

在'File f = new File(“abc”);','“abc”'是一个参数。我们在方法中使用声明的参数项:'public File(String pathname)...'。这里:'pathname'是一个参数。 – davidxxx

回答

2

Remember Servers是一个服务器数组,因此您可以执行SERVERS [x],并且可以访问位于给定索引x处的服务器对象。

之后,你只需要使用的干将,是这样的:

String name = SERVERS[2].getName(); 
int index = SERVERS[2].getIndex(); 
//etc etc 
2

你必须提供在Server类返回服务器名称的方法。

例如:

public String getName(){ 
    return name; 
} 

这样,你能做到这一点找回它:

String name = SERVERS[2].getName();