匿名内部类和最终修饰符 [英] Anonymous Inner classes and Final modifier

查看:132
本文介绍了匿名内部类和最终修饰符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解正确匿名班总是 final

这已在 JLS 15.9.5

中具体提到但是,当我运行以下代码时检查它是否显示内部类不是 final

However, when i run the following code to check that it is showing that Inner class is not final.

    public class Test{
    static class A<T> {
    }
    public static void main(String arg[]) {
        A<Integer> obj = new A() {
        };
        if ((obj.getClass().getModifiers() & Modifier.FINAL) != 0) {
            System.out.println("It is a final  " + obj.getClass().getModifiers());
        } else {
            System.out.println("It is not final " + obj.getClass().getModifiers());
        }
    }
}

上述程序的输出为:

It is not final 0

请清除我的疑问,因为我无法理解这种行为。

Please clear my doubt as i am not able to understand this behavior.

推荐答案

显式是源代码中编写的内容。因此,如果某些内容被声明为公共最终类,则表示该类显式为最终

Explicit is something what is written in the source code. So, if something is declared as public final class, it means that the class is explicitly final.

隐式是未在源代码中写下的内容,但在某个构造的上下文中或基于语言规则,元素的行为与在指定修饰符。

Implicit is something that is not written down in the source code, but that in the context of a certain construct or based on the language rules, an element behaves as it were declared with the specified modifier.

例如,声明枚举SomeEnum {}中的关键字 enum 导致 SomeEnum final ,因为语言规则规定了它。它的效果与关键字 final 的效果相同。

For example, the keyword enum in the declaration enum SomeEnum { } causes SomeEnum to be final, because the language rules prescribe it. Its effect is the same as the effect of the keyword final.

匿名类的示例是隐式最终的是因为没有语言结构来覆盖匿名类。所以它表现得好像是 final 。我认为有效这个词在这里更好。

The example of an anonymous class being implicitly final is because no language construct exists to override an anonymous class. So it behaves as if it were final. I think the word "effectively" is better here.

但是,你不能根据反射的呈现方式做出假设。请考虑以下代码段:

However, you cannot make assumptions based on how reflection presents things. Consider the following snippet:

public class Test {
    interface SomeInterface { }
    abstract interface SomeAbstractInterface { }
    static abstract class SomeAbstractClass { }
    enum SomeEnum { }

    public static void main(String arg[]) {
        System.out.println(Modifier.toString(SomeInterface.class.getModifiers()));
        System.out.println(Modifier.toString(SomeAbstractInterface.class.getModifiers()));
        System.out.println(Modifier.toString(SomeAbstractClass.class.getModifiers()));
        System.out.println(Modifier.toString(SomeEnum.class.getModifiers()));
    }
}

结果如下:

abstract static interface
abstract static interface
abstract static
static final

接口抽象接口被认为是一个抽象的接口。它们也被反射视为静态的。显然,在解析和编译Java源代码的过程中,可以删除或添加一些修饰符。

Both interface and abstract interface are considered an abstract interface. They are also considered static by reflection. Apparently, in the process of parsing and compiling the Java source code, some modifiers could be removed or added.

这篇关于匿名内部类和最终修饰符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