(day8) The Interface

Hardware Interface

USB interface , UTP(网线接口)

Interface

一流的企业订协议
二流的企业做产品

a.More abstract than abstract class.
b.Data interaction

The defination of interface

We use 'interface' to define a interface

public interface dataInteraction{

}
Multiple inheritance(Different from class)
interface ti{

}
interface t2{

}
public interface dataInteraction extends t1,t2{

}

The inner elements in a interface

a.Can't instantiate (No constructors)
b.Only can define a static final variable.(But its default is this)
c.After we define a variable,we can access its member by "interface name + method name".

We use this way to replace the enumeration(枚举)

interface Season{
  (public static final)int Spring = 1;
  (public static final)int Summer = 2;
  (public static final)int Autumn = 3;
  (public static final)int Winter = 4;
}

public class MyClass{
main(){
   Season.Spring;
  }
}

The methods

In a interface,the default of a method is an abstract method.So if anyone invokes it and it should implement it.

public interface DataInteraction{
  (public abstract)void input();
  (public abstract)void output();
}

//Use implements to realize this interface
public class MacBook implements DataInteraction{
@Override
public void input(){
  System.out.println("苹果电脑安全输入");
  }
@Override
public void output(){
    System.out.println("苹果电脑安全输出");
  }
}

public class MyClass{

}

Inner interface

But the problem is,for some class,it may won't use the abstract method in the interface .Then we need to use the inner interface.

public interface DataInteraction{
  
interface Input{
(public abstract)void input();
  }
 interface Output{
 (public abstract)void output();
  }
}

public class Printer implements DataInteraction.Input{
  //Nom this printer doesn't need to implement method Output
  public void printf(){
    System.out.println("打印机开始打印数据");
  }
} 

![JS7D(9U)DGYBJ6GRFCBNBU.png

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容