哪些访问修饰符应该被设置给内部类的成员接口,以及为什么。

huangapple 未分类评论45阅读模式
英文:

which access modifiers should be set to a member interface belong to inner-class, and why

问题

在下面发布的代码中,HTEsotericPoliticsOfTheWest 是一个内部类。当我尝试定义一个接口时,Eclipse 生成了一个错误,因为接口不是静态的!你能解释一下为什么在内部类的上下文中,接口必须被声明为静态的吗?

**代码:

//这是一个内部类
private class HTEsotericPoliticsOfTheWest extends HandlerThread {

    private final String TAG = HTEsotericPoliticsOfTheWest.class.getSimpleName();
    private Handler _mHandler = null;

    //#构造函数
    public HTEsotericPoliticsOfTheWest(String name) {
        super(name);
    }
    public HTEsotericPoliticsOfTheWest(String name, int priority) {
        super(name, priority);
    }

    public void enqueueTaskForNorthGroup(String token) {
        Message msg = Message.obtain();
        msg.obj = token;
    }

	//********问题在这里***********
    interface Isynchronizer {
        void filteredNews();
    }

    public void enqueueTaskForEastGroup(String token) {
    }
    //#处理程序实例
    public Handler getInitedHandlerInstanceForNorthGroup() {
        return new Handler(getLooper(), new HandlerCallbackNorthGroup());
    }
    public Handler getInitedHandlerInstanceForEastGroup() {
        return new Handler(getLooper(), new HandlerCallbackEastGroup());
    }

    //#运行
    @Override
    public void run() {
        super.run();
        Log.w(TAG, "run");
    }


    @Override
    protected void onLooperPrepared() {
        super.onLooperPrepared();
        Log.w(TAG, "onLooperPrepared");
    }

    //#终止方法
    @Override
    public boolean quit() {
        return super.quit();
    }

    @Override
    public boolean quitSafely() {
        return super.quitSafely();
    }

    //#处理程序回调类
    private class HandlerCallbackNorthGroup implements Handler.Callback {

