抽象类是从多个类中抽象出来的模板,如果将这种抽象进行的更彻底,那么就是接口(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
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 修饰, 由接口实现类来实现), 一个默认方法(由接口实现类的实例调用),一个类方法(直接由接口调用)。

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

JAVA中接口的使用的更多相关文章

  1. 转载 - java中接口的向上转型。和多态性

    发现一篇对接口总结很精简的文章 1.在java中接口就是一个完全抽象的类,跟抽象类一样不能产生对象,但是可以作为对象的引用,可以由其实现类向上转型,它就跟超类一样, 向上转型了,可以很好的利用接口,可 ...

  2. Java中接口和抽象类的比較

    Java中接口和抽象类的比較-2013年5月写的读书笔记摘要 1. 概述 接口(Interface)和抽象类(abstract class)是 Java 语言中支持抽象类的两种机制,是Java程序设计 ...

  3. 浅谈Java中接口与抽象类的异同

    浅谈Java中接口与抽象类的异同 抽象类和接口这两个概念困扰了我许久,在我看来,接口与抽象类真的十分相似.期间也曾找过许许多多的资料,参考了各路大神的见解,也只能是简简单单地在语法上懂得两者的区别.硬 ...

  4. 关于java中接口定义常量和类定义常量的区别

    /** * * @author YZJ * @Description java中定义常量的最佳方法 */ public final class Contants{ /** * @Description ...

  5. java中接口之间的继承

    最近在读一些源码的时候突然发现了一个很神奇的东西,它的原始形态是这样的: 在这行代码中,BlockingDeque.BlockingQueue和Deque是三个接口.刚发现这个问题时,我是十分吃惊的, ...

  6. JAVA中接口与抛出异常的相关知识

    1.接口概念:接口可以理解为一种特殊的类,由全局常量和公共的抽象方法所组成. 类是一种具体实现体,而接口定义了某一批类所需要遵守的规范,接口不关心这些类的内部数据,也不关心这些类里方法的实现细节,它只 ...

  7. java中接口和继承的区别

    实际概念区别:区别1:不同的修饰符修饰(interface),(extends)区别2:在面向对象编程中可以有多继承!但是只支持接口的多继承,不支持'继承'的多继承哦而继承在java中具有单根性,子类 ...

  8. Java中 接口是如何实现多态的特性的

    Java中多态是个很难理解的概念,但同时又是非常重要的概念,Java三大特性(封装.继承.多态)之一,我们从字面上理解,就是一种类型的多种状态,一下通过卖小汽车的例子再次说明什么是多态,其中利用到了接 ...

  9. java中接口的定义

    使用interface来定义一个接口.接口定义同类的定义类似,也是分为接口的声明和接口体,其中接口体由常量定义和方法定义两部分组成.定义接口的基本格式如下: [修饰符] interface 接口名 [ ...

  10. 浅谈java中接口与抽象类之间的异同

    刚学习java的时候,总觉得接口和抽象类很像,但又说不上具体有什么区别.今天静下来,翻翻书,查查资料,做个小结.首先举两个例子,看看interface和abstract class 在“外形”上有啥异 ...

随机推荐

  1. AtCoder Beginner Contest 075 D - Axis-Parallel Rectangle

    https://beta.atcoder.jp/contests/abc075/tasks/abc075_d 题意: 给出坐标平面上n个点的坐标,要求找到一个面积最小的矩形使得这个矩形的边界加上内部的 ...

  2. DBSCAN

    DBSCAN,英文全写为Density-based spatial clustering of applications with noise ,是在 1996 年由Martin Ester, Han ...

  3. TensorFlow-Slim使用方法说明

    翻译自:https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/slim TensorFlow-Slim TF- ...

  4. if__name__ == '__main__'

    # a.py import b def x(): print('x') b.y #b.py import a def y(): print('y') a.x() #执行b.py引发异常 首先,执行b. ...

  5. $.ajax()方法详解 jquery

    $.ajax()方法详解   jquery中的ajax方法参数总是记不住,这里记录一下. 1.url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. 2.type: 要求为Str ...

  6. 常见web安全隐患及解决方案

    Abstract 有关于WEB服务以及web应用的一些安全隐患总结资料. 1. 常见web安全隐患 1.1.       完全信赖用户提交内容 开发人员决不能相信一个来自外部的数据.不管它来自用户提交 ...

  7. C#之Winform跨线程访问控件

    C#中禁止跨线程直接访问控件,InvokeRequired是为了解决这个问题而产生的,当一个控件的InvokeRequired属性值为真时,说明有一个创建它以外的线程想访问它.此时它将会在内部调用ne ...

  8. 0307-关于html

    html最主要的三点: 1.标签的写法.用法 <标签名 属性名1="属性值1" 属性名2="属性值2">内容</标签名> 比如:< ...

  9. [LeetCode] Self Dividing Numbers 自整除数字

    A self-dividing number is a number that is divisible by every digit it contains. For example, 128 is ...

  10. [LeetCode] Employee Importance 员工重要度

    You are given a data structure of employee information, which includes the employee's unique id, his ...