接口总结

为什么要使用接口呢?其实大部分时候我们能不用接口尽量不用,因为比起抽象类来,它的运行速度是慢的。但正如书中所言,接口最吸引人的原因之一就是允许同一个接口具有不同的具体实现。其次,它向上转型也可以实现多个基类,防止客户端程序员创建该类的对象等等。接口可以实现多继承,而抽象类不可以。抽象类可以有一些默认的方法实现,接口没有。接口的访问修饰符默认是public的,不允许修改。接口没有构造器,接口不能实现main方法。
适配问题
在适配接口中,我们能学到, 可以依据不同接口,来给一个类实现不同的适配功能。这里也提到了Scanner类和Readable接口之间的适配问题。
同时将查阅过的文档和Readable接口的说明附上:
请看代码如下:
 1 // Implementing an interface to conform to a method.
 2 import java.nio.*;
 3 import java.util.*;
 4  
 5 public class RandomWords implements Readable {
 6   private static Random rand = new Random(47);
 7   private static final char[] capitals =
 8     "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
 9   private static final char[] lowers =
10     "abcdefghijklmnopqrstuvwxyz".toCharArray();
11   private static final char[] vowels =
12     "aeiou".toCharArray();
13   private int count;
14   public RandomWords(int count) { this.count = count; }    
15   public int read(CharBuffer cb) {
16     if(count-- == 0)
17       return -1; // Indicates end of input
18     cb.append(capitals[rand.nextInt(capitals.length)]);
19     for(int i = 0; i < 4; i++) {
20       cb.append(vowels[rand.nextInt(vowels.length)]);
21       cb.append(lowers[rand.nextInt(lowers.length)]);
22     }
23     cb.append(" ");
24     return 10; // Number of characters appended
25   }
26   public static void main(String[] args) {
27     Scanner s = new Scanner(new RandomWords(10));
28     while(s.hasNext())
29       System.out.println(s.next());
30   }
31 }

 

这里实现了接口Readable中的read()方法。而它只是单独为Scanner创建的,让Scanner成为不会被限制的某个特定类。这样就解放了Scanner,它可以作用于更多的类型了。所以说,在现有类上增加新的接口,可以让方法接受接口类型,是一种让任何类都可以对该方法进行适配的方式。
接口的域
接口的任何域自动都是static和final的。在接口的常量初始化的时候必须给其初值。定义的常量中不允许有空final。
嵌套接口
在类中可以增加内部接口,内部接口可以被置为private,但是它的调用必须是用类中的某个private方法调用的。在接口中也可以镶嵌内部接口,但是所有的接口都是public。
参考以下代码:
//: interfaces/nesting/NestingInterfaces.java
package interfaces.nesting;
 
class A {
  interface B {
    void f();
  }
  public class BImp implements B {
    public void f() {}
  }
  private class BImp2 implements B {
    public void f() {}
  }
  public interface C {
    void f();
  }
  class CImp implements C {
    public void f() {}
  }    
  private class CImp2 implements C {
    public void f() {}
  }
  private interface D {
    void f();
  }
  private class DImp implements D {
    public void f() {}
  }
  public class DImp2 implements D {
    public void f() {}
  }
  public D getD() { return new DImp2(); }
  private D dRef;
  public void receiveD(D d) {
    dRef = d;
    dRef.f();
  }
}    
 
interface E {
  interface G {
    void f();
  }
  // Redundant "public":
  public interface H {
    void f();
  }
  void g();
  // Cannot be private within an interface:
  //! private interface I {}
}    
 
public class NestingInterfaces {
  public class BImp implements A.B {
    public void f() {}
  }
  class CImp implements A.C {
    public void f() {}
  }
  // Cannot implement a private interface except
  // within that interface's defining class:
  //! class DImp implements A.D {
  //!  public void f() {}
  //! }
  class EImp implements E {
    public void g() {}
  }
  class EGImp implements E.G {
    public void f() {}
  }
  class EImp2 implements E {
    public void g() {}
    class EG implements E.G {
      public void f() {}
    }
  }    
  public static void main(String[] args) {
    A a = new A();
    // Can't access A.D:
    //! A.D ad = a.getD();
    // Doesn't return anything but A.D:
    //! A.DImp2 di2 = a.getD();
    // Cannot access a member of the interface:
    //! a.getD().f();
    // Only another A can do anything with getD():
    A a2 = new A();
    a2.receiveD(a.getD());
  }
} ///:~

 

仔细看一看,这段代码极为有趣。

转载于:https://www.cnblogs.com/masking-timeflows/p/9359649.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值