使用非静态方法而不将其引用到对象?

1 java methods object

public class Appointment{
    public TimeInterval getTime();
    {/*implementation not shown*/}

    public boolean conflictsWith(Appointment other)
    {
     return getTime().overlapsWith(other.getTime());
    }
}

public class TimeInterval{
    public boolean overlapsWith(TimeInterval interval)
    {/*implementation not shown*/}
}
Run Code Online (Sandbox Code Playgroud)

我的问题在于return getTime().overlapsWith(other.getTime())声明.getTime()不是静态方法,所以我认为它只能在引用一个对象时使用.但是根据该声明,没有提到任何对象.我理解getTime()为后续方法返回一个对象,但它本身呢?我的同学提供了一个解释,说明"当我们想要使用该conflictsWith()方法时,我们会声明一个对象,因此return语句将等同于return object.getTime().overlapsWith(other.getTime()?;"这个解释是否正确?那么这意味着当我们在方法中使用非静态方法时,不需要引用任何对象吗?

Ada*_*iss 6

由于getTime()它不是静态方法,因此在当前对象上调用它.实现等同于

public boolean conflictsWith(Appointment other)
{
  return this.getTime().overlapsWith(other.getTime());
}
Run Code Online (Sandbox Code Playgroud)

你只需要调用conflictsWith()一个Appointment对象,如下所示:

Appointment myAppt = new Appointment();
Appointment otherAppt = new Appointment();
// Set the date of each appt here, then check for conflicts
if (myAppt.conflictsWith(otherAppt)) {
  // Conflict
}
Run Code Online (Sandbox Code Playgroud)