http://wenwen.sogou.com/z/q711752754.htm
1. Java不支持多重继承,即一个类只能有一个父类 2. 为了克服单继承的缺点,Java使用了接口,一个类可以实现多个接口 3. 接口是抽象方法和常量值定义的集合,是一种特殊的抽象类 4. 接口中只包含常量和方法的定义,没有变量和方法的实现 5. 接口中的所有方法都是抽象的 6. 接口中成员的访问类型都是public 7. 接口中的变量默认使用public static final标识(可以在定义的时候不加此修饰,系统默认)1. 接口通过使用关键字interface来声明格式:interface 接口的名字 接口体: 1. 接口体中包含常量定义和方法定义两部分 2. 接口体中只进行方法的声明,不允许提供方法的实现 3. 方法的定义没有方法体,且用分号结尾 http://blog.youkuaiyun.com/liujun13579/article/details/7736116/ 1、精简程序结构,免除重复定义 2、拓展程序功能,应对需求变化。
- interface Flyanimal{
- void fly();
- }
- class Insect {
- int legnum=6;
- }
- class Bird {
- int legnum=2;
- void egg(){};
- }
- class Ant extendsInsect implements Flyanimal {
- public void fly(){
- System.out.println("Ant can fly");
- }
- }
- classPigeon extends Bird implements Flyanimal {
- public void fly(){
- System.out.println("pigeon can fly");
- }
- public void egg(){
- System.out.println("pigeon can lay eggs ");
- }
- }
- public classInterfaceDemo{
- public static void main(String args[]){
- Ant a=new Ant();
- a.fly();
- System.out.println("Ant's legs are"+ a.legnum);
- Pigeon p= new Pigeon();
- p.fly();
- p.egg();
- }
- }