zoukankan      html  css  js  c++  java
  • java 8-7 接口

    1、 接口的特点:
    A:接口用关键字interface表示
    interface 接口名 {}
    B:类实现接口用implements表示
    class 类名 implements 接口名 {}
    C:接口不能实例化
    那么,接口如何实例化呢?
    按照多态的方式来实例化。
    D:接口的子类
    a:可以是抽象类。但是意义不大。
    b:可以是具体类。要重写接口中的所有抽象方法。(推荐方案)

    由此可见:
    A:具体类多态(几乎没有)
    B:抽象类多态(常用)
    C:接口多态(最常用)

     1 //定义动物培训接口
     2 interface AnimalTrain {
     3 public abstract void jump();
     4 }
     5 
     6 //抽象类实现接口
     7 abstract class Dog implements AnimalTrain {
     8 }
     9 
    10 //具体类实现接口
    11 class Cat implements AnimalTrain {
    12 public void jump() {
    13 System.out.println("猫可以跳高了");
    14 }
    15 }
    16 
    17 class InterfaceDemo {
    18 public static void main(String[] args) {
    19 //AnimalTrain是抽象的; 无法实例化
    20 //AnimalTrain at = new AnimalTrain();
    21 //at.jump();
    22 
    23 AnimalTrain at = new Cat();
    24 at.jump();
    25 }
    26 }

    2、 接口成员特点
    成员变量;只能是常量,并且是静态的。最终量,不能重写
    默认修饰符: public static final
    建议:自己手动给出。
    构造方法:接口没有构造方法。
    成员方法:只能是抽象方法。
    默认修饰符: public abstract
    建议:自己手动给出。

    所有的类都默认继承自一个类:Object。
    类 Object 是类层次结构的根类。每个类都使用 Object 作为超类。

     1 interface Inter {
     2 public int num = 10;
     3 public final int num2 = 20;
     4 public static final int num3 = 30;
     5 
     6 //错误: 需要<标识符>
     7 //public Inter() {}
     8 
     9 //接口方法不能带有主体
    10 //public void show() {}
    11 
    12 //abstract void show(); //默认public
    13 public void show(); //默认abstract
    14 }
    15 
    16 //接口名+Impl这种格式是接口的实现类格式
    17 /*
    18 class InterImpl implements Inter {
    19 public InterImpl() {
    20 super();
    21 }
    22 }
    23 */
    24 
    25 class InterImpl extends Object implements Inter {
    26 public InterImpl() {
    27 super();
    28 }
    29 
    30 public void show() {}
    31 }
    32 
    33 //测试类
    34 class InterfaceDemo2 {
    35 public static void main(String[] args) {
    36 //创建对象
    37 Inter i = new InterImpl();
    38 System.out.println(i.num);
    39 System.out.println(i.num2);
    40 //i.num = 100;
    41 //i.num2 = 200;
    42 //System.out.println(i.num); //无法为最终变量num分配值
    43 //System.out.println(i.num2);//无法为最终变量num2分配值
    44 System.out.println(Inter.num);
    45 System.out.println(Inter.num2);
    46 System.out.println("--------------");
    47 }
    48 }

    3、 类与类:
    继承关系,只能单继承,可以多层继承。
    类与接口:
    实现关系,可以单实现,也可以多实现。
    并且还可以在继承一个类的同时实现多个接口。
    接口与接口:
    继承关系,可以单继承,也可以多继承。

     1 interface Father {
     2 public abstract void show();
     3 }
     4 
     5 interface Mother {
     6 public abstract void show2();
     7 }
     8 
     9 interface Sister extends Father,Mother { //接口跟接口可以是多继承关系
    10 
    11 }
    12 
    13 //class Son implements Father,Mother //类和接口是多继承,多实现
    14 class Son extends Object implements Father,Mother { 
    15 public void show() {
    16 System.out.println("show son"); 
    17 }
    18 
    19 public void show2() {
    20 System.out.println("show2 son");
    21 }
    22 }
    23 
    24 class InterfaceDemo3 {
    25 public static void main(String[] args) {
    26 //创建对象
    27 Father f = new Son();
    28 f.show();
    29 //f.show2(); //报错
    30 
    31 Mother m = new Son();
    32 //m.show(); //报错
    33 m.show2();
    34 }
    35 }

    4、 抽象类和接口的区别:
    A:成员区别
    抽象类:
    成员变量:可以变量,也可以常量
    构造方法:有
    成员方法:可以抽象,也可以非抽象
    接口:
    成员变量:只可以常量
    成员方法:只可以抽象

    B:关系区别
    类与类
    继承,单继承
    类与接口
    实现,单实现,多实现
    接口与接口
    继承,单继承,多继承

    C:设计理念区别
    抽象类 被继承体现的是:”is a”的关系。抽象类中定义的是该继承体系的共性功能。
    接口 被实现体现的是:”like a”的关系。接口中定义的是该继承体系的扩展功能。

    何事都只需坚持.. 难? 维熟尔。 LZL的自学历程...只需坚持
  • 相关阅读:
    验证foreach 能否操做更改原表
    asp.net post/get 公共方法
    C# json日期转换
    学数学
    2742: [HEOI2012]Akai的数学作业
    BZOJ2208
    树状数组求逆序对
    网络流复习计划
    SG函数学(hua)习(shui)记录
    SPLAY板子
  • 原文地址:https://www.cnblogs.com/LZL-student/p/5860915.html
Copyright © 2011-2022 走看看