服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java 库的建立方法及其实例(下)


  public matcher matcher(){
  
  return new matcher(this);
  
  }
  
  public matcher matcher(string s){
  
  matcher m=new matcher(this);
  
  m.settarget(s);
  
  return m;
  
  }
  
  public matcher matcher(char[] data,int start,int end){
  
  matcher m=new matcher(this);
  
  m.settarget(data,start,end);
  
  return m;
  
  }
  
  public matcher matcher(matchresult res,int groupid){
  
  matcher m=new matcher(this);
  
  if(res instanceof matcher){
  
  m.settarget((matcher)res,groupid);
  
  }
  
  else{
  
  m.settarget(res.targetchars(),res.start(groupid)+res.targetstart(),res.length(groupid));
  
  }
  
  return m;
  
  }
  
  public matcher matcher(reader text,int length)throws ioexception{
  
  matcher m=new matcher(this);
  
  m.settarget(text,length);
  
  return m;
  
  }
  
  以上都是用来实现第一种返回matcher的方法。可以看到,这几种实现都是通过new matcher实例, 以this(即pattern实例)为参数,然后根据参数不同调用matcher.settarget()方法。
  
  public matcher matcher(matchresult res,string groupname){
  
  integer id=res.pattern()。groupid(groupname);
  
  if(id==null) throw new illegalargumentexception("group not found:"+groupname);
  
  int group=id.intvalue();
  
  return matcher(res,group);
  
  }
  
  这是第二种返回matcher的方法。
  
  从这里我们可以发现一种比较好的方法:当你需要两个互相关联的类,一个类的实例需要构造另一 个类的实例,可以在第一个类中用一个public方法,方法实现为调用第二个类的构造函数,并可以 用构造出来的实例调用其他方法,根据第一个类的实例的数据来设置第二个类的实例。
  
  同样的,也有replacer, 和tokenizer的构造方法。
  
  public replacer replacer(string expr){
  
  return new replacer(this,expr);
  
  }
  
  /**
  
  * returns a replacer will substitute all occurences of a pattern
  
  * through applying a user-defined substitution model.
  
  * @param model a substitution object which is in charge for match substitution
  
  * @see replacer
  
  */
  
  public replacer replacer(substitution model){
  
  return new replacer(this,model);
  
  }
  
  public retokenizer tokenizer(string text){
  
  return new retokenizer(this,text);
  
  }
  
  /**
  
  * tokenizes a specified region by an occurences of the pattern.
  
  * note that a series of adjacent matches are regarded as a single separator.
  
  * the same as new retokenizer(pattern,char[],int,int);
  
  * @see retokenizer
  
  * @see retokenizer#retokenizer(jregex.pattern,char[],int,int)
  
  */
  
  public retokenizer tokenizer(char[] data,int off,int len){
  
  return new retokenizer(this,data,off,len);
  
  }
  
  /**
  
  * tokenizes a specified region by an occurences of the pattern.
  
  * note that a series of adjacent matches are regarded as a single separator.
  
  * the same as new retokenizer(pattern,reader,int);
  
  * @see retokenizer
  
  * @see retokenizer#retokenizer(jregex.pattern,java.io.reader,int)
  
  */
  
  public retokenizer tokenizer(reader in,int length) throws ioexception{
  
  return new retokenizer(this,in,length);
  
  }
  
  回忆一下,我在本文的开头,曾经提到过:"一个好的库必须是一个紧凑的关系紧密的整体,而不 是一个分散的关系松散的对象的集合。"从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;

扫描关注微信公众号