        @Override
        public boolean handleMessage(Message msg) {
            String token = (String) msg.obj;
            int s;

            switch (token) {
                case NORTH_GROUP_PRESEDENTIAL_1:
                    s = 3;
                    try {
                        TimeUnit.SECONDS.sleep(3);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Log.i("", s + " elapsed");
                    break;
                case NORTH_GROUP_EDUCATIONAL_PRIVATE_1:
                    s = 7;
                    try {
                        TimeUnit.SECONDS.sleep(7);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Log.i("", s + " elapsed");
                    break;
                case NORTH_GROUP_GEOGRAPHICAL_1:
                    s = 10;
                    try {
                        TimeUnit.SECONDS.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Log.i("", s + " elapsed");
                    break;
                case NORTH_GROUP_MARINE_1:
                    s = 13;
                    try {
                        TimeUnit.SECONDS.sleep(13);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Log.i("", s + " elapsed");
                    break;
            }
            return false;
        }
    }


    //问题:在主 Activity 中的处理程序与内部类中的处理程序之间传递/通信是否可行? 
    private class HandlerCallbackEastGroup implements Handler.Callback {

        @Override
        public boolean handleMessage(Message msg) {
            return false;
        }
    }
}
英文:

In the below posted code, HTEsotericPoliticsOfTheWest is an inner-class, when i attempted to define an interface, eclipse generated an error as the interface is not ststic!
can you please explain why an interface must be declared ststic within the context of an inner-class??

**code:

//This is an inner-class
private class HTEsotericPoliticsOfTheWest extends HandlerThread {

    private final String TAG = HTEsotericPoliticsOfTheWest.class.getSimpleName();
    private Handler _mHandler = null;

    //#constructor(s)
    public HTEsotericPoliticsOfTheWest(String name) {
        super(name);
    }
    public HTEsotericPoliticsOfTheWest(String name, int priority) {
        super(name, priority);
    }

    public void enqueueTaskForNorthGroup(String token) {
        Message msg = Message.obtain();
        msg.obj = token;
    }

	//********ISSUE IS HERE***********
    interface Isynchronizer {
        void filteredNews();
    }

    public void enqueueTaskForEastGroup(String token) {
    }
    //#handlers-instances
    public Handler getInitedHandlerInstanceForNorthGroup() {
        return new Handler(getLooper(), new HandlerCallbackNorthGroup());
    }
    public Handler getInitedHandlerInstanceForEastGroup() {
        return new Handler(getLooper(), new HandlerCallbackEastGroup());
    }

    //#run
    @Override
    public void run() {
        super.run();
        Log.w(TAG, "run");
    }


    @Override
    protected void onLooperPrepared() {
        super.onLooperPrepared();
        Log.w(TAG, "onLooperPrepared");
    }

    //#termination-methods
    @Override
    public boolean quit() {
        return super.quit();
    }

    @Override
    public boolean quitSafely() {
        return super.quitSafely();
    }

    //#handlers-callbacks classes
    private class HandlerCallbackNorthGroup implements Handler.Callback {

        @Override
        public boolean handleMessage(Message msg) {
            String token = (String) msg.obj;
            int s;

            switch (token) {
                case NORTH_GROUP_PRESEDENTIAL_1:
                    s = 3;
                    try {
                        TimeUnit.SECONDS.sleep(3);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Log.i("", s + " elapsed");
                    break;
                case NORTH_GROUP_EDUCATIONAL_PRIVATE_1:
                    s = 7;
                    try {
                        TimeUnit.SECONDS.sleep(7);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Log.i("", s + " elapsed");
                    break;
                case NORTH_GROUP_GEOGRAPHICAL_1:
                    s = 10;
                    try {
                        TimeUnit.SECONDS.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Log.i("", s + " elapsed");
                    break;
                case NORTH_GROUP_MARINE_1:
                    s = 13;
                    try {
                        TimeUnit.SECONDS.sleep(13);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Log.i("", s + " elapsed");
                    break;
            }
            return false;
        }
    }


    //Q:is it achievable/feasible to pass/communicate between two Handlers?one is in the mainAct while the other is in an inner class?
    private class HandlerCallbackEastGroup implements Handler.Callback {

        @Override
        public boolean handleMessage(Message msg) {
            return false;
        }
    }
}

答案1

得分: 0

> Eclipse产生了一个错误,因为该接口不是静态的!

当一个接口嵌套在另一个类型中时,它是一个static成员。然而,问题在于内部类可能不会声明静态成员。这就是导致错误的组合。

在内部类方面,Java语言规范 对于接口有明确的规定:

> 8.1.3. 内部类和封闭实例
>
> 内部类不可以声明静态初始化程序(§8.7)或者成员接口,否则会导致编译时错误。
>
> 内部类不可以声明静态成员,除非它们是常量变量(§4.12.4),否则会导致编译时错误。

你可能会对阅读JLS 14版本感兴趣,该版本重新表述了嵌套接口作为类的成员是隐式静态的;并且不允许内部类声明静态成员(无论是显式还是隐式)。

英文:

> eclipse generated an error as the interface is not static!

When an interface is nested in another type, it is a static member. The problem, however, is that an inner class may not declare static members. This is the combination that's leading to the error.

On inner classes, the Java Language Specification is even specific about interfaces:

> 8.1.3. Inner Classes and Enclosing Instances
>
> Inner classes may not declare static initializers (§8.7) or member interfaces, or a compile-time error occurs.
>
> Inner classes may not declare static members, unless they are constant variables (§4.12.4), or a compile-time error occurs.

You may be interested to read JLS 14's version which is reworded to specify that a nested interface is implicitly static as a member of the class; and that an inner class is not allowed to declare static (explicitly or implicitly so) members.

huangapple
  • 本文由 发表于 2020年4月8日 13:20:24
  • 转载请务必保留本文链接:https://java.coder-hub.com/61093807.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定