【问题标题】:Implementing Interface - Method Signature Error实现接口 - 方法签名错误
【发布时间】:2015-09-18 05:54:00
【问题描述】:

我有这个界面:

public interface IAppointmentDAO extends IAppointmentDate {
    void close();

    void deleteAppointment(long id);

    Appointment getAppointment(long id);

    List<Appointment> getGroomerAppointmentsByDate(int groomerId, Calendar date);

    List<Appointment> getGroomerAppointmentsByDate(int groomerId,
            Calendar date, int appId);

    List<Event> getGroomerEventsByDate(int groomerId, Calendar date);

    boolean isEmptyDate(int day, int month, int year, int groomerId);

我有一个实现接口的类。它有这个方法:

   @Override
        public boolean isEmptyDate(int day, int month, int year, int groomerId) {
            Calendar today = Calendar.getInstance();
            today.set(year, month, day, 0, 0, 0);
            today.set(Calendar.MILLISECOND, 0);

            Calendar tomorrow = Calendar.getInstance();
            tomorrow.set(year, month, day + 1, 0, 0, 0);
            tomorrow.set(Calendar.MILLISECOND, 0);

            String[] insertArgs = new String[] {
                    String.valueOf(today.getTimeInMillis()),
                    String.valueOf(tomorrow.getTimeInMillis()),
                    String.valueOf(groomerId) };
            Cursor cursor = database.rawQuery(
                    SQLiteConstants.COUNT_APPOINTMENTS_FOR_DATE, insertArgs);
            try {
                if (cursor.moveToFirst())
                    return cursor.getInt(0) > 0 ? false : true;
                return true;
            } finally {
                cursor.close();
            }
        }

我们可以看到方法的签名和界面中的底部签名是一样的。但是,我的类有一个错误,说它没有实现接口中的所有方法。我做了快速修复以使类实现接口中的方法,它为类提供了这个方法:

@Override
    public boolean isEmptyDate(int day, int month, int year, long id) {
        return false;
    }

为什么Android Studio 认为我的接口isEmptyDate 方法采用id 类型的long 参数?

【问题讨论】:

  • 尝试清理缓存(文件 - 使缓存无效/重新启动)并重建项目
  • 我觉得很奇怪。
  • 如果两个版本的方法名称相同,则需要同时实现

标签: java android android-studio interface


【解决方案1】:

我猜想长id参数的方法来自IAppointmentDate接口。

【讨论】:

  • 好主意,但这次不是这样。在我的界面中,boolean isEmptyDate(int day, int month, int year, int groomerId); 是灰色的,表示它从未使用过。
  • 这可能是正确的(从未使用过),因为没有人这么称呼它。你能发布这两个接口的完整代码 - IAppointmentDAO 和 IAppointmentDate 吗?
  • 我看到它在 IAppointmentDate 界面中。傻我。谢谢!
【解决方案2】:

似乎是方法的问题。 1. 只需用 false 初始化一个局部布尔变量。 2. 将 cursor.getInt(0) 分配给上述变量。 3. finally 之后返回这个变量。 4. 应该能解决你的问题

【讨论】:

  • 这甚至与问题无关。
猜你喜欢
  • 1970-01-01
  • 2011-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-21
  • 1970-01-01
相关资源
最近更新 更多