JAVA如何在同一类的非静态方法中使用新返回对象的方面
我正在做一个赋值,在这里你创建了一个对象“代理”,大约有六个属性是通过set和get方法中的随机生成器随机分配的 我遇到的一个大问题是,每当我试图构造一个包含Agent1的方法时,总是会出现“Agent1无法解决”错误JAVA如何在同一类的非静态方法中使用新返回对象的方面,java,Java,我正在做一个赋值,在这里你创建了一个对象“代理”,大约有六个属性是通过set和get方法中的随机生成器随机分配的 我遇到的一个大问题是,每当我试图构造一个包含Agent1的方法时,总是会出现“Agent1无法解决”错误 public class Agent { // properties of Agents-to-be, set to private to keep things safe private String Gender; private String Birth
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;
}
下一种方法就是我遇到麻烦的方法。无论我如何更改它,我总是得到错误消息“Agent1无法解析”,我认为这只是意味着对象代理1停止存在于我的静态方法之外。此方法仅用于分配的这一部分,在后面的部分中,我将不得不调用generateAgent方法来创建两个代理实例,以便彼此对话。否则,我已经尝试将这两种方法结合起来,而且似乎是这样的,所以我只需要一种方法通过它们来保存我的对象
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();
}
下面是set和get方法各自的一个示例,以防您认为它们是导致我的问题的原因。他们都非常相似
// 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;
}
“sayX”都依赖于前两种方法的输出,当我使用与set和get相同的方法时,它们就会工作
public void howOldAreYou()
{
System.out.println( this.Name + " says: I am " + this.Age + " Years old ") ;
}
在main方法中有这样的代码
generateAgent();
Agent1.WhoAreYou();
但是,您的方法generateAgent()
正在返回对类型为Agent
的对象实例的引用,您已将其命名为Agent1
。你的名字是不相关的,对象类型和状态很重要-但是,为了符合你的命名法,试试这个
Agent Agent1 = generateAgent(); // this Agent Agent1 refers to the same Agent
// from generateAgent (which was also named Agent 1).
Agent1.WhoAreYou();
我收到同样的错误消息,它说“代理1从未被读取”。我也尝试过这样做,并删除generateAgent()中的新代理,但仍然有相同的错误消息,只是这次“Agent1无法解析”也来自generateAgent()行。为什么要删除generateAgent()中的新代理?你的问题是什么?我想你的建议会创建一个新的代理,所以没有必要。我错了。我的问题是如何让我的“WhoAreYou”方法识别我在“generateAgent”中创建的Agent1对象将引用分配给范围中的变量。这是我不知道如何做的。在我的generateAgent()方法之外,没有任何东西可以识别Agent1,并且赋值说明此方法应该返回代理类的对象