详述接口
一、接口的定义
抽象方法:没有方法体的方法。如果一个类中含有抽象方法,则该类被称为抽象类。如果一个抽象类中的所有方法都是抽象方法,则可将该抽象类定义为接口。
public interface IMammal {
int age = 10;
void Move() ;
void Breath() ;
}
二、接口的实现
1、接口中的抽象方法必须通过类来实现。实现接口中抽象方法的类称为接口实现类。
public interface IMammal {
int age = 10;
void Move() ;
void Breath() ;
}
public class Whale implements IMammal{
public void Move() {
System.out.println("靠鳍游动......");
}
public void Breath() {
System.out.println("呼吸.......");
}
}
因为Whale实现了接口IMammal的抽象方法,所以Whale类既是接口IMammal的实现类。
2、一个类可以实现一个或多个接口
public interface IFather {
void eat();
void breath();
}
public interface IMother {
void speak();
}
public interface ISon extends IFather,IMother{
}
public class Test implements IFather,IMother{
public static void main(String[] args) {
Test test = new Test();
test.eat();
test.breath();
test.speak();
}
public void eat() {
System.out.println("eating......");
}
public void breath() {
System.out.println("breathing......");
}
public void speak() {
System.out.println("speaking......");
}
}
3、如果一个类没有实现接口中所有抽象方法,则该类必须为抽象类。
public interface IMammal {
int age = 10;
void Move() ;
void Breath() ;
}
public abstract class Whale implements IMammal{
public void Move() {
System.out.println("靠鳍游动......");
}
}
因为Whale并没有实现接口IMammal的所所有抽象方法,所以Whale类必须是抽象类。
4、接口重复抽象方法,实现类实现一个;接口中重复常量名,实现类必须指定调用哪个接口的常量。
public interface IFather {
int AGE = 10;
void eat();
void speak(boolean x);
}
public interface IMother {
String AGE = "20";
void speak(boolean x);
void breath();
}
public class Test implements IFather,IMother{
public static void main(String[] args) {
Test test = new Test();
test.eat();
test.breath();
test.speak(true);
// System.out.println(AGE);
System.out.println(IFather.AGE);
System.out.println(IMother.AGE);
}
public void eat() {
System.out.println("eating......");
}
public void speak(boolean x) {
if(x) {
System.out.println("妈妈,我爱你!");
}else {
System.out.println("爸爸,我爱你!");
}
}
public void breath() {
System.out.println("breathing......");
}
}
接口IFather和接口IMother中有一个重复的抽象方法speak(boolean x),实现类实现一个,这是一种多态的形式。接口中有重复的常量名,实现类必须指明调用哪个接口的常量,否则将会报错。
三、接口的特点
1、接口中没有默认构造方法,并且也不能定义构造方法。
如果定义构造方法,程序将会报错。
2、接口一般以I开头
为了区别于类接口名一般以I开头,例如:IMammal、IFather、IMother等
3、接口中的变量是常量即默认由public、static、final修饰
public interface IMammal {
int AGE = 10;
void Move() ;
void Breath() ;
}
public class Whale implements IMammal{
public void Move() {
System.out.println("靠鳍游动......");
}
public void Breath() {
System.out.println("呼吸.......");
}
}
public class Test {
public static void main(String[] args) {
Whale whale = new Whale();
whale.Move();
IMammal mammal = new Whale();
mammal.Move();
System.out.println(IMammal.AGE);
}
}
变量AGE能用类名调用,证明它默认被static修饰
但你如果想对变量AGE赋值,程序会报错“The final field IMammal.AGE cannot be assigned”,证明变量AGE默认被final修饰
4、接口中的抽象方法默认public、abstract
public interface IMammal {
int AGE = 10;
void Move() ;
void Breath() ;
}
例如接口IMammal中的Move()方法和Breath()方法就把public、abstract省去不写
5、在JDK8及其以后版本的JDK中,接口内可以定义静态方法(static修饰的方法)与默认方法(default关键字修饰的方法)
例如:
四、接口的继承
1、Java类中只能单继承即一个类只能继承另一个类,但Java接口可以多继承,一个接口可以继承一个或多个接口。对于一个接口来说必须至少有一个实现类完全实现了该接口的抽象方法。
public interface IFather {
void eat();
void breath();
}
public interface IMother {
void speak();
}
public interface ISon extends IFather,IMother{
}
ISon接口继承了IFather接口和IMother接口
2、接口可以继承多个接口,但不能继承类;类可以实现多个接口,但不能继承接口