什么是在类内部的接口和接口内创建类的使用 [英] What is the use of creating a class inside interface and interface inside class
问题描述
我想知道需要在类中放置一个类和接口里面的类吗?
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 {}
}
推荐答案
这是我从一些链接复制和粘贴(我以前和你分享)
可以帮助你。
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)
interface employee{
class Role{
public String rolename;
public int roleId;
}
Role getRole();
// other methods
}
在上面的界面中,角色类型强烈地对员工接口(employee.Role)。
2)在接口中有一个静态类,你有可能缩短一个通用的编程片段:检查一个对象是否是一个接口的实例,如果是这样调用这个接口的一个方法。看看这个例子:
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:
public interface Printable {
void print();
public static class Caller {
public static void print(Object mightBePrintable) {
if (mightBePrintable instanceof Printable) {
((Printable) mightBePrintable).print();
}
}
}
}
而不是这样:
Now instead of doing this:
void genericPrintMethod(Object obj) {
if (obj instanceof Printable) {
((Printable) obj).print();
}
}
您可以写:
void genericPrintMethod(Object obj) {
Printable.Caller.print(obj);
}
这篇关于什么是在类内部的接口和接口内创建类的使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!