如何使用匿名内部类? [英] How to use the anonymous inner class?

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

问题描述

我已经使这两个类使用了匿名内部类的概念。
Class 1有一个静态内部类。第2类使用它。但我无法理解如何调用内部类的方法。请帮帮我。

I have made these 2 classes to make use of the concept of anonymous inner class. Class 1 has a static inner class. And class 2 uses it. But i cant understand how to call the method of the inner class. Please help me out.

Class 1

public class outerclass {
  outerclass() {
    System.out.println("Constructor of new class");
  }

  public void showthis(String str) {
    System.out.println(str);
  }

  static class insideclass {
    insideclass() {
      System.out.println("This is inside class constructor");
    }

    public void nowshowthis(String str) {
      System.out.println(str);
    }
  }
}

Class 2

public class helloworld {
  public static void main(String args[]) {
    //this is an object of the outer class
    outerclass example=new outerclass();
    //How do i make an anonymous inner class and call the method "nowshowthis()"
  }
}


推荐答案

匿名内部类是在另一个类的方法体内创建和定义的内部类。实质上,您正在从抽象定义中动态创建一个具体类。到目前为止,你的InnerClass类实际上只是一个普通的内部类,意思是非匿名的。

An anonymous inner class is one that is created AND defined within the body of another class's method. In essence, you are creating a concrete class on the fly from an abstract definition. What you have so far with your InnerClass class is actually just a regular inner class, meaning non-anonymous.

如果你想尝试匿名的内部类,最简单的方法我能想到的是将你的InnerClass更改为一个接口,如下所示:

If you want to experiment with anonymous inner classes, the simplest way I can think of is to change your InnerClass to an interface, like so:

public interface InnerClass{
    public void doSomething();
}

所以目前,InnerClass蹲下;在定义之前它没有任何意义。接下来,您将要更改OuterClass的工作方式。像这样改变你的showThis()函数:

So at the moment, InnerClass does squat; it has no meaning until it is defined. Next, you'll want to change how OuterClass works. Change your showThis() function like so:

public showThis(InnerClass innerObj){
    innerObj.doSomething();
}

现在我们让你的外部类要求内部类实例做某事,但是我们还没有定义我们希望它做什么。这就是魔术发生的地方 - 在你的main方法中,你将定义内部类实例的实际内容:

Now we have your outer class asking the inner class instance to do something, but we still have not defined what it is we want it to do. This is where the magic happens - in your main method, you will define what the inner class instance actually looks like:

public static void main (String[] args){
    OuterClass outer = new OuterClass();

    // This is the key part: Here you are creating a new instance of inner class 
    // AND defining its body. If you are using Eclipse, and only write the 
    // new InnerClass() part, you'll notice that the IDE complains that you need 
    // to implement the doSomething() method, which you will do as though you
    // were creating a plain 'ol class definition
    outer.showThis(new InnerClass(){ 
        public void doSomething(){
            System.out.println("This is the inner anonymous class speaking!");
        }
    });
}

在实践中,我没有过多使用匿名内部类,但是了解有用。我在进行GUI编程时经常使用它们来定义GUI控件事件的监听器,例如按钮单击。

In practice, I've not used anonymous inner classes too much, however they are useful to know about. I have used them most often when I am doing GUI programming, to define listeners for GUI control events, such as a button click.

另外,正如其他人提到的那样,请记住Java标准的类名的第一个字母是大写字母,我在这里做过。您需要遵循该标准,因为它使其他人更容易阅读您的代码,并且您可以很容易地看出您何时在查看课程以及何时查看对象。

Also, as other people have mentioned, keep in mind that the Java standard has the first letter of the class name a capital letter, which I have done here. You'll want to follow that standard as it makes it far easier for other people to read your code, and at a glance you can very easily tell when you're looking at a class, and when you're looking at an object.

无论如何,希望有所帮助。

Anyways, hope that helps.

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

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