有人可以解释这段代码中发生了什么。无法理解简单的 oops 程序
Can somebody explain what is happening in this code. Unable to understand simple oops program
interface Foo1
{
int bar();
}
class Sprite
{
public int fubar( Foo1 foo )
{
return foo.bar();
}
public void testFoo()
{
fubar(
new Foo1()
{
public int bar()
{
return 1;
}
}
);
}
}
无法理解 fubar() 方法。有人能给我解释一下这个程序吗
方法testFoo
创建了一个实现Foo1
接口的匿名内部class的实例,并将其传递(实际上是传递指针的值)给fubar
方法。
fubar
方法在返回结果的 Foo1
实例上调用 bar
方法。
fubar
方法的结果在 testFoo
方法中被忽略。
最终效果是这段代码实际上并没有像当前显示的那样执行任何操作。
所以根据您的代码,您有一个名为 Foo1 的接口,其中包含一个名为 bar()
的抽象方法。
Sprite class 有两个方法 fubar 和 testFoo。
在您的 fubar 方法中,它获取 Foo1 对象作为参数和 returns foo.bar()
值。
在你的 testFoo 方法中,你正在调用 fubar 方法并在其中实现抽象 bar()
方法,其中 returns 1 作为整数值。
你的代码中仍然没有main方法,所以还没有输出。
这是您的代码的概述,如果您需要更多说明,请告知。
interface Foo1
{
int bar();
}
class Sprite
{
public int fubar( Foo1 foo )
{
return foo.bar();
}
public void testFoo()
{
fubar(
new Foo1()
{
public int bar()
{
return 1;
}
}
);
}
}
无法理解 fubar() 方法。有人能给我解释一下这个程序吗
方法testFoo
创建了一个实现Foo1
接口的匿名内部class的实例,并将其传递(实际上是传递指针的值)给fubar
方法。
fubar
方法在返回结果的 Foo1
实例上调用 bar
方法。
fubar
方法的结果在 testFoo
方法中被忽略。
最终效果是这段代码实际上并没有像当前显示的那样执行任何操作。
所以根据您的代码,您有一个名为 Foo1 的接口,其中包含一个名为 bar()
的抽象方法。
Sprite class 有两个方法 fubar 和 testFoo。
在您的 fubar 方法中,它获取 Foo1 对象作为参数和 returns foo.bar()
值。
在你的 testFoo 方法中,你正在调用 fubar 方法并在其中实现抽象 bar()
方法,其中 returns 1 作为整数值。
你的代码中仍然没有main方法,所以还没有输出。
这是您的代码的概述,如果您需要更多说明,请告知。