Java学习笔记之接口基本概念

1、基本概念

接口是Java中最重要的概念,接口可以理解为是一种特殊的类,里面全部都是由全局的公共常量和抽象方法组成。

换言之,如果一个类中完全是由全局常量和抽象方法组成,那就可以定义为一个接口

接口定义格式:

interface 接口名 {
全局常量;
抽象方法;
}

interface A {  // 定义接口A
	private static final String AUTOOR = "HLY";  // 全局常量
	private abstract void print();		// 抽象方法
	private abstract String getInfo();	// 抽象方法
}

对于接口来讲,因为在一开始定义的时候就明确其组成是全局常量和抽象方法,于是可以简化如下:

interface A {  // 定义接口A
	String AUTOOR = "HLY";  // 全局常量
	void print();		// 抽象方法
	String getInfo();	// 抽象方法
}

以上两种定义完全一样,没有任何差别。

2、实现接口

与抽象类一样,要实现接口必须使用子类,子类通过implement关键字实现接口
格式如下:

class 子类 implements 接口A,接口B,...{
	// 
} 

我们看一下具体例子

interface IA{   // 定义接口 IA
    String AUTHOR = "YHL";
    void print();
    String getInfo();

}

interface IB{   // 定义接口 IB
    void say();
}

class ChildX implements IA,IB{ // 子类实现接口IA,IB
    public void print(){ // 实现print方法
        System.out.println("作者:" + AUTHOR);
    }

    public String getInfo(){	// 实现getInfo方法
        return "HELLO";
    }

    public void say(){	// 实现say方法
        System.out.println("hello, " + AUTHOR);
    }
}
public class InterfaceDemo {
    public static void main(String[] args) {
        ChildX x = new ChildX();
        x.print();
        x.say();
    }
}

// 运行结果
作者:YHL
hello, YHL

2、继承抽象类实现接口

一个子类可以同时继承抽象类和实现接口
格式如下:

class 子类 extends 抽象类 implements 接口A,接口B...{
}
interface IA{   // 定义接口 IA
    String AUTHOR = "YHL";
    void print();
    String getInfo();

}

abstract class IB{   // 定义接口 IB
    public abstract void say();
}

class ChildX extends IB implements IA{
    public void print(){
        System.out.println("作者:" + AUTHOR);
    }

    public String getInfo(){
        return "HELLO";
    }

    public void say(){
        System.out.println("hello, " + AUTHOR);
    }
}
public class InterfaceDemo {
    public static void main(String[] args) {
        ChildX x = new ChildX();
        x.print();
        x.say();
    }
}

// 运行结果
作者:YHL
hello, YHL

在使用中,一个抽象类可以实现一个接口,那么对于抽象类的子类则必须实现接口和抽象类定义的所有抽象方法。

interface IA{   // 定义接口 IA
    String AUTHOR = "YHL";
    void print();
    String getInfo();

}

abstract class IB implements IA{   // 抽象类 IB 实现接口IA
    public abstract void say();
}

class ChildX extends IB{	// ChildX类继承 IB类,并实现所有抽象方法
    public void print(){
        System.out.println("作者:" + AUTHOR);
    }

    public String getInfo(){
        return "HELLO";
    }

    public void say(){
        System.out.println("hello, " + AUTHOR);
    }
}
public class InterfaceDemo {
    public static void main(String[] args) {
        ChildX x = new ChildX();
        x.print();
        x.say();
    }
}

// 运行结果
作者:YHL
hello, YHL

3、接口的继承

一个接口不能继承抽象类,但是可以利用extends关键字继承多个接口,实现接口的多继承。
格式:

interface 子接口 extends 父接口A,父接口B,...{
}
interface A { // 接口A 
	String AUTHOR = "YHL";  // 全局常量
	void printA();	// 抽象方法printA
}

interface B {
	void printB();
}

interface C extends A,B{  // 接口C继承接口A、B
	void printC()
}

class X implements C{
	public void printA(){
		System.out.println("Hello, printA")
	}
	
	public void printB(){
		System.out.println("Hello, printB")
	}
	
	public void printC(){
		System.out.println("Hello, printC")
	}
}

public class Demo{
	public static void main(String args[]){
		X x = new X();
		x.printA();
		x.printB();
		x.printC();
	}
}

一个接口可以继承多个接口,一个抽象类可以实现一个接口,但是一个接口不能继承一个抽象类。

猜你喜欢

转载自blog.csdn.net/zuolixiangfisher/article/details/85103082