Java anonymous class of 200 cases 95 for beginners

Introduction to the author

Author name: Ming Shiyin in programming world
Introduction: CSDN blog expert has been engaged in software development for many years and is proficient in Java and JavaScript. Bloggers also learn and grow step by step from scratch, know the importance of learning and accumulation, and like to fight and upgrade with ADC. Welcome to pay attention and look forward to learning, growing and taking off with you!

introduction

Many Java beginners ask me that it's worrying for the novice to turn around and forget the Java knowledge he has studied very hard. How can Xiaobai grow up quickly and become a big cow?
In fact, there is only one skill to become a big God: "learn more and practice more", so brother Ming sorted out typical practice examples. Through practice, you can quickly improve coding skills and proficiency, so that you can never return on the way to become a big man (remember to practice with your own hands)!

Navigation

✪ introduction to Java white 200 case series directory index
◄ previous article    94.Java inner class - local inner class
► next article to be updated     

summary

Anonymous class refers to an internal class without a class name. Its syntax is as follows:

New < class or interface > (){
//Body of class
};

New StatementDeclares a new anonymous class, which extends a given class or implements a given interface. Using anonymous classes can make the code more concise, compact and modular.

Example 1

Implement a common Java anonymous class

package demo.demo95;
//Define class A
public class A {

	public void print() {
	}
}
package demo.demo95;

public class B {
	
	//test method
	public void test(){
		// The anonymous class created inherits A
		A a = new A(){
			@Override
			public void print() {
				System.out.println("Anonymous class test print");
			}
		};
		a.print();
	}
	
	public static void main(String[] args) {
		new B().test();
	}
}

Operation results:

Anonymous class test print

It can be seen from the output that the anonymous inner class has its own implementation.

Example 2

Implementation of interface anonymous class
package demo.demo95;
//Define class A
public interface C {

	public void print() ;
}
package demo.demo95;

public class D {
	
	//test method
	public void test(){
		// The anonymous class created implements C
		C a = new C(){
			@Override
			public void print() {
				System.out.println("Anonymous class test print Interface");
			}
		};
		a.print();
	}
	
	public static void main(String[] args) {
		new D().test();
	}
}

Operation results:

Anonymous class test print interface

This is similar to the inheritance.

Classic anonymous class instance

Threads are classic instances of anonymous classes

package demo.demo95;

public class ThreadTest {

	public static void main(String[] args) {
		
		Thread thread = new Thread(new Runnable() {
			//Override run method
			public void run() {
				int i=10;
				while (i>0) {
					System.out.println("Thread printing:"+i);
					i--;
				}
			}
		});
		//Start thread
		thread.start();
	}
}

function:

Thread printing: 10
Thread printing: 9
Thread printing: 8
Thread printing: 7
Thread printing: 6
Thread printing: 5
Thread printing: 4
Thread printing: 3
Thread printing: 2
Thread printing: 1

This example can also omit some more code:

package demo.demo95;

public class ThreadTest {

	public static void main(String[] args) {
		//After new is completed, start the thread directly following start 
		new Thread(new Runnable() {
			//Override run method
			public void run() {
				int i=10;
				while (i>0) {
					System.out.println("Thread printing:"+i);
					i--;
				}
			}
		}).start();
	}
}

Summary

This section summarizes the "Java anonymous class". I hope it can be helpful to you. Please help [like] + [collection] + [punch in the comment area]. If you are interested in learning java with brother Xiao Ming, [pay attention to a wave] won't get lost.

Let me know you by punching in the comment area. Mingge will continue to pay attention to your learning progress!

Navigation

✪ introduction to Java white 200 case series directory index
◄ previous article    94.Java inner class - local inner class
► next article to be updated     

Popular column recommendation

1.Java game series (Tetris, aircraft war, plant war, zombie, etc.)
2.JavaWeb project practice (library management, online examination, dormitory management system, etc.)
3. Wonderful examples of JavaScript (aircraft war, minesweeping, snake eating, verification code, etc.)
4. Introduction to Java Xiaobai 200 cases
5. Learn Java from zero, learn Java with interest, and learn Java from the perspective of King glory

Keywords: Java

Added by argh2xxx on Sat, 09 Oct 2021 03:26:28 +0300