Java 创建方法和对象
大家好,所以我得到了这个驱动程序类,并被告知要创建类以使驱动程序类正常工作。在很大程度上,我认为我的思路是正确的,但我也不完全确定,因为我是java新手。我收到2个编译错误,因为我没有添加add(temp)方法。老实说,我不知道该把这个方法放进哪个类。现在我在团队课程中有它,但是我得到了一个编译错误。如果有人能给我一些见解,我将不胜感激Java 创建方法和对象,java,class,methods,Java,Class,Methods,大家好,所以我得到了这个驱动程序类,并被告知要创建类以使驱动程序类正常工作。在很大程度上,我认为我的思路是正确的,但我也不完全确定,因为我是java新手。我收到2个编译错误,因为我没有添加add(temp)方法。老实说,我不知道该把这个方法放进哪个类。现在我在团队课程中有它,但是我得到了一个编译错误。如果有人能给我一些见解,我将不胜感激 public class Driver{ public static void main(String args[]) throws Exception{
public class Driver{
public static void main(String args[]) throws Exception{
//All information is stored in input.txt and is being
//read in below.
Scanner input = new Scanner(new File("input.txt"));
//Creates a new League and passes in a String signifying
//which league it is.
League american = new League("AL");
League national = new League("NL");
for(int i=0; i<15; i++){
//Creates a new team and adds the current team
//to the american league.
//You can assume there are exactly 15 teams in each league.
Team temp = new Team(input.next());
american.add(temp); // compile error
}
for(int i=0; i<15; i++){
//Creates a new team and adds the current team
//to the national league.
//You can assume there are exactly 15 teams in each league.
Team temp = new Team(input.next());
national.add(temp); // compile error
}
}
我的团队课
public class Team
{
// instance variables - replace the example below with your own
private String team;
/**
* Constructor for objects of class Team
*/
public Team(String Team)
{
team = Team;
}
public void setTeam(String Team){
this.team = Team;
}
public String getTeam(){
return team;
}
public String add(League y)
{
return y; //compiling error
}
}
此函数返回一个字符串
。您正在传递参数y
,它是一个联盟
。然后尝试返回与产生错误的字符串相同的League
将返回类型更改为League
,或者不返回y
,而是返回一个有意义的字符串(甚至y.toString()
)始终发布编译错误add()
方法应该在League
类中:public void add(Team Team){/*…*/}
。另外,用java
标记你的问题。我会在一秒钟内将它们添加到league类中。我是否正确地将getLeagueAmerican和getLeagueNational设置为两个,或者我应该只设置getLeague和setLeague?Robbie在add方法的主体中添加了什么?
public class Team
{
// instance variables - replace the example below with your own
private String team;
/**
* Constructor for objects of class Team
*/
public Team(String Team)
{
team = Team;
}
public void setTeam(String Team){
this.team = Team;
}
public String getTeam(){
return team;
}
public String add(League y)
{
return y; //compiling error
}
}
public String add(League y)
{
return y; //compiling error
}