zoukankan      html  css  js  c++  java
  • 【Java 基础篇】【第六课】接口interface

    Java提供的这个interface的语法,目的就是将接口从类中剥离出来,构成独立的主体。

    首先加入我们定义了这个杯子接口:

    interface Cup
    {
        void addWater(int w);
        void drinkWater(int w);
    }

    interface当中,注意亮点:

    1.不需要定义方法的主体

    2.不需要说明的可见性(默认为public)

    在一个的类中实施接口,例如下面的MusicCup

    class MusicCup implements Cup
    {
        public void addWater(int w)
        {
            water = water + w;
        }
        
        public void drinkWater(int w)
        {
            water = water - w;
        }
        
    private     int water = 0;
    }

    这里需要注意的就是:一旦在类中实施了某个interface,必须在该类中定义interface的所有方法(addWater()和drinkWater())。类中的方法需要与interface中的方法原型相符。否则,Java将报错。

    interface接口存在的意义:

    我们使用了interface,但这个interface并没有减少我们定义类时的工作量。我们依然要像之前一样,具体的编写类。我们甚至于要更加小心,

    不能违反了interface的规定。既然如此,我们为什么要使用interface呢?

    事实上,interface就像是行业标准。一个工厂(类)可以采纳行业标准 (implement interface),也可以不采纳行业标准。

    但是,一个采纳了行业标准的产品将有下面的好处:

    •更高质量: 没有加水功能的杯子不符合标准。

    •更容易推广: 正如电脑上的USB接口一样,下游产品可以更容易衔接。

    如果我们已经有一个Java程序,用于处理符合Cup接口的对象,比如领小朋友喝水。那么,只要我们确定,我们给小朋友的杯子(对象)实施了Cup接口,就可以确保小朋友可以执行喝水这个动作了。

    至于这个杯子(对象)是如何具体定义喝水这个动作的,我们就可以留给相应的类自行决定 (比如用吸管喝水,或者开一个小口喝水)。

    多个接口:

    一个类可以实施不止一个接口interface。

    例如我们还有一个interface:

    interface Musicplayer
    {
        void play();
    }

    所以真正的MusicCup还需要实施这个接口,所以如下所示:

    class MusicCup implements Cup, MusicPlayer
    {
        public void addWater(int w)
        {
            water = water + w;
        }
        
        public void drinkWater(int w)
        {
            water = water - w;
        }
        
        public void play()
        {
            System.out.println("dun...dun...dun...");
        }
            
        private     int water = 0;
    }

    就这些,好了附带一个源码大家看吧:

     1 interface Cup
     2 {
     3     void addWater(int w);
     4     void drinkWater(int w);
     5 }
     6 
     7 interface MusicPlayer
     8 {
     9     void play();
    10 }
    11 
    12 
    13 /*这个类如果implements Cup了,那么Cup中定义的方法, 在MusicCup
    14 中必须要有addWater和drinkWater,否则会报错,这点和c++不一样*/
    15 class MusicCup implements Cup, MusicPlayer
    16 {
    17     public void addWater(int w)
    18     {
    19         water = water + w;
    20         System.out.println("water is " + water);
    21     }
    22     
    23     public void drinkWater(int w)
    24     {
    25         water = water - w;
    26         System.out.println("water is " + water);
    27     }
    28     
    29     public void play()
    30     {
    31         for (int i = 0; i <water; i++)
    32         {
    33             System.out.println("dun...dun...dun...");
    34         }
    35     }
    36     
    37     public int waterContent()
    38     {
    39         return water;
    40     }    
    41     
    42     private     int water = 0;
    43 }
    44 
    45 public class test 
    46 {
    47     public static void main(String[] args)
    48     {
    49         MusicCup mycupCup = new MusicCup();
    50         mycupCup.addWater(5);
    51         mycupCup.play();
    52         mycupCup.drinkWater(3);
    53         mycupCup.play();
    54         System.out.println("water content is " + mycupCup.waterContent());    
    55     }
    56 }
    View Code

     输出结果:

    water is 5
    dun...dun...dun...
    dun...dun...dun...
    dun...dun...dun...
    dun...dun...dun...
    dun...dun...dun...
    water is 2
    dun...dun...dun...
    dun...dun...dun...
    water content is 2

  • 相关阅读:
    Android的计量单位px,in,mm,pt,dp,dip,sp
    android实现图片平铺效果&WebView多点触控实现缩放
    360°全景图
    为Vell001家族使用过的图标
    WordPress更新服务加快收录
    WordPress模版结构
    android权限大全
    Android强制设置横屏或竖屏
    android获取屏幕分辨率
    android用ImageView显示网络图片
  • 原文地址:https://www.cnblogs.com/by-dream/p/3964648.html
Copyright © 2011-2022 走看看