English 中文(简体)
匿名内身 3. 要求澄清的类别
原标题:Anonymous Inner Class request for clarification

在学习TTS时,我穿过以下法典:

    speakBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            mTts.speak(words.getText().toString(), TextToSpeech.QUEUE_ADD, null);
        }});

我确实是 Java的新鲜事,因此我对确定各种构造的信任程度是巨大的。 我认为,以上是,但new OnClickListener ()与我混淆。 因此,请确认和/或纠正以下任何一项谅解:

  1. The inner class is defined right after new OnClickListener().
  2. OnClickListener is a super class from which the inner class is derived.
  3. The (anonymous) inner class has only one member function: OnClick().
  4. What is @Override inside the definition of the inner class? If this is an annotation, then I am confused as this answer states that anonymous inner classes cannot be annotated.

最后,能否用像我这样宽松的方式来撰写上述雕像?

最佳回答
  1. Yes; it s defined between the braces.
  2. Yes, except that it s an interface, not a class.
  3. Yes.
  4. You cannot add class-level annotations.
    @Override is a method-level annotation, which works fine.
问题回答

基本上,这套法典构成了一种由汇编者为你所欲知的类别,因此,你需要全心全意地关注其命名。 这一班级将安装“OnClickListener”接口,并按接口要求,控制Click(View)方法的实施。

因此,你的狙击手可以这样作:

class OnClickListenerThingy01 implements OnClickListener { // name is invented from the top of my head and corresponds actual name manging in no way
    @Override
    public void onClick(View view) {
        mTts.speak(words.getText().toString(), TextToSpeech.QUEUE_ADD, null);
    }
}
speakBtn.setOnClickListener(new OnClickListenerThingy01());

The @Override annotation is not place on the category se - which has no declaration in You code the annotation can be supplemented, being codificationer- produced - but on the methods.

The @Override annotation is used to mark overrides (how surprising) and method implementations. Its main use is to generate a compiler error if the signature of the overridden method changes, but you fail to update the overriding declaration accordingly, so that you won t get really surprised when you overrides fail to work, because, say, what was overridden was renamed.

如果在接口实施方面,如果你忘记全面实施接口,汇编者将产生错误,因此“Override”可能看起来是多余的,但并非多余。 事实上,在您的法典中,不依赖的方法(例如,从接口中删除方法声明)赢得了一定停留,这是一件很 n的事情。

虽然必须指出的是,像Eclipse这样的民主选举学会很可能使这些关切无效,因为所提供的辅助工具足以避免这种实地工作。 任何途径,@Override,在使用时,在你的方法上都很 n。

在这里,为改写你的幻灯,使其更加清晰地“到”:

class MyOuterClass {
    private class MyOnClickListener implements OnClickListener {
        @Override
        public void onClick(View view) {
            mTts.speak(words.getText().toString(), TextToSpeech.QUEUE_ADD, null);
        }
    }

    // later (inside some method)...
        speakBtn.setOnClickListener(new MyOnClickListener());
}
  • The inner class is defined right after new OnClickListener().

在该守则中,有人想说,奥克罗赖斯特(Lstener)是名副其实的。 如同你所认为,类别定义也放在方括号内。

  • OnClickListener is a super class from which the inner class is derived.

  • The (anonymous) inner class has only one member function: OnClick().

情况并非如此。 它可以拥有更多的成员职能。

  • What is @Override inside the definition of the inner class? If this is an annotation, then I am confused as this answer states that anonymous inner classes cannot be annotated.

rel=“nofollow”> 超过用于对方法进行实时错误核对。 您可使用匿名内部班级方法说明。

OnClickListener is a super class from which the inner class is derived

Not really : OnClickListener is not a class. This syntaxis is used to create an instance of a new (anonymous) class which implements the interface OnClickListener. So you have only one method to implement : onClick().





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签