JAVA中接口的使用

  抽象类是从多个类中抽象出来的模板,如果将这种抽象进行的更彻底,那么就是接口(interface)了。什么是接口,简单的讲,接口就是抽象类的进一步抽象,这种进一步的抽象只定义了一种规范,而不需要关心具体的数据状态和方法实现细节,它只规定了一部分必须提供的方法。下面介绍接口的具体使用细节;

  1.接口里不能包含构造器和初始化块定义,只能包含成员变量(静态常量)、方法(抽象实例方法、类方法或默认方法)、内部类(内部接口、枚举)这三种成员。

  2.接口里的所有成员都应该定义为public权限,这个public可以省略,系统会自动补充。同理,接口里定义的静态常量会自动增加static和final,因此也可以省略。而且由于没有构造器和初始化块,接口里面的成员变量只能在定义时指定初始值。

  3.接口里定义的方法只能是抽象方法、类方法或默认方法,所以系统会自动为普通方法增加public abstract修饰,同时普通方法不能有方法体(抽象方法)。但是类方法和默认方法都必须要有方法体。

  4.默认方法必须要有default修饰,不能用static修饰,同理public会自动增加,默认方法需要接口实现的类的实例来调用。类方法必须要static修饰,public自动增加,类方法可以用接口来直接调用。

  5.接口支持多继承,即一个接口可以有多个直接父接口。

 

  下面是具体的例子:

package biology;
public interface Animal
{
    int classification = 7;
    String eat();
    String move();
    default void description()
    {
        System.out.println("I am a kind of biology");
    }
    static String summary()
    {
        return "The nature is wonderful";
    }
}


package biology;
public class Dog implements Animal
{    
    public String name;
    public Dog(String name)
    {
        this.name = name;
    }
    public String eat()
    {
        return name + " eat meat and grass";
    }
    public String move()
    {
        return name + " move with dog's legs";
    }
}



package biology;
public class Goat implements Animal
{    
    public String name;
    public Goat(String name)
    {
        this.name = name;
    }
    public String eat()
    {
        return name + " eat grass";
    }
    public String move()
    {
        return name + " move with goat's legs";
    }
}




package biology;
public class Tiger implements Animal
{    
    public String name;
    public Tiger(String name)
    {
        this.name = name;
    }
    public String eat()
    {
        return name + " eat meat";
    }
    public String move()
    {
        return name + " move with tiger's legs";
    }
}



package biology;
public class Test
{
    public static void main(String[] args)
    {
        Dog animal1 = new Dog("Shepherd");
        Tiger animal2 = new Tiger("Bengal Tiger");
        Goat animal3 = new Goat("sheep");
        
        System.out.println("The classification of biology is " + Animal.classification);
        System.out.println(animal1.name + ": " + animal1.eat() + ". " + animal1.move());
        System.out.println(animal2.name + ": " + animal2.eat() + ". " + animal2.move());
        System.out.println(animal3.name + ": " + animal3.eat() + ". " + animal3.move());
        animal1.description();
        animal2.description();
        animal3.description();
        System.out.println(Animal.summary());
        
        
    }
}

  运行结果如下:

The classification of biology is 7
Shepherd: Shepherd eat meat and grass. Shepherd move with dog's legs
Bengal Tiger: Bengal Tiger eat meat. Bengal Tiger move with tiger's legs
sheep: sheep eat grass. sheep move with goat's legs
I am a kind of biology
I am a kind of biology
I am a kind of biology
The nature is wonderful

 

  在这里例子中,我们定义了一个接口Animal, 在这个接口中定义了一个成员变量classification(自动添加public static final 修饰, 由接口调用),两个抽象方法eat()和move()(自动添加public abstract 修饰, 由接口实现类来实现), 一个默认方法(由接口实现类的实例调用),一个类方法(直接由接口调用)。

  差不多就是这么多,具体的细节还要多看书和敲代码。

posted @ 2017-08-15 10:49  生信老码农  阅读(714)  评论(0编辑  收藏  举报