Home > Back-end >  Beginners to ask a question about anonymous classes by value, please everybody help
Beginners to ask a question about anonymous classes by value, please everybody help

Time:10-27

 public class People {

Public interface Runner {
Void the run ();
}
Public void stuAge (int num) {
System. The out. Println (" The age of The Student is "+ num);
}

Public static void main (String [] args) {
Runner Runner=new Runner () {
@ Override
Public void run1 () {
int age=18;
System. Out.println (" run 10 miles!" );
}
};
Runner. The run ();

People People=new People ();
People. StuAge (age);
}
}

I'd like to know how the age in the anonymous inner class stuAge, predecessors please help look at it

CodePudding user response:


/* ** @ description: an anonymous inner class 
* @ author:
* @ date: 2020/10/26 it
*/
Public class People {

Public interface Runner1 {
Void run1 ();
Int run2 ();
}

Public void stuAge (int num) {
System. The out. Println (" The age of The Student is "+ num);
}

Public static void main (String [] args) {
Runner1 runner=new Runner1 () {
int age;
@ Override
Public void run1 () {
Age=18;
System. Out.println (" run 10 miles!" );
}

@ Override
Public int run2 () {
Return the age;
}
};
Runner. Run1 ();

People People=new People ();
People. StuAge (runner. Run2 ());
}
}

In addition to using this method, are there any other way? If the interface cannot be modified
  • Related