Java - 访问私有实例变量 [英] Java - access private instance variables

查看:110
本文介绍了Java - 访问私有实例变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to access the private variables from the following class listing (Species.java) in order to use them in the KlingonOx.java class.

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

The purpose of the KlingonOx.java class is to determine after how many years the population of the Elephant species will be larger than the population of the Klingon Ox species.

这是物种。 java class:

Here is the Species.java class:

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类:

Here is the KlingonOx.java class:

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类给出我认为population和growthRate是Species中的私有实例变量的错误,因此无法访问。我已经尝试使用getPopulation和getGrowthRate方法调用来检索变量,但我不确定如何正确地执行此操作。

The KlingonOx.java class gives me the error that "population" and "growthRate" are private instance variables in Species and therefore cannot be accessed. I've tried to use the getPopulation and getGrowthRate method calls to retrieve the variables but I'm not sure how to do so correctly.

任何反馈都表示赞赏。

推荐答案

在带变量的课程中:

class Foo {
  private int variable;

  public int getVariable() { return variable; }
}

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

这几乎是一切。

这篇关于Java - 访问私有实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