【发布时间】:2017-11-21 10:56:00
【问题描述】:
我已删减代码以缩短页面,但我想问如何将 personInterests 更改为它自己的类。为这个模糊的问题道歉,但基本上我想将我的 Person 类中的 personInterests 更改为 personInterests 有多个变量的类。
import java.util.ArrayList;
import java.util.*;
public class Person{
private String personName;
private String[] personInterests = new String[3];
public Person(String personName, String[] personInterests){
this.personName = personName;
this.personInterests = personInterests;
}
public void setInterests(String[] personInterests){
this.personInterests = personInterests;
}
public String[] getInterests(){
return personInterests;
}
public String getName(){
return personName;
}
public String toString(){
String result = getName() + " ";
for (String interests : personInterests) {
result += interests + " ";
}
return result;
}
}
这是我对它如何工作的想法,只是不确定我将如何使用这个类并稍后调用它。
import java.util.ArrayList;
import java.util.*;
public class Interests {
private int interestDangerRating;
private String interestName;
private ArrayList<Interests> interestsList = new ArrayList<>();
public Interests (int interestDangerRating ,String interestName){
this.interestDangerRating = interestDangerRating;
this.interestName = interestName;
}
public void addInterests(Interests p){
interestsList.add(p);
}
Interests getInterests(int i){
return interestsList.get(i);
}
}
感谢任何帮助,正如我所说,这段代码大部分已被删除,这是一个已经完成的旧项目,只是想看看我是否可以更改一些功能。
【问题讨论】:
-
您创建的
Interests类有什么问题?你能不能从其他任何一个类中调用它?Interests interests = new Interests;