简体   繁体   English

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

[英]interface data in another interface in java

I have two interfaces Interface1 and Interface2 as below: 我有两个接口Interface1Interface2 ,如下所示:

public interface Interface1 {
  int x = 10 ;
}

public interface Interface2 {
  int y = 20
}

I want to call the Interface2 value of y in Interface1 . 我要调用的Interface2的值yInterface1 Is it possible? 可能吗? if yes can you tell me the way. 如果可以,你能告诉我方法吗。

Fields in interfaces are implicitly public static and final . 接口中的字段隐式为public staticfinal

Just reference Interface2.y . 只需参考Interface2.y

I think you're asking how to add a y to Interface1 based on y from Interface2 . 我想你问如何将添加yInterface1基于yInterface2 That might look like, 可能看起来像

public interface Interface1 {
    int x = 10 ;
    int y = Interface2.y;
}

Alternatively, you could extend Interface2 like 或者,您可以像这样扩展Interface2

public interface Interface1 extends Interface2 {
    int x = 10;
}

You can also nest interfaces like: 您还可以嵌套如下接口:

public interface A {
    int a = 0;
    interface B{
        int b = 0;
        int c = a;
    }  
}

Or simply do in this way, 或者只是这样做

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.
 }

public interface Interface1 extends interface2 { 公共接口Interface1扩展interface2 {

int x = 10;

} }

public interface Interface2{ 公共接口Interface2 {

 int y = 20

} }

public class temp { 公共课程临时{

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