(JAVA) 正在初始化对象 - 在一行中调用方法
(JAVA) Initializing object - calling method in one line
public perform Ulti (Location origin, int number) {
Map i = new Map(origin, i.getDestination(), number);
}
Map的构造函数是(Location n, Location m, int k);
我的问题是,我不知道目的地,但是 Map 中有一个名为 getDestination() 的方法。我知道来源输入第一个参数,如何使用新创建对象的方法?
注意:地图对象不能为空; //所以我不确定我可以使用什么其他占位符
如果你有一个保存目的地的变量,那么你必须先设置它
例如:
map i = new map();
i.setDistination("----");
String distination = i.getDistination();
但在您的示例中,您可以简单地键入目的地或从其他对象获取目的地。
在创建对象之前不能调用它的方法。如果可以的话,传递对象本身已经具有的值是没有意义的。 'is' 目的地是什么?如果您有其他方式访问它,只需向 Map 添加一个空的构造函数。
像这样
public class Map{
public Map(){
}
//Rest of code
}
现在您可以创建没有目的地的对象:
地图 i = 新地图();
然后设置目的地。
您可以在 Map
class -
中执行以下操作
class Map{
Location origin;
Location destination;
int number;
//create a no argument constructor so that your Map can
// be created without any constructor whenever required
Map(){
}
//create a constructor with two argument
Map(Location origin, int number){
this.origin = origin;
this.number = number;
}
//getter and setter methods.
}
现在您可以像这样创建 Map
instance/object -
Map i = new Map(origin, number);
Location m = // some code for generating Location as destination
i.setDestination(m);
public perform Ulti (Location origin, int number) {
Map i = new Map(origin, i.getDestination(), number);
}
Map的构造函数是(Location n, Location m, int k);
我的问题是,我不知道目的地,但是 Map 中有一个名为 getDestination() 的方法。我知道来源输入第一个参数,如何使用新创建对象的方法?
注意:地图对象不能为空; //所以我不确定我可以使用什么其他占位符
如果你有一个保存目的地的变量,那么你必须先设置它
例如:
map i = new map();
i.setDistination("----");
String distination = i.getDistination();
但在您的示例中,您可以简单地键入目的地或从其他对象获取目的地。
在创建对象之前不能调用它的方法。如果可以的话,传递对象本身已经具有的值是没有意义的。 'is' 目的地是什么?如果您有其他方式访问它,只需向 Map 添加一个空的构造函数。 像这样
public class Map{
public Map(){
}
//Rest of code
}
现在您可以创建没有目的地的对象: 地图 i = 新地图(); 然后设置目的地。
您可以在 Map
class -
class Map{
Location origin;
Location destination;
int number;
//create a no argument constructor so that your Map can
// be created without any constructor whenever required
Map(){
}
//create a constructor with two argument
Map(Location origin, int number){
this.origin = origin;
this.number = number;
}
//getter and setter methods.
}
现在您可以像这样创建 Map
instance/object -
Map i = new Map(origin, number);
Location m = // some code for generating Location as destination
i.setDestination(m);