Java – 访问私有实例变量

我需要从以下类列表(Species.java)访问私有变量,以便在KlingonOx.java类中使用它们。

KlingonOx.java类的目的是确定大象物种的种群数量将大于克林贡牛种的数量。

这是Species.java类:

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) && (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类:

 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("ELEPHANT EXCEEDS KLINGON OX IN" + year + "YEARS"); } } } 

KlingonOx.java类给出了一个错误,“population”和“growthRate”是Species中的私有实例变量,因此无法访问。 我试图使用getPopulation和getGrowthRate方法调用来检索变量,但我不知道如何正确地执行此操作。

任何反馈都表示赞赏。

在带变量的类中:

 class Foo { private int variable; public int getVariable() { return variable; } } 

在客户端类:

 class Bar { void method() { ... Foo foo = new Foo(); int population = foo.getVariable(); ... } } 

这几乎就是一切。

您应该使用klingonox.getPopulation() – 而不是使用klingonox.population – 您的其他Species对象也是如此。

这应该是您需要进行的唯一更改才能使用getPopulation方法。

第一,

 klingonox.setPopulation(int population); population = population; klingonox.setGrowthRate(double growthRate); growthRate = growthRate; 

如果你设置的人口通过值klingonox.setPopulation(20),你为什么要尝试将人口分配给人口。 KlingonOx没有实地人口。 调用readInput()时,应该已经分配了您的填充名称和growthRate;

大象对象也是如此。

使用

 klingonox.getPopulation(); 

private访问修饰符允许我们隐藏变量,以便只能访问声明它的类。 你上课 –

 public class Species { private String name; private int population; private double growthRate; public int getPopulation(){return population;} public double growthRate(){return growthRate;} } 

这个概念也称为封装,我们使用public方法来访问和修改private变量。

您想从Sub -Class访问Super -Class的instance variable

使用带有Getter-Setter方法的super关键字。

例如:

 public class Species { private String name; private int population; private double growthRate; public int getPopulation(){ return this.population; } public double getGrowthRate(){ return this.growthRate; } public String getName(){ return this.name; } // Setters........... } public class KlingonOx extens Spices{ ....... ....... public static void main(String[] args){ int p = super.getPopulation(); ........ ........ } }