0 java oop object new-operator
我是新手(也是编程的新手),我试图寻找答案,但却找不到答案.我的任务今天到期,所以非常感谢帮助.这个问题在我身上发生了两次,但第一次我能够通过编程另一种方式忽略它,现在我已经不能了.每次我创建一个新对象时(Agent a = new Agent() <-- name of my class),它都会干扰同类型的其他对象(这与引用类型有什么关系吗?).我正在尝试创建一个DiscussionDirector class 需要两个Agent对象并在它们之间创建一个随机对话(基于随机),但我甚至无法启动,因为我还没有能够创建两个类型为Agent的对象.
这是代理的代码:
import java.util.Calendar;
import java.io.*;
import java.util.Random;
public class Agent{
private static boolean isMale;
private static String birthdate;
private static int birthyear;
private static int birthmonth;
private static int birthday;
private static String name;
private static String nativeCity;
private static String currentCity;
private static String major;
private static Random r = new Random();
public static void main(String[]args){
}
public String getCityNow(){
return this.currentCity;
}
public String getCityBorn(){
return this.nativeCity;
}
public String getName(){
return this.name;
}
public boolean getGender(){
return this.isMale;
}
public String getMajor(){
return this.major;
}
public String getBirthday(){
String birthdate = (this.birthday + "/" + this.birthmonth + "/" + this.birthyear);
return birthdate;
}
public void sayHelloTo(String name){
System.out.println(this.name + " says: Hi " + name + ", I'm " + this.name);
}
public void sayHello(){
System.out.println(this.name + " says: Hello, my name is " + this.name);
}
public void CityBorn(){
System.out.println(this.name + " says: I am from " + this.nativeCity);
}
public void howOldAreYou(){
System.out.print(this.name + " says: I am ");
if(Calendar.getInstance().get(Calendar.MONTH) < this.birthmonth){
System.out.println((Calendar.getInstance().get(Calendar.YEAR) - this.birthyear - 1) + " years old");
}
else if((Calendar.getInstance().get(Calendar.MONTH) == this.birthmonth) && (Calendar.getInstance().get(Calendar.DAY_OF_MONTH) == this.birthday)){
System.out.println((Calendar.getInstance().get(Calendar.YEAR) - this.birthyear - 1) + " years old");
}
else{
System.out.println((Calendar.getInstance().get(Calendar.YEAR) - this.birthyear) + " years old");
}
}
public void sayGender(){
System.out.println(this.name + " says: I am a ");
if(isMale == true){
System.out.print("man");
}
else{
System.out.print("woman");
}
}
public void CityNow(){
System.out.println(this.name + " says: I currently live in " + this.currentCity);
}
public void sayMajor(){
System.out.println(this.name + " says: I am studying " + this.major);
}
public void whoAreYou(){
sayHello();
CityBorn();
howOldAreYou();
sayMajor();
CityNow();
}
public Agent()throws IOException{
this.isMale = r.nextBoolean();
if(this.isMale == true){
WordList MaleNames = new WordList("MaleNames.txt");
this.name = MaleNames.getRandomWord();
}
else{
WordList FemaleNames = new WordList("FemaleNames.txt");
this.name = FemaleNames.getRandomWord();
}
this.birthyear = 1995 - r.nextInt(60); //Agents can't be too young or too old.
this.birthmonth = r.nextInt(11)+1;
if(this.birthmonth == 1|this.birthmonth == 3|this.birthmonth == 5|this.birthmonth == 7|this.birthmonth == 8|this.birthmonth == 10|this.birthmonth == 12){
this.birthday = r.nextInt(30)+1;
}
else if (this.birthmonth == 2){
this.birthday = r.nextInt(27)+1;
}
else{
this.birthday = r.nextInt(29)+1;
}
WordList Major = new WordList("Majors.txt");
this.major = Major.getRandomWord();
WordList Cities = new WordList("Cities.tx");
this.nativeCity = Cities.getRandomWord();
this.currentCity = Cities.getRandomWord();
}
public Agent generateAgent()throws IOException{
return new Agent();
}
}
Run Code Online (Sandbox Code Playgroud)
所以是的,有没有人知道为什么当我创建一个两个Agent()对象然后generateAgent() 对它们做对象时,它们总是一样的?
谢谢