java interface

 1 interface Runner //定义接口
 2 {
 3         int i=3;
 4         public void start();
 5         void run();
 6         void stop();
 7 }
 8 interface Eater extends Runner //接口间可以继承
 9 {
10         public final static int j=4;
11         void openMouth();
12         void upAndDown();
13         void goIn();
14 }
15 class TT implements Eater //引用接口
16 {
17         public void start()
18         {
19                 System.out.println("---------start()-------");
20         }
21         public void run()
22         {
23                 System.out.println("---------run()-------");
24         }
25         public void stop()
26         {
27                 System.out.println("---------stop()-------");
28         }
29         public void openMouth()
30         {
31                 System.out.println("---------openMouth()-------");
32         }
33         public void upAndDown()
34         {
35                 System.out.println("---------upAndDown()-------");
36         }
37         public void goIn()
38         {
39                 System.out.println("---------goIn()-------");
40         }
41 }
42 public class TestInterface
43 {
44         public static void main(String[] args)
45         {
46                 Runner tt=new TT();//接口的引用指向实现的对象
47                 System.out.println(tt.i);
48                 System.out.println(Runner.i);
49                 tt.start();
50                 Eater ee=new TT();
51                 System.out.println(ee.j);
52                 System.out.println(Eater.j);
53                 ee.start();
54         }
55 }
复制代码

接口的应用2:

复制代码
 1 public class TestInterface {
 2         
 3         public static void main(String[] args){
 4                 
 5                 CareAnimalable c = new Worker();
 6                 //Worker w = (Worker)c;
 7                 TestInterface t = new TestInterface();
 8                 t.t(c); //多态
 9 
10                 c = new Farmer();
11                 t.t(c); 
12 
13                 
14         }
15 
16         public void t(CareAnimalable c){//尽量定义为接口或父类的引用
17                 c.feed();
18                 c.play();
19         }
20 }
21 
22 
23 interface CareAnimalable{
24         public void feed();
25         public void play();
26 }
27 
28 class Worker implements CareAnimalable{
29         
30         public void feed(){
31                 System.out.println("-----feed()----");
32         }
33 
34         public void play(){
35                 System.out.println("-----play()----");
36         }
37 }
38 
39 class Farmer implements CareAnimalable{
40         
41         public void feed(){
42                 System.out.println("-----Farmer feed()----");
43         }
44 
45         public void play(){
46                 System.out.println("-----Farmer play()----");
47         }
48 }
复制代码


以上就是接口的用法,在编程开发中我们可以通过定义接口来简便编程。在ANDROID开发中,接口的用处很大,所以希望大家能把接口的知识掌握。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值