第八章: Interfaces & Inner Classes

第八章: Interfaces & Inner Classes

 

 

 

 

 

 

 

1、 Interfaces

 

interface Instrument {

 

  // Compile-time constant:

 

  int I = 5; // static & final

 

  // Cannot have method definitions:

 

  void play(Note n); // Automatically public

 

  String what();

 

  void adjust();

 

}
               “Multiple inheritance” in Java如下图:
class Hero extends ActionCharacter

 

    implements CanFight, CanSwim, CanFly {

 

  public void swim() {}

 

  public void fly() {}

 

}      //实体类在前接口在后

 

the core reason for interfaces:first, to be able to upcast to more than one base type,second, 
to prevent the client programmer from making an object of this class and to establish that it is only an interface.
if you know something is going to be a base class, your first choice should be to make it an interface, 
and only if you’re forced to have method definitions or member variables should you change to an abstract class, 
or if necessary a concrete class.
Extending an interface with inheritance:

 

interface Vampire extends DangerousMonster, Lethal {

 

  void drinkBlood();

 

}

 

Nesting interfaces(嵌套接口)

 

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 {        //可以在类里定义privateinterface

 

    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 {}   //接口里定义的嵌套接口不能为private

 

}

 

 

2、Inner classes(内部类)

 

a)        A class defined within a method

 

 

 

 

 

 

 

b)        A class defined within a scope inside a method

 

 

 

 

 

 

 

c)        An anonymous class implementing an interface

 

 

 

 

 

 

 

d)        An anonymous class extending a class that has a nondefault constructor

 

 

 

 

 

 

 

e)        An anonymous class that performs field initialization

 

 

 

 

 

 

 

f)         An anonymous class that performs construction using instance initialization (anonymous inner classes cannot have constructors)

 

 

 

 

 

 

 

2.1)Anonymous inner classes(匿名内部类)

 

 

 

 

 

 

 

public class Parcel6 {

 

 

 

 

 

 

 

  public Contents cont() {

 

 

 

 

 

 

 

    return new Contents() {  //Create an object of an anonymous class that’s

 

 

 

 

 

 

 

      private int i = 11;    //inherited from Contents,通过默认的constructor

 

 

 

 

 

 

 

      public int value() { return i; }

 

 

 

 

 

 

 

    }; // Semicolon required in this case分号是必须的

 

 

 

 

 

 

 

  }

 

 

 

 

 

 

 

  public static void main(String[] args) {

 

 

 

 

 

 

 

    Parcel6 p = new Parcel6();

 

 

 

 

 

 

 

    Contents c = p.cont();

 

 

 

 

 

 

 

  }

 

 

 

 

 

 

 

} ///:~

 

 

 

 

 

 

 

following code shows what to do if your base class needs a constructor with an argument:

 

 

 

 

 

 

 

// An anonymous inner class that calls the base-class constructor.

 

 

 

 

 

 

 

// Creating a constructor for an anonymous inner class

 

 

 

 

 

 

 

abstract class Base {

 

  public Base(int i) {

 

    System.out.println("Base constructor, i = " + i);

 

  }

 

  public abstract void f();

 

}

 

public class AnonymousConstructor {

 

  private static Test monitor = new Test();

 

  public static Base getBase(int i) {

 

// Base constructor call and Pass constructor argument.

 

    return new Base(i) {  //x作为构造函数的参数传给new Base(i)

 

      {  //创建匿名的构造函数,你也只能有一个匿名的构造函数

 

        System.out.println("Inside instance initializer");

 

      }

 

      public void f() {

 

        System.out.println("In anonymous f()");

 

      }

 

    };// Semicolon required

 

  }

 

  public static void main(String[] args) {

 

    Base base = getBase(47);

 

    base.f();

 

    monitor.expect(new String[] {

 

      "Base constructor, i = 47",

 

      "Inside instance initializer",

 

      "In anonymous f()"

 

    });

 

  }

 

} ///:~

 

2.2)If you’re defining an anonymous inner class and want to use an object that’s defined outside the anonymous inner class, 
the compiler requires that the argument reference be final

The link to the outer class: inner classes have access rights to all the elements in the enclosing class(宿主类)

 

 

 

 

 

 

 

2.3)Nested classes(嵌套类)-- make the inner class static.it means 1)You don’t need an outer-class object in order to create an object of a nested class.2)You can’t access a non-static outer-class object from an object of a nested class.它内部可以有static数据成员和内部类,而普通的内部类不可以有

 

 

 

 

 

 

 

