且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么我的接口类型对象不能执行未在接口中声明的方法?

更新时间:2022-01-18 04:24:12

您必须在接口中声明要在这种情况下使用的所有方法.该界面对printSomethingElse一无所知,这就是为什么您遇到上述错误的原因.

You have to declare all methods that you want to use in that case in the interface. The interface knows nothing of printSomethingElse and that is why you're getting the above error.

接口的目的是使您可以在多个相似的已实现类之间拥有通用的功能列表.例如,List是一个接口,其中包含由不同类以各种方式实现的功能的列表",例如,LinkedList使用双向链接列表来提供ListArrayList的功能,动态扩展数组可以做到这一点.

The purpose of an Interface is so that you can have a common list of functions across multiple similar implemented classes. For example, List is an interface which contains a 'list' of functions implemented in various ways by different classes such as LinkedList which uses a Doubly Linked list to provide the functionality of List and ArrayList which uses an Dynamically expanding array to do so.