我被困在我的家庭作业的一部分上。我不确定如何将代理运送到另一个空间。
向 Agent 类添加一个usePortal
没有参数且返回类型为 void 的方法。此方法获取代理位置的门户,并检查它是否为空。
• 如果门户不为空,则让门户使用门户的传输方法传输代理。您将需要使用这个词来指代需要运输的代理。
• 如果门户为空,则不执行任何操作。
public class Agent {
private Space _location;
private String _name;
public Space get_location() {
return _location;
}
public void set_location(Space _location) {
this._location = _location;
}
public String get_name() {
return _name;
}
public void set_name(String _name) {
this._name = _name;
}
public String toString(){
return _name;
}
public String toStringLong(){
return _name + " is in " + _location;
}
public void usePortal(){
if(Portal.get_destination() == null){
}else{
}
}
}
public class Portal {
private static String _name;
private static String _direction;
private static Space _destination;
public String get_name() {
return _name;
}
public void set_name(String _name) {
Portal._name = _name;
}
public String get_direction() {
return _direction;
}
public void set_direction(String _direction) {
Portal._direction = _direction;
}
public static Space get_destination() {
return _destination;
}
public void set_destination(Space _destination) {
Portal._destination = _destination;
}
public String toString(){
return _name + " that goes " + _direction;
}
public String toStringLong(){
return _name + " that goes " + _direction + " to " + _destination;
}
public void transport(Agent student){
student.set_location(_destination);
}
}
public class Space {
Portal p = new Portal();
private String _name;
private String _description;
private Portal _portal;
public String get_name() {
return _name;
}
public void set_name(String _name) {
this._name = _name;
}
public String get_description() {
return _description;
}
public void set_description(String _description) {
this._description = _description;
}
public Portal get_portal() {
return _portal;
}
public void set_portal(Portal _portal) {
this._portal = _portal;
}
public String toString() {
return _name;
}
public String toStringLong(){
if (_portal != null){
return _name + ": " + _description + " with a " + p.toStringLong();
}
return _name + ": " + _description;
}
}