没有实施关键字,我们可以使用接口吗?

Nar*_*aki 0 java interface implements

我们可以使用变量和接口方法而无需使用关键字"Implements".

注意:接口和类在同一个包中.

提前致谢..!!!

san*_*hat 7

  • public static final默认情况下,接口的所有变量都可以直接使用它们
  • 您可以通过匿名类实现接口(不使用implements关键字)

    public static void main(String[] args) throws Exception{
       System.out.println(I.s); // accessing Interface I's variable
       I i = new I() {
    
        @Override
        public int getS() {
            return 10;
        }
       };
       System.out.println(i.getS()); // accessing I's method
    }
    
    
    interface I {
    
       String s = "test";
    
       int getS();
    }
    
    Run Code Online (Sandbox Code Playgroud)