gpt4 book ai didi

java - 我可以使用已添加到 ArrayList 的不同类的方法吗?

转载 作者:行者123 更新时间:2023-11-29 06:12:06 24 4
gpt4 key购买 nike

我有一个 Project 类,我希望它包含我想做的项目。我是一名珠宝商,基本上想从我正在进行的每个项目中获得一个文件。现在,每个文件都有一个名称、开始日期、截止日期和描述。我想调用 printPro() 我只是不知道如何调用。

项目类如下所示:

public class Project {
String name;
int date;//start date
int fDate;//finish date
String description;

public Project(String gName, int gDate, int gFDate, String gDescription){
// mean they are the ones that are given.
}
public void receivedName(String receivedName) {
name= receivedName;
}
public void receivedDate(int receivedDate) {
date= receivedDate;
}
public void receivedFDate(int receivedDate) {
fDate= receivedDate;
}


public String returnName(){
return name;
}
public int returnDate(){
return date;
}
public int returnFDate(){
return fDate;
}
public String returnDescription(){
return description;
}

public String printPro(int index){
String tempString = ("Project Name: " + name +
"\nStart Date: " + date +
"\nFinish Date: " + fDate + "\nDescription: " + description);
return tempString;
}

public void printProject(int index){

System.out.println("Project Name: " + name +
"\nStart Date: " + date +
"\nFinish Date: " + fDate + "\nDescription: " + description);
}

}

现在我所拥有的只是通过控制台运行的一切。当我学习如何创建和导入文本文件时,我打算稍后构建一个窗口。不管怎样,这里是控制台的代码:

public class Top{
Math help = new Math();//Creates a Math object
People person = new People();//Creates a People object
// Project nProject= new Project();
ArrayList<Project> mProject = new ArrayList<Project>();
Project[] storeProject; //Tried to create an array of Projects I could just add to.
Project[] storeTempProject;
StringTest sTest = new StringTest(); // This is my class to use the console.
String choice; //Temporary strings
String redo;

public static void main (String [] args){
// ============Project Menu =========================
private void project() {
System.out.println("ADD, DELETE, REVIEW, or BACK?");
sTest.stringReader();
choice = sTest.getString();

if (sTest.test(choice, "add")){
System.out.println("What would you like to call this project?");
sTest.stringReader();
String name = sTest.getString();

System.out.println("When would you like it to start?");
sTest.stringReader();
String startDate = sTest.getString();
int sDate = Integer.parseInt(startDate);

System.out.println("When would you like it to end?");
sTest.stringReader();
String finishDate = sTest.getString();
int fDate = Integer.parseInt(finishDate);

System.out.println("What is the description of your project?");
sTest.stringReader();
String projectDescription = sTest.getString();

mProject.add(new Project(name, sDate, fDate, projectDescription));
}
}

我从我的代码中删除了一些不必要的东西,所以它不会让这个问题陷入困境。如果您需要更多信息,请告诉我。我已经搜索了一个小时,所以我假设这行不通,我需要换一种方式。

最佳答案

我不完全确定你的问题,但也许这就是你所追求的:

您可以像这样在 ArrayList 的元素上调用方法:

for (Project proj : mProject) {
proj.printPro();
}

要显示第一个,只需执行以下操作:

mProject.get(0) 

将返回 ArrayList 中的第一个项目。要返回第二个,只需将“get”的参数更改为 1。您应该能够弄清楚如何获取其余部分。

关于java - 我可以使用已添加到 ArrayList 的不同类的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6433576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com