当我创建类Championship
的对象时,它表明没有可访问的方法。
public class Championship
{
private ListOfDrivers driversList;
private ListOfVenues venuesList;
private RNG rand;
public static void main(String[] args) {
Championship sim = new Championship();
sim.startgame();
}
private void startgame()
{
this.driversList = new ListOfDrivers();
this.venuesList = new ListOfVenues();
this.rand = new RNG();
System.out.println("List of venues:");
venuesList.printVenues();
System.out.println("And starts....");
for(Venue venue : venuesList.getVenues()) {
System.out.println("------------------------------------------------------------");
System.out.println("Welcome one and all to " + venue.getVenueName() + "!");
// Give race penalties at the start of the race
for(Driver driver: driversList.getDrivers()) {
switch(driver.getRanking()) {
case 1: driver.setTime(0); break;
case 2: driver.setTime(3); break;
case 3: driver.setTime(5); break;
case 4: driver.setTime(7); break;
default: driver.setTime(10);
}
}
System.out.println("Drivers list!");
driversList.printDrivers();
System.out.println("Race starts!!");
for(int lap = 1; lap<=venue.getNoOfLaps(); lap++) {
System.out.println("In Lap " + lap + " now..");
for(Driver driver: driversList.getDrivers()) {
if(driver.getEligibleToRace())
{
int faultRand = rand.faultRNG();
if(faultRand == 1)
{
System.out.println(driver.getName() + " just had an unrecoverable mechanical fault!!");
System.out.println(driver.getName() + " is out of the race..");
driver.setEligibleToRace(false);
driver.setTime(99999);
continue;
}
else if(faultRand <=3)
{
System.out.println(driver.getName() + " just had a major mechanical fault!!");
driver.increaseTime(120);
}
else if(faultRand <=5)
{
System.out.println(driver.getName() + " just had a minor mechanical fault!!");
driver.increaseTime(20);
}
driver.increaseTime(venue.getAverageLapTime());
if(driver.getSkill().equals("Overtaking"))
{
if(lap%3 == 1) {
int reduc = rand.timeReductionRNG(10, 20);
driver.decreaseTime(reduc);
System.out.println(driver.getName() + " reduced time of " + reduc + " seconds");
}
} else
{
int reduc = rand.timeReductionRNG(1, 8);
driver.decreaseTime(reduc);
System.out.println(driver.getName() + " reduced time of " + reduc + " seconds");
}
}
}
Driver currentRaceLeader = driversList.lowestTime();
System.out.println("Lap " + lap + " statistics:");
System.out.println("Leader: " + currentRaceLeader.getName() + "\tTotal Time: " +
currentRaceLeader.getTime() + " seconds");
}
System.out.println("The championship race at " + venue.getVenueName() + " has concluded!");
driversList.allocateScores();
System.out.println("Results are: ");
String format = "%20s | %8s | %10s | %8s ";
System.out.println(String.format(format, "Name", "Ranking", "Race Time", "Score"));
for(Driver driver : driversList.getDrivers()) {
if(driversList.getDrivers().get(4) == driver)
break;
System.out.println(String.format(format, driver.getName(), driver.getRanking(),
driver.getTime(), driver.getScore()));
}
}
System.out.println("------------------------------");
System.out.println("Championship has concluded and the champion is:");
Driver winner = driversList.highestScore();
System.out.println(winner.getName() + " with a total of " + winner.getScore() + " points!!");
System.out.println("------------------------------");
}
}
当我单击对象时,它表明没有可访问的方法,因此我无法启动仿真。有什么我想念的吗?该代码可以正常编译,并且没有错误消息。当我创建班级冠军的新对象时,它仍然无法模拟。
答案 0 :(得分:0)
是的,除Championship
对象的默认构造函数之外的所有方法都标记为私有,因此只能在内部由类实现本身访问。
实例化Championship
对象的实例后,就会自动调用startgame()
方法。
public static void main(String[] args) {
Championship sim = new Championship();
sim.startgame();
}
如果您希望能够创建Championship
的实例而无需立即调用startgame()
方法,或者在实例化之外调用startgame()
方法-标记startgame()
{{1 }}和/或从构造函数中删除public
。
方法原型应该匹配
sim.startgame()