[英]Java - access private instance variables
我需要從以下類列表(Species.java)中訪問私有變量,以便在KlingonOx.java類中使用它們。
KlingonOx.java類的目的是確定多少年后大象物種的數量將大於Klingon Ox物種的數量。
這是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)
{
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類:
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類給我的錯誤是“種群”和“ 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.population
,應該使用klingonox.getPopulation()
-其他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
變量。
-您要訪問的instance variable
中的Super
從-Class Sub
級轎車。
-通過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();
........
........
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.