什么是匿名内部类? [英] What is an anonymous inner class?

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

问题描述

当我尝试在Java中的abstract class上做一些示例时,我不小心得到了类似Eclipse中的匿名内部类之类的东西.

When I tried to do some sample on an abstract class in Java I accidentally got some thing like anonymous inner class in Eclipse.

我在下面粘贴了这段代码.我不明白abstract class与匿名类之间的关系.

I have pasted the piece of code below. I don't understand how the abstract class is related to anonymous class.

package com.Demo;

abstract class OuterClass {
    abstract void OuterClassMethod();
}

public abstract class InnerClass extends OuterClass {
    public static void main(String[] args) {
        InnerClass myInnerClass = new InnerClass() {
            @Override
            void OuterClassMethod() {
                int OuterClassVariable = 10;
                System.out.println("OuterClassVariable" + " " + OuterClassVariable);
            }
        };
    }
}

推荐答案

匿名类是类的嵌入式"具体实现,通常(但不一定)是抽象类或接口.从技术上讲,它是扩展/实现的超类的子类.

A anonymous class is an "in-line" concrete implementation of a class, typically (but not necessarily) of an abstract class or an interface. It is technically a subclass of the extended/implemented super class.

通过Google获得更多信息.

Google for more.

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

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