2.4)Referring to the outer class object(引用宿主类的对象)-- If you need to produce the reference to the outer class object, you name the outer class followed by a dot and this

 

 

 

 

 

 

 

// Creating instances of inner classes.

 

 

 

 

 

 

 

public class Parcel11 {

 

 

 

 

 

 

 

  class Contents {

 

 

 

 

 

 

 

    private int i = 11;

 

 

 

 

 

 

 

    public int value() { return i; }

 

 

 

 

 

 

 

  }

 

 

 

 

 

 

 

  class Destination {

 

 

 

 

 

 

 

    private String label;

 

 

 

 

 

 

 

    Destination(String whereTo) { label = whereTo; }

 

 

 

 

 

 

 

    String readLabel() { return label; }

 

 

 

 

 

 

 

  }

 

 

 

 

 

 

 

  public static void main(String[] args) {

 

 

 

 

 

 

 

    Parcel11 p = new Parcel11();

 

 

 

 

 

 

 

    // Must use instance of outer class

 

 

 

 

 

 

 

    // to create an instances of the inner class:

 

 

 

 

 

 

 

    Parcel11.Contents c = p.new Contents();

 

 

 

 

 

 

 

    Parcel11.Destination d = p.new Destination("Tanzania");

 

 

 

 

 

 

 

  }

 

 

 

 

 

 

 

} ///:~

 

 

 

 

 

 

 

2.5)Inheriting from inner classes

 

 

 

 

 

 

 

class WithInner {

 

 

 

 

 

 

 

  class Inner {}

 

 

 

 

 

 

 

}

 

 

 

 

 

 

 

public class InheritInner extends WithInner.Inner {

 

 

 

 

 

 

 

  //! InheritInner() {} // Won't compile

 

 

 

 

 

 

 

  InheritInner(WithInner wi) {

 

 

 

 

 

 

 

    wi.super();    //必须传给它宿主类对象的构造函数,初始化宿主类

 

 

 

 

 

 

 

  }

 

 

 

 

 

 

 

  public static void main(String[] args) {

 

 

 

 

 

 

 

    WithInner wi = new WithInner();

 

 

 

 

 

 

 

    InheritInner ii = new InheritInner(wi);

 

 

 

 

 

 

 

  }

 

 

 

 

 

 

 

} ///:~

 

 

 

 

 

 

 

2.6) Why inner classes?—(a)Each inner class can independently inherit from an implementation. Thus, the inner class is not limited by whether the outer class is already inheriting from an implementation.. That is, inner classes effectively allow you to inherit from more than one non-interface.(b)、用于控制框架(control framework),this is an example of the Template Method design pattern.

重载、覆盖、多态、隐藏的区别与联系

     override 可以翻译为覆盖,从字面就可以知道,它是覆盖了一个方法并且对其重写,以求达到不同的作用。对我们来说最熟悉的覆盖就是对接口方法的实现,在接口中一般只是对方法进行了声明,而我们在实现时,就需要实现接口声明的所有方法。除了这个典型的用法以外,我们在继承中也可能会在子类覆盖父类中的方法。在覆盖要注意以下的几点:
1、覆盖的方法的标志必须要和被覆盖的方法的标志完全匹配,才能达到覆盖的效果;
2、覆盖的方法的返回值必须和被覆盖的方法的返回一致;
3、覆盖的方法所抛出的异常必须和被覆盖方法的所抛出的异常一致,或者是其子类;
4、被覆盖的方法不能为private,否则在其子类中只是新定义了一个方法,并没有对其进行覆盖。


      overload可以翻译为重载,它是指我们可以定义一些名称相同的方法,通过定义不同的输入参数来区分这些方法,然后再调用时,VM就会根据不同的参数样式,来选择合适的方法执行。在使用重载要注意以下的几点:
1、在使用重载时只能通过不同的参数样式。例如,不同的参数类型,不同的参数个数,不同的参数顺序(当然,同一方法内的几个参数类型必须不一样,例如可以是fun(int, float), 但是不能为fun(int, int));
2、不能通过访问权限、返回类型、抛出的异常进行重载;
3、方法的异常类型和数目不会对重载造成影响;
4、对于继承来说,如果某一方法在父类中是访问权限是priavte,那么就不能在子类对其进行重载,如果定义的话,也只是定义了一个新方法,而不会达到重载的效果。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值