2014-09-29 3 views
-1

나는 숙제의 일부를 고수하고있다. 에이전트가 다른 공간으로 어떻게 옮겨 졌는지 확실하지 않습니다.공간으로 에이전트를 이동하는 방법

매개 변수 및 void 반환 유형이없는 에이전트 클래스에 usePortal 메서드를 추가합니다. 이 메소드는 에이전트의 위치 포털을 가져 와서 null인지 확인합니다.

• 포털이 null이 아닌 경우 포털의 전송 방법을 사용하여 포털로 포워딩하도록합니다. 이 필요로하는 상담원을 나타낼 때 this라는 단어를 사용해야합니다.

• 포털이 null 인 경우 아무 것도 수행하지 마십시오.

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; 

    } 
} 
+0

'Portal' 클래스에'transport()'메소드가 있습니까? –

+0

public void transport (에이전트 학생) { student.set_location (_destination);} 에이전트 클래스에서이 메서드를 호출하는 방법을 모르겠 음 –

답변

1

그것은이 될 것이다 :

public void usePortal(){ 

    if(!(_location.get_portal() == null)){ 
     _location.get_portal().transport(this); 
    } 
} 

에이전트의 공간 변수 (_location가) 널 포털이있는 경우 u는 확인하고 해당 포털을 얻을 당신이 무엇이든에이 전송 meathod의 사용하기 때문에 현재 인스턴스화 된 에이전트는 (this)

+0

에이전트를 이동하려고하면 첫 번째 공간을 건너 뛰고 에이전트의 대상 만 가져옵니다. 그것 아래 하나 –

0

목록 및 배열은 Java 기반입니다. 첫 번째 요소는 0이 아니고 1입니다.