接口的使用

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.csdn.net/liujun13579/article/details/7736116/
1、精简程序结构,免除重复定义
2、拓展程序功能,应对需求变化。

 
 
  1. interface   Flyanimal{     
  2.    void fly();  
  3. }  
  4. class   Insect {     
  5.    int  legnum=6;  
  6. }  
  7. class  Bird {     
  8.   int  legnum=2;  
  9.   void egg(){};  
  10. }  
  11. class Ant extendsInsect implements  Flyanimal {  
  12.    public void fly(){  
  13.        System.out.println("Ant can  fly");  
  14.    }  
  15. }  
  16. classPigeon  extends Bird implements  Flyanimal {  
  17.    public void fly(){  
  18.        System.out.println("pigeon  can fly");  
  19.    }  
  20.    public void egg(){  
  21.        System.out.println("pigeon  can lay  eggs ");  
  22.    }  
  23. }  
  24. public classInterfaceDemo{  
  25.    public static void main(String args[]){  
  26.      Ant a=new Ant();  
  27.      a.fly();  
  28.      System.out.println("Ant's legs are"+ a.legnum);  
  29.      Pigeon p= new Pigeon();  
  30.     p.fly();  
  31.      p.egg();  
  32.   }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值