我的程序中有一个AddressBook
类和一个Persons
类。 AddressBook
类有一个Persons
对象数组。
import java.util.*;
public class Persons {
private String name;
private String lastName;
private String addres;
private String city;
private String state;
private int zip;
private String phoneNumber;
public Persons(String name , String lastname,String phoneNumber) {
this.name = name;
this.lastName = lastname;
this.phoneNumber = phoneNumber;
}
//some getter and setter here for all private fields and some other code
这是AddressBook.addPerson
方法:
public void addPerson(Persons prs) {
for (int i = 0; i < ArrayOfPerson.length; i++) {
if (ArrayOfPerson[i] == null) {
ArrayOfPerson[i] = prs;
break;
}
}
}
和Main
类:
public class Main {
public static void main(String[] args) {
AddressBook addr = new AddressBook();
addr.addPerson(new Persons("first name", "first lastname", "123"));// in here for example/ how can i use this person object later
addr.addPerson(new Persons("second name", "second lastname", "456"));
addr.addPerson(new Persons("third name", "thirs last name", "789"));
addr.addPerson(new Persons("fourth name", "fourth last name", "101112"));
}
}
我的问题是关于Main
课程。如何重新使用创建的人物对象?例如,我想做这样的事情:
System.out.println(x.getname());
其中x是由new
关键字作为参数创建的person对象。
打扰一下,如果我的问题是初学者的问题......我在google搜索并没有找到任何内容。
答案 0 :(得分:4)
您只需要在创建变量时将此对象分配给变量,然后您可以访问它并将其作为参数传递:
Persons firstOne = new Persons("first name","first lastname","123");
您的代码将是:
public static void main(String[] args) {
AddressBook addr = new AddressBook();
Persons firstOne = new Persons("first name","first lastname","123");
//You will use it as parameter like this:
addr.addPerson(firstOne);
addr.addPerson(new Persons("second name", "second lastname","456"));
addr.addPerson(new Persons("third name", "thirs last name","789"));
addr.addPerson(new Persons("fourth name", "fourth last name","101112"));
}
然后你可以像这样访问它:
System.out.println(firstOne.getname());
或者,如果没有创建变量,您可以通过其索引addr.get(index)
从集合中获取它:
System.out.println(addr.get(0).getname());
答案 1 :(得分:2)
您应该将每个Persons
存储到一个变量中,如下所示:
Persons p1 = new Persons("first name","first lastname","123")
然后获取字段name
:
System.out.println(p1.getName());
答案 2 :(得分:2)
你必须向这个人提供变量:
addr.addPerson(x);
System.out.println(x.getname());
然后你可以这样做:
System.out.println(addr.get(3).getname())
或者您可以从lits中获取元素:
@media all and (max-width: 1200px) and (-ms-high-contrast:active), (-ms-high-contrast:none) {
.breadcrumb.Connect {
bottom: 176%;
}
}
@media all and (min-width: 1920px) and (-ms-high-contrast:active), (-ms-high-contrast:none) {
.breadcrumb.Connect {
bottom: 181.5%;
left: -30%;
width: 160.2%;
}
}
@media all and (min-width: 1201px) and (max-width: 1919px) and (-ms-high-contrast:active), (-ms-high-contrast:none) {
.breadcrumb.Connect {
bottom: 172.5%;
left: -7.5%;
width:115.2%;
}
div.newsletter-container-connect div.right-form-block {
width: 43%;
}
}
答案 3 :(得分:2)
你会做这样的事情:
public class Main {
public static void main(String[] args) {
AddressBook addr = new AddressBook();
addr.addPerson(new Persons("first name","first lastname","123"));// in here for example/ how can i use this person object later
addr.addPerson(new Persons("second name", "second lastname","456"));
Person x = new Persons("third name", "thirs last name","789")
addr.addPerson(x);
addr.addPerson(new Persons("fourth name", "fourth last name","101112"));
System.out.println(x.getName());
}