如何用Java编写自己的库(3)
2008-01-05 18:14:44 来源:WEB开发网核心提示:回忆一下,我在本文的开头,如何用Java编写自己的库(3),曾经提到过:"一个好的库必须是一个紧凑的关系紧密的整体,而不是一个分散的关系松散的对象的集合,它说明Matcher类的内部有一个指向Pattern实例的reference. this.re=regex; //int memregCount=(memr
回忆一下,我在本文的开头,曾经提到过:"一个好的库必须是一个紧凑的关系紧密的整体,而不是一个分散的关系松散的对象的集合。"从API说明文档所显示的这个库的树形结构,并不能看出这些类之间的联系。而从源代码的角度,我们则可以清楚地看到这一点。在这一部分的讨论中,我们 也明白了两点:
1、如何编写重载构造函数
2、在一个类的实例中返回另外一个类的实例
接下来,看看Matcher类。这个类实现了MatchResult interface. 看看MatchResult的定义:
[games]$javap -classpath .. -s jregex.MatchResult
Compiled from jregex/MatchResult.java
public interface jregex.MatchResult
/* ACC_SUPER bit NOT set */
{
public static final int MATCH;
/* I */
public static final int PREFIX;
/* I */
public static final int SUFFIX;
/* I */
public static final int TARGET;
/* I */
public abstract jregex.Pattern pattern();
/* ()Ljregex/Pattern; */
public abstract int groupCount();
/* ()I */
public abstract boolean isCaptured();
/* ()Z */
public abstract boolean isCaptured(int);
/* (I)Z */
public abstract boolean isCaptured(java.lang.String);
/* (Ljava/lang/String;)Z */
public abstract java.lang.String group(int);
/* (I)Ljava/lang/String; */
public abstract boolean getGroup(int, java.lang.StringBuffer);
/* (ILjava/lang/StringBuffer;)Z */
public abstract boolean getGroup(int, jregex.TextBuffer);
/* (ILjregex/TextBuffer;)Z */
public abstract java.lang.String group(java.lang.String);
/* (Ljava/lang/String;)Ljava/lang/String; */
public abstract boolean getGroup(java.lang.String, java.lang.StringBuffer);
/* (Ljava/lang/String;Ljava/lang/StringBuffer;)Z */
public abstract boolean getGroup(java.lang.String, jregex.TextBuffer);
/* (Ljava/lang/String;Ljregex/TextBuffer;)Z */
public abstract java.lang.String prefix();
/* ()Ljava/lang/String; */
public abstract java.lang.String suffix();
/* ()Ljava/lang/String; */
public abstract java.lang.String target();
/* ()Ljava/lang/String; */
public abstract int targetStart();
/* ()I */
public abstract int targetEnd();
/* ()I */
public abstract char targetChars()[];
/* ()[C */
public abstract int start();
/* ()I */
public abstract int end();
/* ()I */
public abstract int length();
/* ()I */
public abstract int start(int);
/* (I)I */
public abstract int end(int);
/* (I)I */
public abstract int length(int);
/* (I)I */
public abstract char charAt(int);
/* (I)C */
public abstract char charAt(int, int);
/* (II)C */
}
jregex.MatchResult定义了一些abstract函数。有什么作用?在后面我们将会讨论到。
再看看Matcher的实现。
games]$javap -classpath .. -s jregex.Matcher
Compiled from jregex/Matcher.java
public class jregex.Matcher extends java.lang.Object implements jregex.MatchResult {
public static final int ANCHOR_START;
/* I */
public static final int ANCHOR_LASTMATCH;
/* I */
public static final int ANCHOR_END;
/* I */
public static final int ACCEPT_INCOMPLETE;
/* I */
jregex.Matcher(jregex.Pattern);
/* (Ljregex/Pattern;)V */
public final void setTarget(jregex.Matcher, int);
/* (Ljregex/Matcher;I)V */
public void setTarget(java.lang.String);
/* (Ljava/lang/String;)V */
public void setTarget(java.lang.String, int, int);
/* (Ljava/lang/String;II)V */
public void setTarget(char[], int, int);
/* ([CII)V */
public final void setTarget(char[], int, int, boolean);
/* ([CIIZ)V */
public void setTarget(java.io.Reader, int) throws java.io.IOException;
/* (Ljava/io/Reader;I)V */
public final boolean isStart();
/* ()Z */
public final boolean matches();
/* ()Z */
public final boolean matches(java.lang.String);
/* (Ljava/lang/String;)Z */
public void setPosition(int);
/* (I)V */
public final boolean find();
/* ()Z */
public final boolean find(int);
/* (I)Z */
public jregex.MatchIterator findAll();
/* ()Ljregex/MatchIterator; */
public jregex.MatchIterator findAll(int);
/* (I)Ljregex/MatchIterator; */
public final boolean proceed();
/* ()Z */
public final boolean proceed(int);
/* (I)Z */
public final void skip();
/* ()V */
public java.lang.String toString();
/* ()Ljava/lang/String; */
public jregex.Pattern pattern();
/* ()Ljregex/Pattern; */
public java.lang.String target();
/* ()Ljava/lang/String; */
public char targetChars()[];
/* ()[C */
public int targetStart();
/* ()I */
public int targetEnd();
/* ()I */
public char charAt(int);
/* (I)C */
public char charAt(int, int);
/* (II)C */
public final int length();
/* ()I */
public final int start();
/* ()I */
public final int end();
/* ()I */
public java.lang.String prefix();
/* ()Ljava/lang/String; */
public java.lang.String suffix();
/* ()Ljava/lang/String; */
public int groupCount();
/* ()I */
public java.lang.String group(int);
/* (I)Ljava/lang/String; */
public java.lang.String group(java.lang.String);
/* (Ljava/lang/String;)Ljava/lang/String; */
public boolean getGroup(int, jregex.TextBuffer);
/* (ILjregex/TextBuffer;)Z */
public boolean getGroup(java.lang.String, jregex.TextBuffer);
/* (Ljava/lang/String;Ljregex/TextBuffer;)Z */
public boolean getGroup(int, java.lang.StringBuffer);
/* (ILjava/lang/StringBuffer;)Z */
public boolean getGroup(java.lang.String, java.lang.StringBuffer);
/* (Ljava/lang/String;Ljava/lang/StringBuffer;)Z */
public java.lang.String groups()[];
/* ()[Ljava/lang/String; */
public java.util.Vector groupv();
/* ()Ljava/util/Vector; */
public final boolean isCaptured();
/* ()Z */
public final boolean isCaptured(int);
/* (I)Z */
public final boolean isCaptured(java.lang.String);
/* (Ljava/lang/String;)Z */
public final int length(int);
/* (I)I */
public final int start(int);
/* (I)I */
public final int end(int);
/* (I)I */
public java.lang.String toString_d();
/* ()Ljava/lang/String; */
static {};
/* ()V */
先来看看它的构造函数,这个函数在Pattern中被调用用来构造Matcher类的实例。
Matcher(Pattern regex){
//注重下面这一行,它说明Matcher类的内部有一个指向Pattern实例的reference.
this.re=regex;
//int memregCount=(memregs=new MemReg[regex.memregs]).length;
//for(int i=0;i // this.memregs[i]=new MemReg(-1); //unlikely to SearchEntry, in this case we know memreg indicies by d
efinition
//}
//counters=new int[regex.counters];
//int lookaheadCount=(lookaheads=new LAEntry[regex.lookaheads]).length;
//for(int i=0;i // this.lookaheads[i]=new LAEntry();
//}
//定义了一些内部的数据,MemReg是一个有三个整数的类。类的声明见下。
int memregCount,counterCount,lookaheadCount;
if((memregCount=regex.memregs)>0){
MemReg[] memregs=new MemReg[memregCount];
for(int i=0;i memregs[i]=new MemReg(-1); //unlikely to SearchEntry, in this case we know memreg indicies by defin
ition
}
this.m
更多精彩
赞助商链接