java 私有变量访问_Java - 访问私有实例变量
我需要從以下類(lèi)列表(Species.java)訪(fǎng)問(wèn)私有變量,以便在KlingonOx.java類(lèi)中使用它們。
KlingonOx.java類(lèi)的目的是確定大象物種的種群數(shù)量將大于克林貢牛種的數(shù)量。
這是Species.java類(lèi):
import java.util.Scanner;
public class Species
{
private String name;
private int population;
private double growthRate;
public void readInput()
{
Scanner keyboard = new Scanner(System.in);
System.out.println("What is the species' name?");
name = keyboard.nextLine();
System.out.println("What is the population of the species?");
population = keyboard.nextInt();
while(population < 0)
{
System.out.println("Population cannot be negative.");
System.out.println("Reenter population:");
population = keyboard.nextInt();
}
System.out.println("Enter growth rate (% increase per year):");
growthRate = keyboard.nextDouble();
}
public void writeOutput()
{
System.out.println("Name = " + name);
System.out.println("Population = " + population);
System.out.println("Growth rate = " + growthRate + "%");
}
public int predictPopulation(int years)
{
int result = 0;
double populationAmount = population;
int count = years;
while( (count>0) && (populationAmount>0) )
{
populationAmount = (populationAmount + (growthRate/100) * populationAmount);
count --;
}
if (populationAmount > 0)
result = (int)populationAmount;
return result;
}
public Species(String name)
{
name = name;
population = 0;
growthRate = 0.0;
}
public Species(int population)
{
name = "";
if (population > 0)
population = population;
else
{
System.out.println("ERROR: using a negative" + "population.");
System.exit(0);
}
growthRate = 0.0;
}
public Species(double growthRate)
{
name = "";
population = 0;
growthRate = growthRate;
}
public Species(String name, int population, double growthRate)
{
name = name;
if (population > 0)
population = population;
else
{
System.out.println("ERROR: using a negative" + "population.");
System.exit(0);
}
growthRate = growthRate;
}
public Species()
{
name = "";
population = 0;
growthRate = 0;
}
public void setSpecies(String newName, int newPopulation, double newGrowthRate)
{
name = newName;
if (newPopulation >= 0)
population = newPopulation;
else
{
System.out.println("ERROR: using a negative " + "population.");
System.exit(0);
}
growthRate = newGrowthRate;
}
public void setName(String name)
{
name = name;
}
public void setPopulation(int population)
{
if (population > 0)
population = population;
else
{
System.out.println("ERROR: using a negative" + "population.");
System.exit(0);
}
}
public void setGrowthRate(double growthRate)
{
growthRate = growthRate;
}
public String getName()
{
return name;
}
public int getPopulation()
{
return population;
}
public double getGrowthRate()
{
return growthRate;
}
public boolean equals(Species otherObject)
{
return (name.equalsIgnoreCase(otherObject.name)) &&
(population == otherObject.population) &&
(growthRate == otherObject.growthRate);
}
}
這是KlingonOx.java類(lèi):
import java.util.Scanner;
public class KlingonOx extends Species
{
public static void main(String[] args)
{
new KlingonOx().run();
}
public void run()
{
Species klingonox = new Species();
Species elephant = new Species();
System.out.println("Please enter data on the species Klingon Ox.");
klingonox.readInput();
klingonox.writeOutput();
klingonox.setPopulation(int population);
population = population;
klingonox.setGrowthRate(double growthRate);
growthRate = growthRate;
System.out.println("Please enter data on the species Elephant.");
elephant.readInput();
elephant.writeOutput();
elephant.setPopulation(population);
population = population;
elephant.setGrowthRate(growthRate);
growthRate = growthRate;
int year = 0;
if(klingonox.population < elephant.population)
{
while(klingonox.population < elephant.population)
{
klingonox.population = (int)(klingonox.population + (klingonox.population * (klingonox.growthRate/100) ) );
elephant.population=(int)(elephant.population + (elephant.population * (elephant.growthRate/100) ) );
year++;
}
System.out.println("KLINGON OX EXCEEDS ELEPHANT IN" + year + "YEARS");
}
else
{
while(klingonox.population > elephant.population)
{
klingonox.population=(int)(klingonox.population+(klingonox.population*(klingonox.growthRate/100)));
elephant.population=(int)(elephant.population+(elephant.population*(elephant.growthRate/100)));
year++;
}
System.out.println("ELEPHANT EXCEEDS KLINGON OX IN" + year + "YEARS");
}
}
}
KlingonOx.java類(lèi)給出了一個(gè)錯(cuò)誤,即“population”和“growthRate”是Species中的私有實(shí)例變量,因此無(wú)法訪(fǎng)問(wèn)。我曾嘗試使用getPopulation和getGrowthRate方法調(diào)用來(lái)檢索變量,但我不確定如何正確地執(zhí)行此操作。
感謝任何反饋。
總結(jié)
以上是生活随笔為你收集整理的java 私有变量访问_Java - 访问私有实例变量的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 麻省理工18年春软件构造课程阅读10“抽
- 下一篇: Kali渗透(二)之被动信息收集