[英]interface data in another interface in java
我有两个接口Interface1
和Interface2
,如下所示:
public interface Interface1 {
int x = 10 ;
}
public interface Interface2 {
int y = 20
}
我要调用的Interface2
的值y
在Interface1
。 可能吗? 如果可以,你能告诉我方法吗。
接口中的字段隐式为public
static
和final
。
只需参考Interface2.y
。
我想你问如何将添加y
到Interface1
基于y
从Interface2
。 可能看起来像
public interface Interface1 {
int x = 10 ;
int y = Interface2.y;
}
或者,您可以像这样扩展Interface2
public interface Interface1 extends Interface2 {
int x = 10;
}
您还可以嵌套如下接口:
public interface A {
int a = 0;
interface B{
int b = 0;
int c = a;
}
}
或者只是这样做
public interface Interface2{
int Y = 20
}
public interface Interface1 extends Interface2 {
int X = 10 ;
//The member 'Y' will be available here, you can simply make use of it you don't need to call like Interface2.Y.
}
公共接口Interface1扩展interface2 {
int x = 10;
}
公共接口Interface2 {
int y = 20
}
公共课程临时{
public static void main(String[] args)
{
System.out.println(interface1.x);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.