zoukankan      html  css  js  c++  java
  • Java学习之面向对象(1.接口)

    1.接口(interface):

    (1)多个无关的类可以实现同一个接口。

    (2)一个类可以实现多个无关的接口。

    (3)与继承关系类似,接口与实现类之间存在多态性。

     (4)接口是抽象方法和常量值的定义的集合。

    (5)从本质上讲,接口是一种特殊的抽象类,这种抽象类中只包含常量和方法的定义,而没有变量和方法的实现。

    接口定义举例:

    public interface Runner{

      public static final int id=1;

      public void start();

      public void run();

      public void stop();

    }

    2.接口的特性:

    (1)接口可以多重实现;(多继承)

    (2)接口中声明的属性默认为 public static final 的,也只能是 public static final 的;

    (3)接口中只能定义抽象方法,而且这些方法默认为 public的,也只能是 public的;

    (4)接口可以继承其它的接口,并添加新的属性和抽象方法。( interface A extends B{})

    这里有一个游戏,人猿泰山。
    主角是一个单独的类,这里我们主要用怪物说明接口的用法:
    怪物有很多种,
    按地域分:有的在天上飞,有的在地上跑,有的在水里游
    按攻击方式分:有的能近距离物理攻击,有的能远距离射击

    假设游戏里需要这样的几种怪——
    野狗:地上移动,近距离攻击
    黑熊:地上移动,近/远距离攻击
    秃鹫:地上/天上移动,远距离攻击
    食人鱼:水中移动,近距离攻击
    鳄鱼:地上/水中移动,近距离攻击

     1 interface OnEarth{//陆地接口
     2 int earthSpeed=100;//陆地移动速度
     3 void earthMove();//陆地移动方法
     4 }
     5 
     6 interface OnWater{//水中接口
     7 int waterSpeed=200;//水中移动速度
     8 void waterMove();//水中移动方法
     9 }
    10 
    11 interface OnAir{//空中接口
    12 int airSpeed=1000;//水中移动速度
    13 void airMove();//水中移动方法
    14 }
    15 
    16 interface NearAttack{//近距离攻击接口
    17 int nearAttackPower=100;//近距离攻击力
    18 void nearAttack();//近距离攻击方法
    19 }
    20 
    21 interface FarAttack{//远距离攻击接口
    22 int farAttackPower=100;//远距离攻击力
    23 void farAttack();//远距离攻击方法
    24 }
    25 
    26 //这样一来,根据需求,我们可以选择性的继承接口:
    27 class Tyke implements OnEarth, NearAttack{ //野狗类
    28 public void earthMove(){//实现继承的方法1
    29     System.out.println("earthMove");
    30     } 
    31 public void nearAttack(){//实现继承的方法2
    32     System.out.println("nearAttack");
    33     }
    34 }
    35 
    36 class BlackBear implements OnEarth, NearAttack, FarAttack{//黑熊类
    37 public void earthMove(){//实现继承的方法1
    38     System.out.println("earthMove");
    39 } 
    40 public void nearAttack(){//实现继承的方法2
    41     System.out.println("nearAttack");
    42 }
    43 public void farAttack(){//实现继承的方法3
    44     System.out.println("farAttack");
    45 }
    46 }
    47 
    48 class Vulture implements OnEarth, OnAir, FarAttack{//秃鹫类
    49 public void earthMove(){//实现继承的方法1
    50     System.out.println("earthMove");
    51 } 
    52 public void airMove(){//实现继承的方法2
    53     System.out.println("airMove");
    54 } 
    55 public void farAttack(){//实现继承的方法3
    56     System.out.println("farAttack");
    57 }
    58 }
    59 
    60 class ManeatFish implements OnWater, NearAttack{//食人鱼类
    61 public void waterMove(){//实现继承的方法1
    62     System.out.println("waterMove");
    63 } 
    64 public void nearAttack(){//实现继承的方法2
    65     System.out.println("nearAttack");
    66 }
    67 }
    68 
    69 class Crocodile implements OnEarth, OnWater, NearAttack{//鳄鱼类
    70 public void earthMove(){//实现继承的方法1
    71     System.out.println("earthMove");
    72 } 
    73 public void waterMove(){//实现继承的方法2
    74     System.out.println("waterMove");
    75 }
    76 public void nearAttack(){//实现继承的方法3
    77     System.out.println("nearAttack");
    78 }
    79 }
    80 public class TextInterface{
    81 public static void main(String[] args){
    82     Crocodile c=new Crocodile();
    83     c.earthMove();
    84     
    85 }
    86 }
    输出:

    earthMove



  • 相关阅读:
    读后感
    mysql分库分表的基本方法
    pdo接口用法
    视频技术基础
    【原创】shell易错语法汇总
    php底层的运行机制
    mysql统计函数
    etc/shadow 登陆口令破解
    JAVA学习(方法重载)
    JAVA学习(方法的定义及调用)
  • 原文地址:https://www.cnblogs.com/shide/p/2969181.html
Copyright © 2011-2022 走看看