在接口内创建类和在类内创建接口有什么用
我想知道将类放置在接口中和将接口放置在类中需要什么?
class A {
interface B {}
}
interface D {
class E {}
}
I want to know what is the need of placing a class inside interface and an interface inside class?
class A {
interface B {}
}
interface D {
class E {}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是我从某个链接复制并粘贴的(我之前做过并与您分享)
也许这可以帮助你一点。
1)
在上面的接口中,您将角色类型强绑定到员工接口(employee.Role)。
2)使用接口内的静态类,您可以缩短公共编程片段:检查对象是否是接口的实例,如果是,则调用该接口的方法。看这个例子:
现在
你可以写:
This i am copying and pasting from some link (i earlier did and sharing with you)
May be that can help you a bit.
1)
In the above interface you are binding the Role type strongly to the employee interface(employee.Role).
2) With a static class inside an interface you have the possibility to shorten a common programming fragment: Checking if an object is an instance of an interface, and if so calling a method of this interface. Look at this example:
Now instead of doing this:
You can write:
组织。
在接口内指定一个类将该类直接与该接口联系起来 - 使用该接口的客户端将有权访问该类及其提供的所有功能。
我只在 Java 1.4 及更低版本中看到过接口内类的模式,以提供与接口一起使用的枚举类型 - 因为接口只能使用该类,并且该类可以受到保护,使用的客户端接口只能接受接口中定义的类的实例作为枚举值。这只是我能想到的唯一例子 - 我确信还存在其他例子,但我很少看到使用接口内部的类。
对于翻转的情况,它仍然是组织。在类内部指定接口意味着只有该类应该使用该接口。其他类和接口仍然可以使用该接口,具体取决于其访问级别,这并不是重点 - 组织记录了接口的意图 - 仅在包含该接口的类中使用。
如果它在该类之外有用,则应将其适当地移至其自己的类型。因此,这两种用途都很罕见,但它们的用途主要是通过 Java 语法直接组织代码并记录其意图。
Organization.
Specifying a class inside an interface ties that class directly to that interface - clients which use that interface will have access to that class and all the functionality that it provides.
I've seen the pattern of a class inside an interface really only in Java 1.4 and lower to provide an enumerated type to go along with the interface - since the interface could only use the class, and the class can be protected, clients that use the interface could only accept the instances of the class defined in the interface as the enumerated values. That's just the only example I can come up with - I'm sure others exist, but it's rare that I see a class inside of an interface used.
For the flipped case, it's still organization. Specifying an interface inside of a class signifies that only that class should use the interface. That other classes and interfaces can still use that interface, depending on its access level, is not the point - the organization documents the intent of the interface - to be used only in the class that contains it.
If it is useful outside of that class, it should be moved appropriately to its own type. So, both of these uses are rare, but their use is primarily to organize the code and document its intent directly via Java syntax.
如果类的功能与接口密切相关,并且我们不会在任何地方使用该类,那么我们可以在接口中定义类。
在上述情况下,车辆类可用于 VehicleService,但我们不会在其他地方使用它。
接口
实现类
If functionality of a class is closely associated with an interface and we are not going to use that class anywhere then we can define a class inside an interface.
In above case vehicle class is available for VehicleService and we are not using it anywhere else.
Interface
Implementation Class
在这种情况下,如果任何类功能与任何接口密切相关,我们就在接口内声明一个类。
有时,在接口内部提供抽象方法的默认实现也很有用。
If any class functionality is closely associate with any interface in that situation we declare a class inside an interface.
Some time it is also useful to provide default implementation of abstract method inside interface.
在接口内部创建类允许程序员对类进行限制,即只有当我们实现该接口时才能访问该内部类。
creating class inside interface allow programmer to have restrictions on class i.e., that inner class is accessible only if we implement that interface.