楼主: NewOccidental
2072 16

Java in a Nutshell, 6th Edition [推广有奖]

11
Nicolle 学生认证  发表于 2015-9-25 09:42:04 |只看作者 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

12
Nicolle 学生认证  发表于 2015-9-25 09:46:56 |只看作者 |坛友微信交流群
提示: 作者被禁止或删除 内容自动屏蔽

使用道具

13
ReneeBK 发表于 2015-9-25 09:49:29 |只看作者 |坛友微信交流群
  1. Extending interfaces
  2. Interfaces may extend other interfaces, and, like a class definition, an interface definition may include an extends clause. When one interface extends another, it inherits all the abstract methods and constants of its superinterface and can define new abstract methods and constants. Unlike classes, however, the extends clause of an interface definition may include more than one superinterface. For example, here are some interfaces that extend other interfaces:

  3. public interface Positionable extends Centered {
  4.     void setUpperRightCorner(double x, double y);
  5.     double getUpperRightX();
  6.     double getUpperRightY();
  7. }
  8. public interface Transformable extends Scalable, Translatable, Rotatable {}
  9. public interface SuperShape extends Positionable, Transformable {}
复制代码

使用道具

14
ReneeBK 发表于 2015-9-25 09:50:04 |只看作者 |坛友微信交流群
  1. Defining an Interface
  2. An interface definition is much like a class definition in which all the methods are abstract and the keyword class has been replaced with interface. For example, the following code shows the definition of an interface named Centered. A Shape class, such as those defined earlier in the chapter, might implement this interface if it wants to allow the coordinates of its center to be set and queried:

  3. public interface Centered {
  4.     void setCenter(double x, double y);
  5.     double getCenterX();
  6.     double getCenterY();
  7. }
  8. A number of restrictions apply to the members of an interface:

  9. An interface contains no implementation whatsoever. All methods of an interface are implicitly abstract and must have a semicolon in place of a method body. The abstract modifier is allowed but, by convention, is usually omitted. Since static methods may not be abstract, the methods of an interface may not be declared static .

  10. An interface defines a public API. All members of an interface are implicitly public, and it is conventional to omit the unnecessary public modifier. It is an error to define a protected or private method in an interface.

  11. An interface may not define any instance fields. Fields are an implementation detail, and an interface is a pure specification without any implementation. The only fields allowed in an interface definition are constants that are declared both static and final.

  12. An interface cannot be instantiated, so it does not define a constructor.

  13. Interfaces may contain nested types. Any such types are implicitly public and static. See Section 3.10 later in this chapter.
复制代码

使用道具

15
ReneeBK 发表于 2015-9-25 09:51:07 |只看作者 |坛友微信交流群
  1. Example 3-7. Defining and using a static member interface
  2. // A class that implements a stack as a linked list
  3. public class LinkedStack {
  4.     // This static member interface defines how objects are linked
  5.     // The static keyword is optional: all nested interfaces are static
  6.     public static interface Linkable {
  7.         public Linkable getNext();
  8.         public void setNext(Linkable node);
  9.     }

  10.     // The head of the list is a Linkable object
  11.     Linkable head;  

  12.     // Method bodies omitted
  13.     public void push(Linkable node) { ... }
  14.     public Object pop() { ... }
  15. }

  16. // This class implements the static member interface
  17. class LinkableInteger implements LinkedStack.Linkable {
  18.     // Here's the node's data and constructor
  19.     int i;
  20.     public LinkableInteger(int i) { this.i = i; }

  21.     // Here are the data and methods required to implement the interface
  22.     LinkedStack.Linkable next;
  23.     public LinkedStack.Linkable getNext() { return next; }
  24.     public void setNext(LinkedStack.Linkable node) { next = node; }
复制代码

使用道具

16
ReneeBK 发表于 2015-9-25 09:51:38 |只看作者 |坛友微信交流群
  1. Example 3-8. An iterator implemented as a member class
  2. import java.util.Iterator;

  3. public class LinkedStack {
  4.     // Our static member interface
  5.     public interface Linkable {
  6.         public Linkable getNext();
  7.         public void setNext(Linkable node);
  8.     }
  9.    
  10.     // The head of the list
  11.     private Linkable head;  
  12.    
  13.     // Method bodies omitted here
  14.     public void push(Linkable node) { ... }
  15.     public Linkable pop() { ... }
  16.    
  17.     // This method returns an Iterator object for this LinkedStack
  18.     public Iterator<Linkable> iterator() { return new LinkedIterator(); }
  19.    
  20.     // Here is the implementation of the Iterator interface,
  21.     // defined as a nonstatic member class.
  22.     protected class LinkedIterator implements Iterator<Linkable> {
  23.         Linkable current;
  24.         // The constructor uses the private head field of the containing class
  25.         public LinkedIterator() { current = head; }
  26.         // The following 3 methods are defined by the Iterator interface
  27.         public boolean hasNext() {  return current != null; }
  28.         public Linkable next() {
  29.             if (current == null) throw new java.util.NoSuchElementException();
  30.             Linkable value = current;
  31.             current = current.getNext();
  32.             return value;
  33.         }
  34.         public void remove() { throw new UnsupportedOperationException(); }
  35.     }
  36. }
复制代码

使用道具

17
Lisrelchen 发表于 2016-5-8 04:56:20 |只看作者 |坛友微信交流群

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加JingGuanBbs
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-5 19:12