我是java的新手,并且在嵌套类方面遇到了一些困难。我在main方法(comp1)中创建了我的内部类的实例但是当我尝试在另一个打印输出(doCompOutput)的方法中引用它时,我得到一个错误,它找不到符号comp1或方法doCompOutput
public class Team{
private String teamName;
private String name1, name2, name3, name4;
public Team(String _teamName, String _name1, String _name2, String _name3, String _name4){
setTeam(_teamName, _name1, _name2, _name3, _name4);
}
public void setTeam(String _teamName, String _name1, String _name2, String _name3, String _name4){
teamName = _teamName;
name1 = _name1;
name2 = _name2;
name3 = _name3;
name4 = _name4;
}
***TEAM GET METHODS REMOVED TO SHORTEN CODE***
public static void doTeamOutput(){
System.out.println(team1.getTeamName() + team1.getName1() + team1.getName2() + team1.getName3() + team1.getName4());
}
public static void main(String[] args){
Team team1 = new Team("Spartans", "chase", "mark", "matt", "jake");
doTeamOutput();
Team team2 = new Team("Warriors", "john", "paul", "george", "ringo");
doTeamOutput();
Competition comp1 = new Competition("Alpha","Spartans","Warriors",2013);
doCompOutput();
}
static class Competition {
String compName;
String winner;
String runner;
int year;
public Competition(String _compName, String _winner, String _runner, int _year){
setComp(_compName, _winner, _runner, _year); //set
}
public void setComp(String _compName, String _winner, String _runner, int _year){
compName = _compName;
winner = _winner;
runner = _runner;
year = _year;
}
public static void doCompOutput(){
System.out.println(comp1.getName());
}
public String getName()
return compName;
***COMP GET METHODS MOVED TO SHORTED CODE***
}
}
答案 0 :(得分:0)
你的方法doCompOutput()试图在编译器不知道的对象上调用方法getName()。
public static void doCompOutput(){
System.out.println(this.getName());
}
使用该代码,该方法将输出您调用方法的对象的compName
。
您的代码有很多错误。您应该查看一些关于the basics of Java
的教程