A detailed example of an anonymous inner class in java
- 2020-10-31 21:45:08
- OfStack
An instance of an anonymous inner class in java
Original appearance:
class TT extends Test{
void show() {
System.out.println(s+"~~~ Ha ha ");
System.out.println(" Super girl ");
}
TT tt=new TT();
tt.show();
It's just that we're doing it anonymously here.
The show() method of Test was overridden, and when it was overridden, the overridden show() method was called
Implementation code:
package cn.com;
class Test {
String s = " Long live brother chun! ";
public void show(Test t) {
System.out.println(" Believe in brother chun for eternal life ");
}
}
class Outer {
int x = 23;
int i = 12;
public void method() {
new Test() {// Anonymous inner class
void show() {// rewrite show() methods
System.out.println(s+"~~~ Ha ha ");
System.out.println(" Super girl ");
}
}.show();
}
}
class TestInner {
public static void main(String[] args) {
new Outer().method();
}
}
If you have any questions, please leave a message or go to this site community exchange discussion, thank you for reading, I hope to help you, thank you for your support to this site!