简体   繁体   中英

JAVA How to use aspects of newly returned object in a non-static method of same class

Im working on an assignment where you create an object " agent" , with about half a dozen attributes that are randomly assigned through random generators in set and get methods.

the big problem I am having as that I keep getting an "Agent1 cannot be resolved" error any time I try to construct a method involving Agent1

   public class Agent 
{
// properties of Agents-to-be, set to private to keep things safe
   private String Gender;
   private String Birthday;
   private String Name;
   private String CityBorn;
   private String CityNow;
   private String Major;
   private String filename;
   private String Age;
// sets 3 variables that will be used throughout the program   
   final String M = "Male" ; 
   final String F = "Female" ; 
   final Random Schrodinger= new Random();  




public static void main(String[] args) 
   {    
    generateAgent();

   Agent1.WhoAreYou();
   }   

// this Method invokes all the set methods(not shown) that set the value of the above 
// private strings. 
   public static Agent generateAgent() {
       Agent Agent1 = new Agent();
       Agent1.setGender(); 
       Agent1.setBirthday();
       Agent1.setName();
       Agent1.setCityBorn();
       Agent1.setCityNow();
       Agent1.setMajor();


       return Agent1;
   }

This next method is the one I am having trouble with. No Matter how I change it, I always get the error message of "Agent1 Cannot be resolved" , which I think just means that the object Agent 1 stops existing outside of my static method. This method is only used for this part of the assignment, in a later part I will have to call on the generateAgent method to create two instances of agents to talk to each other. Otherwise, I've tried combining both of the methods and it seems to work that way, so I just need a way to conserve me object through them.

public void WhoAreYou()
   {

       Agent1.getGender();
       Agent1.getBirthday();
       Agent1.getName();
       Agent1.getCityBorn();
       Agent1.getCityNow();
       Agent1.getMajor();
       Agent1.getMajor();

       Agent1.sayHello();
       Agent1.sayCityBorn();
       Agent1.sayGender();
       Agent1.sayCityNow();
       Agent1.sayMajor();
} 

Here is an example of one of each of the set and get methods, respecively, in case you think that they are the reason for my problem. they are all very similar

// The set Methods. These methods either use a random number generator to create random values for our
// agents properties, or use the WordList class to do so.  

  public void setGender() 
  {    
      // next.int(n) gives a random number between 0 and n-1, so nextInt(2)'s range is 0-1
      int i = Schrodinger.nextInt(2);
      // "if/else" statement, shortened for readability  
      this.Gender = ((i==1)?M:F); 
  }   
/** the "get" methods.These are all one line long and are just used to supply inputs for later code 
 *  All of them return "this.('methodname - get')." The this. prefix serves to insure each method is
 *  only returning a characteristic of one particular object
 **/

  public String getGender()
  {
     return this.Gender;
  }

The "sayX" all rely on the outputs of the previous two methods, and work when i have them in the same method as both set and get

 public void howOldAreYou()
  {
      System.out.println( this.Name + " says: I am " + this.Age + " Years old ") ;
  }   

In your main method you have code like this

 generateAgent();
 Agent1.WhoAreYou();

However, your method generateAgent() is returning a reference to an object instance of type Agent which you have named Agent1 . Your name is irrelevant, the object type and state matter - however, in keeping with your nomenclature try this

Agent Agent1 = generateAgent(); // this Agent Agent1 refers to the same Agent
                                // from generateAgent (which was also named Agent 1).
Agent1.WhoAreYou();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM