繁体   English   中英

Java中另一个接口中的接口数据

[英]interface data in another interface in java

我有两个接口Interface1Interface2 ,如下所示:

public interface Interface1 {
  int x = 10 ;
}

public interface Interface2 {
  int y = 20
}

我要调用的Interface2的值yInterface1 可能吗? 如果可以,你能告诉我方法吗。

接口中的字段隐式为public staticfinal

只需参考Interface2.y

我想你问如何将添加yInterface1基于yInterface2 可能看起来像

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM