我有一个名为Functions的接口,其中没有定义任何方法。然后我有一个实现该接口的实现类,并且还有一个在实现类中定义的方法。如果我创建一个接口类型的变量并为其分配一个实现类型的新实例(其中定义了一种方法)。为什么我不能从变量访问该方法?我想我在这里错过了一些东西。我的印象是,如果为接口类型的变量分配了具有定义方法的实现类型的实例,则该变量可用于运行该方法。
请指教。提前谢谢你。
从概念上讲,你在这里做错了事情。
如果要调用"该方法",则应使用实现类型的变量,而不是接口类型的变量。
或者,如果"该方法"确实属于接口的预期功能,则应将其"向上"移动到接口。
据我了解,您的问题如下:
// Interface with no methods
public interface Functions {
}
// Implementation class with a method defined in it
public class Implementation implements Functions {
public void foo() {
System.out.println("Foo");
}
}
public class Main {
public static void main(String[] args) {
// Create a variable from the interface type and
// assign a new instance of the implementation type
Functions f = new Implementation();
// You try to call the function
f.foo(); // This is a compilation error
}
}
这是正确的行为,这是不可能的。因为编译器看到变量f
具有(静态)类型的Functions
,所以它只能看到该接口中定义的函数。编译器不知道变量是否实际包含对 Implementation
类实例的引用。
要解决此问题,您应该在接口中声明该方法
public interface Functions {
public void foo();
}
或者使变量具有实现类的类型
Implementation f = new Implementation();
您只能使用由引用类型定义的方法,而不是实例类型,例如:
AutoClosable a = new PrintWriter(...);
a.println( "something" );
在这里,AutoClosesable 是引用类型,PrintWriter 是实例类型。
此代码将给出编译器错误,因为 AutoClosable 中定义的唯一方法是 close()
。
你不能这样做,请考虑这个例子:
interface Foo {
}
和类:
class FooBar implements Foo {
public void testMethod() { }
}
class FooBarMain {
public static void main(String[] args) {
Foo foo = new FooBar();
//foo.testMethod(); this won't compile.
}
}
因为在编译时,编译器不会知道您正在创建一个new FooBar();
并且它有一个名为 testMethod()
的方法,该方法将动态确定。因此,它希望您通过接口变量访问的任何内容都应该在您的接口中可用。
你可以做的是,如果你想通过接口变量访问该方法,最好将该方法移动到接口,让客户端实现它。
如果您对此有疑问,请告诉我。