Java基础--- 接口 Interface
Interface定义
- 因为java中没有多继承(一个子类只能有一个父类),所以提出了接口的概念
- 接口的本质——接口是一种特殊的抽象类,这种抽象类里面只包含常量和方法的定义,而没有变量和方法的实现.
- Java中接口可以多继承, 也就是一个接口同时继承多个接口
关于Interfece的成员变量和成员方法
- Interfce的属性(即成员变量)必须是声明成“public static final 类型,即使默认也是成“public static final”. (因为是static final类型,所以在声明时必须要初始化赋值)
- Interface中的方法全部为抽象方法,但是不需要使用abstract修饰,并且默认权限也是public
- Interface中没有构造方法
关于Interface的继承
- 一个类可以实现多个接口,接口里面的成员变量不专属于某个对象,都是静态的成员变量,是属于整个类的,因此一个类去实现多个接口也是无所谓的,不会存在对象之间互相冲突的问题。实现多个接口,也就实现了多重继承,而且又避免了多重继承容易出现问题的地方(菱形继承),这就是用接口实现多重继承的好处.
- 当一个类实现多个接口并且需要使用接口的方法时,需要进行类型转换
- 下图中Teacher同时实现people interface和id interface,
jobType是people interface的方法,idNumber是ID interface的方法
- Interface可以继承interface, 继承时使用extends 关键字。比如interfaceB继承interfaceA,而实现interfaceB的类必须要implement A和B的所有方法
- 注意Java中接口可以多继承, 但是类不能多继承
interface的default方法和static方法
- 接口中可以定义static方法,可通过接口名称.方法名()调用,实现类不能继承static方法;
- 接口中可以定义default方法,default修饰的方法有方法体,表示这个方法的默认实现,子类可以直接调用,可以选择重写或者不重写;
package com.skd.interfacemethod;
/**
* @Description
* @Author virgosnail
* @Date 2018/12/12
*/
public interface Human {
/**
* 必须被重写
*/
void oldMethod();
/**
* 实现类可以选择重写,也可以不重写
*/
default void hi(){
System.out.println("interface default method");
}
/**
* 不能被实现类重写
*/
static void hello(){
System.out.println("interface static method");
}
}
两个interface有名字相同的方法
signature完全一样
- 如果signature完全一样,则只有一个implementation. 可以正常编译运行
public class InterfaceTest {
interface Gift { void present(); }
interface Guest { void present(); }
interface Presentable extends Gift, Guest { }
public static void main(String[] args) {
Presentable johnny = new Presentable() {
@Override public void present() {
System.out.println("Heeeereee's Johnny!!!");
}
};
johnny.present(); // "Heeeereee's Johnny!!!"
((Gift) johnny).present(); // "Heeeereee's Johnny!!!"
((Guest) johnny).present(); // "Heeeereee's Johnny!!!"
Gift johnnyAsGift = (Gift) johnny;
johnnyAsGift.present(); // "Heeeereee's Johnny!!!"
Guest johnnyAsGuest = (Guest) johnny;
johnnyAsGuest.present(); // "Heeeereee's Johnny!!!"
}
}
return type不一样
- 不能编译运行, 会报错
public interface A {
void test ();
}
public interface B {
int test ();
}
public class testIml implements A, B {
@Override
public void test () {
System.out.println("hello") //会报错
}
}
)
其中一个或者都为default方法
- 则必须重写
public interface I1
{
default String getGreeting() {
return "Good Morning!";
}
}
public interface I2
{
default String getGreeting() {
return "Good Afternoon!";
}
}
//会报错, 因为没有重写getGreeting
public class C1 implements I1, I2
{
public static void main(String[] args)
{
System.out.println(new C1().getGreeting());
}
}
所以需要重写getGreeting
public class C1 implements I1, I2 // this will compile, bacause we have overridden getGreeting()
{
public static void main(String[] args)
{
System.out.println(new C1().getGreeting());
}
@Override public String getGreeting()
{
return "Good Evening!";
}
}
Nested Interface or Inner Interface
- 在一个接口里定义另外一个接口, 或者在一个类中定义一个接口
- 主要作用是解决interface的namspace问题( 只能通过另外一个类或者一个接口调用,解决了充满问题)
- 比如Map中的·Entry interface就是nested interface, 通过Map.Entry得到