Home > Java > javaTutorial > body text

Introduction to the differences between anonymous inner classes in java

高洛峰
Release: 2017-03-19 10:54:59
Original
1579 people have browsed it

Anonymous internal classes are internal classes without names

Because they have no names, anonymous internal classes can only be used once. They are usually used to simplify code writing

But use anonymous internals The class also has a prerequisite: it must inherit a parent class or implement an interface

Example 1: Do not use anonymous inner classes to implement abstract methods

abstract class Person {
    public abstract void eat();
}
 
class Child extends Person {
    public void eat() {
        System.out.println("eat something");
    }
}
 
public class Demo {
    public static void main(String[] args) {
        Person p = new Child();
        p.eat();
    }
}
Copy after login

Running results: eat something

As you can see, we use Child to inherit the Person class, and then implement an instance of Child and transform it up into a reference to the Person class

However, if the Child here A class is only used once, so wouldn't it be troublesome to write it as an independent class?

At this time, anonymous inner classes were introduced

Example 2: Basic implementation of anonymous inner classes

abstract class Person {
    public abstract void eat();
}
 
public class Demo {
    public static void main(String[] args) {
        Person p = new Person() {
            public void eat() {
                System.out.println("eat something");
            }
        };
        p.eat();
    }
}
Copy after login

Running result: eat something

As you can see, we directly implement the methods in the abstract class Person in braces

This way we can omit the writing of a class

And anonymous inner classes can also be used On the interface

Example 3: Using anonymous inner class on the interface

interface Person {
    public void eat();
}
 
public class Demo {
    public static void main(String[] args) {
        Person p = new Person() {
            public void eat() {
                System.out.println("eat something");
            }
        };
        p.eat();
    }
}
Copy after login

Running result: eat something

By As can be seen from the above example, as long as a class is abstract or an interface, then the methods in its subclasses can be implemented using anonymous inner classes

The most common situation is in multi-threaded implementation , because to implement multi-threading, you must inherit the Thread class or the Runnable interface

Example 4: Anonymous inner class implementation of the Thread class

public class Demo {
    public static void main(String[] args) {
        new Thread() {
            public void run() {
                for (int i = 1; i <= 5; i++) {
                    System.out.print(i + " ");
                }
            }
        }.start();
    }
}
Copy after login

Running results: 1 2 3 4 5

Example 5: Anonymous inner class implementation of Runnable interface

public class Demo {
    public static void main(String[] args) {
        Runnable r = new Runnable() {
            public void run() {
                for (int i = 1; i <= 5; i++) {
                    System.out.print(i + " ");
                }
            }
        };
        Thread t = new Thread(r);
        t.start();
    }
}
Copy after login

In fact, it can be written more concisely

public static void main(String[] args) {
        new Thread(new Runnable() {
            public void run() {
                for (int i = 1; i <= 5; i++) {
                    System.out.print(i + " ");
                }
            }
        }).start();
    }
Copy after login

Run results: 1 2 3 4 5


The above is the detailed content of Introduction to the differences between anonymous inner classes in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template