源码出自著名的开源软件nutch。如下:
public class BasicUrlNormalizer implements UrlNormalizer
{
……
//建立了两个inner static class实例
private Rule relativePathRule = null;
private Rule leadingRelativePathRule = null;
//外部类的构造函数,在其中对两个实例都进行了操作
public BasicUrlNormalizer()
{
relativePathRule = new Rule();
relativePathRule.pattern = (Perl5Pattern)
compiler.compile("(/[^/]*[^/.]{1}[^/]*/\\.\\./)",
Perl5Compiler.READ_ONLY_MASK);
relativePathRule.substitution = new Perl5Substitution("/");
leadingRelativePathRule = new Rule();
leadingRelativePathRule.pattern = (Perl5Pattern)
compiler.compile("^(/\\.\\./)+", Perl5Compiler.READ_ONLY_MASK);
leadingRelativePathRule.substitution = new Perl5Substitution("/");
}
catch (MalformedPatternException e)
{
e.printStackTrace(LogUtil.getWarnStream(LOG));
throw new RuntimeException(e);
}
}
……
//定义的inner static 类Rule
private static class Rule
{
public Perl5Pattern pattern;
public Perl5Substitution substitution;
}
本人是java菜鸟,请各位大虾指点:
(1)Rule没有构造函数,怎么实例化?
(2)static类的member只有一份copy,怎么两个实例同时操作?
public class BasicUrlNormalizer implements UrlNormalizer
{
……
//建立了两个inner static class实例
private Rule relativePathRule = null;
private Rule leadingRelativePathRule = null;
//外部类的构造函数,在其中对两个实例都进行了操作
public BasicUrlNormalizer()
{
relativePathRule = new Rule();
relativePathRule.pattern = (Perl5Pattern)
compiler.compile("(/[^/]*[^/.]{1}[^/]*/\\.\\./)",
Perl5Compiler.READ_ONLY_MASK);
relativePathRule.substitution = new Perl5Substitution("/");
leadingRelativePathRule = new Rule();
leadingRelativePathRule.pattern = (Perl5Pattern)
compiler.compile("^(/\\.\\./)+", Perl5Compiler.READ_ONLY_MASK);
leadingRelativePathRule.substitution = new Perl5Substitution("/");
}
catch (MalformedPatternException e)
{
e.printStackTrace(LogUtil.getWarnStream(LOG));
throw new RuntimeException(e);
}
}
……
//定义的inner static 类Rule
private static class Rule
{
public Perl5Pattern pattern;
public Perl5Substitution substitution;
}
本人是java菜鸟,请各位大虾指点:
(1)Rule没有构造函数,怎么实例化?
(2)static类的member只有一份copy,怎么两个实例同时操作?
try{
relativePathRule = new Rule();
……
2,
成员类(member class):成员类作为一个class的一部分,你可以在class中的任何地方使用成员类,当你想用成员类的变量和方法你可以不需要外在的授权成员类是唯一的一个你可以声明为static的类,当你声明成员的时间,你仅仅在成员类的所属类中可以引用,如果你想去掉这个约束,你可以声明一个成员类为static类。当你声明一个static成员类,他就变成了嵌套的外部类,并且可以象正常类一样http://dev.cbw.com/java/sunjava/20055245411_3257871.shtml也就是说声明为public static class SomeClass,则这个类可以跟一般的public类一样使用,只不过类名在使用的时候要OutClass.SomeClass kls = new OutClass.SomeClass(...);
我看代码也是这样的感觉。