java中的接口是类的蓝图。它具有静态常数和抽象方法。
java中的接口是一种实现抽象的机制。在java接口中只能有抽象的方法,而不是方法主体。它用于在Java中实现抽象和多重继承。
换句话说,您可以说接口可以有方法和变量,但是在接口中声明的方法只包含方法签名,而不是主体。
Java接口也表示IS-A关系。
它不能像抽象类那样被实例化。
它被用来实现抽象。
通过接口,我们可以支持多重继承的功能。
它可以用来实现松耦合。
那么如何声明接口?
接口是通过使用接口关键字来声明的。它提供了抽象;表示接口中所有的方法都是空的,并且是公有的,所有的字段默认都是公共的、静态的和最终的。实现接口的类必须实现接口中声明的所有方法。
如下图所示( class和接口的关系):
学过集成,我们知道继承并不支持多继承,即一个class不可能同时继承多个父类,因为继承由于模糊性,在类的情况下不支持多重继承。但是它在接口的情况下是支持的,因为实现类提供的实现没有歧义。(多继承如第三个代码)
一般情况之下接口是要定义在class之外,为什么要强调这一点...当时上机测试的时候我一直把interface放在class里面,然后就出现了第二个代码的样子(我也很无奈Ծ‸Ծ)
interface Shengwu{
public void breath();
}
interface Dongwu{
public void hassex() ;
public void eat();
}
interface Human{
public void think( );
public void study() ;
}
class Sheng implements Shengwu{
public void breath(){
System.out.println("void breath();");
System.out.println("Ah,I am breathing......so boring....");
}
}
class Animals implements Dongwu{
public void hassex(){
System.out.println("void hassex();");
System.out.println("Ah,I am faml......");
}
public void eat(){
System.out.println("void eat();");
System.out.println("what do I eat?.....I am so hunary....");
}
}
class Humans implements Human{
public void think(){
System.out.println("void think();");
System.out.print(" Ah,I am thinking now......so interesting....");
}
public void study() {
System.out.println("void study();");
System.out.println("I want to study......Do not stop me....");
}
}
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
Sheng a=new Sheng();
Animals b=new Animals();
Humans c=new Humans();
System.out.println( "所有的输出如下:");
a.breath();
System.out.println( );
b.eat();
System.out.println( );
b.hassex();
System.out.println( );
c.study();
System.out.println( );
c.think();
}
}
以下就是把interface放在class里面的结果,两段代码的最大区别就是第二个代码class需要有static修饰,要不然会报错(之后我会总结一个博客关于static)
public class implement {
interface Shengwu{
public void breath();
}//Shoeing
interface Dongwu{
public void hassex();
public void eat();
}
interface Human{
public void think();
public void study();
}
// public static
public static class Sheng implements Shengwu{
public void breath(){
System.out.println("Ah,I am breathing......so boring....");
}
}
// public static
public static class Animals implements Dongwu{
public void hassex(){
System.out.println("Ah,I am famle......");
}
public void eat(){
System.out.println("what do I eat?.....I am so hunary....");
}
}
// public static
public static class Humans implements Human{
public void think(){
System.out.print(" Ah,I am thinking now......so interesting....");
}
public void study(){
System.out.println("I want to study......Do not stop me....");
}
}
public static void main(String[] args) {
Sheng a=new Sheng();
Animals b=new Animals();
Humans c=new Humans();
a.breath();
b.eat();
b.hassex();
c.study();
c.think();
}
}
下面这个简单的代码很好地体现了多接口的特点,并让我们理解多接口的实现:
interface Printable{
void print();
}
interface Showable{
void show();
}
public class CsdnMoreImplement implements Printable, Showable {
public void print(){System.out.println("Hello,world!");}
public void show(){System.out.println("Hello,Java...");}
public static void main(String args[]){
CsdnMoreImplement a = new CsdnMoreImplement();
a.print();
a.show();
}
}
接口还有许多:嵌套接口、接口的扩展等等
给大家推荐一个比较好的学习编程语言的网站,里面主流programming language基本都有,而且可以一边学英语一边学编程:点击打开链接