Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用非静态方法而不引用对象?_Java_Object_Methods - Fatal编程技术网

Java 使用非静态方法而不引用对象?

Java 使用非静态方法而不引用对象?,java,object,methods,Java,Object,Methods,我的问题在于return getTime().overlapsWith(other.getTime())语句。getTime()不是静态方法,所以我认为它只能在引用对象时使用。但从那句话来看,没有提到任何对象。我知道getTime()为后续的方法返回一个对象,但是它本身呢?我的同学解释说:“当我们想使用conflictsWith()方法时,我们会声明一个对象,因此return语句将相当于return object.getTime().overlapsWith(other.getTime());”

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

当调用非静态方法时,对象
this
是隐含的。在运行时
this
对象指的是正在操作的对象的实例。在您的情况下,它指的是调用外部方法的对象。

当调用非静态方法时,对象
this
是隐含的。在运行时
this
对象指的是正在操作的对象的实例。在您的情况下,它指的是调用外部方法的对象。

因为
getTime()
不是静态方法,它是在当前对象上调用的

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*/}
}
您只能在
约会
对象上调用
conflictsWith()
,如下所示:

public boolean conflictsWith(Appointment other)
{
  return this.getTime().overlapsWith(other.getTime());
}
由于
getTime()
不是静态方法,因此在当前对象上调用它

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*/}
}
您只能在
约会
对象上调用
conflictsWith()
,如下所示:

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

在约会中,getTime()与此等效。getTime()

你的冲突可以这样改写:

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

在约会中,getTime()与此等效。getTime()

你的冲突可以这样改写:

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

谢谢jeremy,但我还有更多的问题:1.这是否意味着我在方法中使用方法时可能会忽略使用对象?2.“this”指的是什么?此类的隐藏对象?如果这些都很简单,我很抱歉。
this
是一个关键字,它充当对象引用,并且引用了您创建的对象实例e in.谢谢jeremy,但我还有更多的问题:1.这是否意味着我在方法中使用方法时可以忽略使用对象?2.“this”指的是什么?这个类的隐藏对象?如果这些都很简单,我很抱歉。
this
是一个关键字,用作对象引用,并引用对象实例y你在。