1, first anonymous inner class to inherit from the abstract base class or implement the base class interface
Like this
Abstract class Seed{int Cnt;public Seed (int x) {cnt=x;} abstract void Showinfo ();} Class Apple{int Num=300;public seed getseed (final int x) {return new Seed (x) {public void Showinfo () {System.out.println (" The Apple's seeds is "+num);}};}
2. The parent class field cannot be initialized within the class unless the initialization block is used, and the child segments of the outer class cannot be changed
That's not going to work,//!. The comment section is not correct
Abstract class Seed{int Cnt;public Seed (int x) {cnt=x;} abstract void Showinfo ();} Class Apple{int Num=300;public seed getseed (final int x) {return new Seed (x) {//!num=200;//!cnt=100;public void Showinfo () {System.out.println ("The Apple ' s seeds is" +num);}};}
Reason:
The so-called anonymous inner class is essentially the same as the real name class, while the position of the inner segment of the class can only be declared or inside the constructor, and changes to the value of the field can only be made within the class method. Changes to field values in other locations are not valid because changes and actions at this time have no chance of execution, so the compiler will make an error. It is possible to manipulate and change the fields in the initialization block.
This is not going to work.
public class wrapping{public int a;public int b;//!a=100;}
Usage Considerations for anonymous internal classes of Java