Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中对象内的对象范围_Java_Constructor_Scope - Fatal编程技术网

Java中对象内的对象范围

Java中对象内的对象范围,java,constructor,scope,Java,Constructor,Scope,我现在正在学习Java,所以我希望这个问题不要太明显。我来自另一种没有垃圾收集的语言。 在另一种语言中,我有时在构造函数中创建对象,然后在析构函数中删除它们,以便在对象的整个生命周期中使用它们 作为一个简单的例子,我有一个用户和一个预订类。booking类引用了一个用户,但是如果我在booking类的构造函数中创建了该用户,那么一旦该用户离开构造函数并超出范围,它就会取消对该用户的引用。以后对booking.bookedBy用户的任何引用调用都将返回null class user { p

我现在正在学习Java,所以我希望这个问题不要太明显。我来自另一种没有垃圾收集的语言。 在另一种语言中,我有时在构造函数中创建对象,然后在析构函数中删除它们,以便在对象的整个生命周期中使用它们

作为一个简单的例子,我有一个用户和一个预订类。booking类引用了一个用户,但是如果我在booking类的构造函数中创建了该用户,那么一旦该用户离开构造函数并超出范围,它就会取消对该用户的引用。以后对booking.bookedBy用户的任何引用调用都将返回null

class user {
    public String username;
    public String displayName;
    user(Connection conn, String usernameIn){
     username = usernameIn;
         ... do DB stuff to populate attributes
    }
}

class booking {
  int bookingID;
  user bookedBy;
  ...
  booking(Connection conn, int bookedIDIn){
     bookingID = bookedIDIn;
      ...do DB stuff to populate attributes and grab bookedByUserID
      ...field value and build the BookedByUsername
     user bookedBy = new user (bookedByUsername)
  }
}

有办法解决这个问题吗?或者我需要重新考虑我的设计吗?

您在构造函数中声明了一个局部变量,它用于分配您在构造函数中创建的用户

我想你想要这个:

class booking {
  int bookingID;
  user bookedBy;
  ...
  booking(Connection conn, int bookedIDIn){
     bookingID = bookedIDIn;
     //there's no declaration of type needed here because 
     //you did that earlier when you declared your member variable up top.
     bookedBy = new user (bookedByUsername) 
  }
}

您正在构造函数中创建一个新的
bookedBy
用户变量,而不是使用类的成员变量

您可能想要更改:

user bookedBy=新用户(bookedBy用户名)

与:


bookedBy=新用户(bookedByUsername)

在booking类中,您实际上已经声明了两个名为user bookedBy的变量。一个是整个预订类的范围,另一个是构造函数的范围。要解决此问题,您需要删除构造函数中的变量声明,如下所示:

class booking {
  int bookingID;
  user bookedBy;
  ...
  booking(Connection conn, int bookedIDIn){
     bookingID = bookedIDIn;
      ...do DB stuff to populate attributes and grab bookedByUserID
      ...field value and build the BookedByUsername
    bookedBy = new user (bookedByUsername)
  }
}

是两个不同的变量

删除第二个类型声明,您的用户实例将被分配到字段级别。即:

class booking {
  int bookingID;
  user bookedBy;
  ...
  booking(Connection conn, int bookedIDIn){
     bookingID = bookedIDIn;
      ...do DB stuff to populate attributes and grab bookedByUserID
      ...field value and build the BookedByUsername
     bookedBy = new user (bookedByUsername)
  }
}

或者,如果你的口味是这样的,Python会影响这个.bookedBy=…
,这避免了歧义。好主意。我可能会用“this”,尤其是当我开始习惯这种语言的时候。非常好。显示我没有使用Java的经验。我想我已经习惯了Delphi,你在方法的顶部声明所有的局部变量。谢谢你的指针。请不要使用小写的类名。注意-我会尽量遵守。是否在任何地方都记录了标准命名约定?我喜欢不区分大小写语言的另一个原因。
user bookedBy = new user (bookedByUsername)
class booking {
  int bookingID;
  user bookedBy;
  ...
  booking(Connection conn, int bookedIDIn){
     bookingID = bookedIDIn;
      ...do DB stuff to populate attributes and grab bookedByUserID
      ...field value and build the BookedByUsername
     bookedBy = new user (bookedByUsername)
  }
}