【发布时间】:2020-03-05 10:29:23
【问题描述】:
我正在尝试制作一个包含三个类文件、两个对象文件和一个主文件的程序,它可以访问这两个文件并运行操作。例如,第一个对象文件使用一个参数创建对象,然后根据该参数为其自身分配属性。
public class People {
private int height, weight;
private String specificPerson;
public People(String person){
this.specificPerson = person;
this.height = person.length * 12;
this.weight = person.length * 40;
}
public int getHeight(){return height;}
public int getWeight() {return weight;}
}
然后将这些对象存储在另一个具有容量和数组的对象的数组中:
public class peopleIndexer {
private int pcapacity, size;
private String[] peopleArray;
public peopleIndexer(int capacity){
this.pcapacity = capacity;
this.peopleArray = new String [capacity];
}
public int getCapacity(){
return pcapacity;
}
public int[] getInfo(String person){
int[] getInfo = new int[2];
int found = Arrays.binarySearch(peopleArray,person);
getInfo[0] = ?.getHeight();
getInfo[1] = ?.getWeight();//I dont know the object name yet so I put "?" for I am not sure
System.out.println("Person" + person + "is " + getInfo[0] + "tall and " + getInfo[1] + " pounds.");
}
}
我需要知道的是如何允许用户使用输入在列表中创建多个人,然后我可以允许他们稍后检索,例如:
String user_input;
People user_input = new People("user_input");
因此,如果用户输入是 jack、ryan 和 nick,我将在 peopleIndexer 中放置三个对象:
People jack = new People(jack);
People ryan = new People(ryan);
People nick = new People(nick);
【问题讨论】:
-
Would you like to create a new Person? Y or N。How many people do you want to create? -
Y,我每次都需要结交新人,人数或容量在程序开始时由用户决定。
标签: java class oop object inheritance