【发布时间】:2014-11-21 20:44:46
【问题描述】:
public class Agent {
private Space _location;
private String _name;
public void setLocation(Space space){
_location = space;
}
public void usePortal(){
if(_location.getPortal() != null){
Portal.transport(Agent.this);
}
}
}
java.lang.Error:未解决的编译问题: 无法从类型 Portal 对非静态方法 transport(Agent) 进行静态引用
以上是它给我的错误。我有一个公共类 Space,其中包含一个 Portal 类型的成员变量和一个 getPortal() getter。看起来像:
public class Space {
private String _name;
private String _description;
private Portal _portal;
public Portal getPortal(){
return _portal;
}
}
在我的公共门户类中,我有一个带有代理参数的传输方法:
public class Portal {
private String _name;
private String _direction;
private Space _destination;
public Space getDestination(){
return _destination;
}
public void transport(Agent str){
str.setLocation(getDestination());
}
}
我的主要问题是使用 usePortal() 方法,Space 和 Portal 类功能齐全。我不知道如何在 Agent 类中调用 Agent 实例上的方法。
【问题讨论】:
-
我不明白你的标题。您正在从实例类
Agent调用实例方法transport。只需创建Portal的实例,然后调用portalInstance.transport(this);? -
先学习
static & this keyword!!
标签: java methods instance-variables