黑马程序员--No enclosing instance of type E is accessible.

来源:互联网 发布:java如何调用接口 编辑:程序博客网 时间:2024/06/05 09:35

——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——-
过滤器设置成内部类还是外部类?
如果内部类设置成内部,就不能定义含参数的构造函数,程序的扩展小,而且不注意的话编译时回会报出如下的错误:No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing instance of type E(e.g. x.new A() where x is an instance of E). E指代内部类。
提示意思是:没有可访问的内部类E的实例,必须分配一个合适的内部类E的实例(如x.new A(),x必须是E的实例。)
其中的原因是:内部类是动态的,也就是开头以public class开头。而主程序是public static class main。在Java中,类中的静态方法不能直接调用动态方法。只有将某个内部类修饰为静态类,然后才能够在静态类中调用该类的成员变量与成员方法
解决方法:
1在不做其他变动的情况下,将public class改为public static class.
2.将内部类设置成外部类,就不会出现这样的错误,设置成外部类的另外一个好处是,能够定义带参数的构造函数,提高程序的扩展性。
欢迎遇到同样问题的朋友查阅。
过滤器代码如下:

package cn.itcast.io.pp.file;import java.io.File;import java.io.FilenameFilter;/** * 文件名过滤器 * @author hello * */class SuffixFilter implements FilenameFilter {    private String suffix;    public SuffixFilter(String suffix) {        super();        this.suffix = suffix;    }    public boolean accept(File dir, String name) {        //对文件名进行健壮性判断        if((name == null && "".equals(name))) {            throw new RuntimeException("文件名不存在");        }        //注意不能使用equals方法        return name.toLowerCase().endsWith(suffix);    }}

——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——-

0 0