【问题标题】:Trouble with methods from libraries in Java?Java 库中的方法有问题吗?
【发布时间】:2014-11-30 11:48:33
【问题描述】:

所以我的类扩展了 Actor,我想使用 getLocation() 方法,它需要来自 Actor 的位置,但它会将我发送到需要 Point 的 java.awt.Point 的 getLocation()

public class Myplayer extends Actor
Location myLocation = player.getLocation();

错误:

incompatible types: java.awt.Point cannot be converted to ch.aplu.jgamegrid.Location

这是两个库的链接:

演员:http://www.aplu.ch/classdoc/jgamegrid/index.html

获取位置

public Location getLocation()

Returns the current location (horizontal and vertical coordinates).

Returns:
    a clone of the current location (cell indices)

点:http://docs.oracle.com/javase/7/docs/api/java/awt/Point.html#getLocation%28%29

获取位置

public Point getLocation()

Returns the location of this point. This method is included for completeness, 
to parallel the getLocation method of Component.

Returns:
a copy of this point, at the same location

如何在不使用 Point 的情况下解决此问题?

编辑:

import ch.aplu.jgamegrid.*;
import java.awt.Color;
import java.util.*;

public class MyPlayer extends Actor

private Players player

public Location myLocation() {
    return player.getLocation();

【问题讨论】:

  • 请在myplayer中显示getLocation的定义,以及player变量的定义。最好发布你的完整课程,只删除你认为不相关的方法。
  • 同时发布你在 myplayer 类中导入的内容
  • 最好编辑您的问题,因为它更具可读性。你也应该发布你的相关课程。
  • 那么,既然您已经编辑了,我们可以看到播放器不是MyPlayer 类型。所以MyPlayer 是否扩展Actor 并不重要。如果你真的需要进一步的帮助,你必须解释你真正的问题,并发布你的完整代码,在上下文中解释出了什么问题。

标签: java methods libraries incompatibility incompatibletypeerror


【解决方案1】:

Player类继承了Actor的所有方法,因此我发现getLocation()只能自己使用,不需要调用对象引用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-24
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    • 2012-07-08
    相关资源
    最近更新 更多