Java inner class
There are four internal categories:
Non-static internal classes
Static inner class
Anonymous class
Local class
Step 1: Non-static internal classes
BattleScore "Battle Achievements" of Non-Static Internal Class
Non-static inner classes can be defined directly in one class
For example:
Battle results are meaningful only when a hero exists.
So when you instantiate BattleScore, you have to build on an existing hero.
Syntax: new external class (). new internal class ()
As Hero's non-static inner class, it can directly access the private instance attribute name of the external class.
package charactor; public class Hero { private String name; // Full name float hp; // Blood volume float armor; // Armor int moveSpeed; // Moving speed // Non-static internal classes are meaningful only when an external class object exists. // Battle results are meaningful only when a hero exists. class BattleScore { int kill; int die; int assit; public void legendary() { if (kill >= 8) System.out.println(name + "Holy shit(dota), legendary(lol)!"); else System.out.println(name + "Not yet supernatural!"); } } public static void main(String[] args) { Hero garen = new Hero(); garen.name = "Galen"; // Instantiate internal classes // BattleScore objects only make sense when a hero object exists. // So its instantiation must be based on an external class object. BattleScore score = garen.new BattleScore(); score.kill = 9; score.legendary(); } }
Step 2: Static inner classes
Declare a static inner class in a class
For example, enemy crystal, when the enemy crystal has no blood, all our heroes win, not just a specific hero win.
Unlike non-static internal classes, the instantiation of static internal crystal-like classes does not require an instance of an external class as a basis and can be instantiated directly.
Grammar: new external class. static internal class ();
Because there is no instance of an external class, instance attributes and methods of an external class cannot be accessed within a static internal class.
Apart from private static members that can access external classes, static internal classes are not much different from ordinary classes.
package charactor; public class Hero { public String name; protected float hp; private static void battleWin(){ System.out.println("battle win"); } //Enemy Crystal static class EnemyCrystal{ int hp=5000; //If the blood volume of the crystal is zero, victory is declared. public void checkIfVictory(){ if(hp==0){ Hero.battleWin(); //Static internal classes cannot directly access object properties of external classes System.out.println(name + " win this game"); } } } public static void main(String[] args) { //Instantiating static inner classes Hero.EnemyCrystal crystal = new Hero.EnemyCrystal(); crystal.checkIfVictory(); } }
Step 3: Anonymous classes
Anonymous classes refer to instantiating a class while declaring it to make the code more concise and concise.
Usually, to use an interface or abstract class, you must create a subclass
Sometimes, in order to use it quickly, an abstract class is instantiated directly and its abstract method is implemented "on the spot".
Now that the abstract method is implemented, it is a new class, just this class, not named.
Such classes are called anonymous classes
package charactor; public abstract class Hero { String name; //Full name float hp; //Blood volume float armor; //Armor int moveSpeed; //Moving speed public abstract void attack(); public static void main(String[] args) { ADHero adh=new ADHero(); //By printing adh, you can see that the object ADH belongs to the ADHero class adh.attack(); System.out.println(adh); Hero h = new Hero(){ //On-the-spot implementation of attack method public void attack() { System.out.println("New offensive means"); } }; h.attack(); //By printing h, you can see that the object h belongs to a class name automatically assigned by a system like Hero . System.out.println(h); } }
Step 4: Local classes
Local classes can be understood as anonymous classes with names
Unlike anonymous classes, internal classes must declare their position as members, i.e. equal to attributes and methods.
Local classes, like anonymous classes, are declared directly in the code block, which can be the main method, for loop, and so on.
package charactor; public abstract class Hero { String name; //Full name float hp; //Blood volume float armor; //Armor int moveSpeed; //Moving speed public abstract void attack(); public static void main(String[] args) { //The difference from anonymous classes is that local classes have custom class names class SomeHero extends Hero{ public void attack() { System.out.println( name+ " New offensive means"); } } SomeHero h =new SomeHero(); h.name ="Meepo"; h.attack(); } }
Step 5: Use external local variables in anonymous classes
Using external local variables in anonymous classes, external local variables must be modified to final
Why to declare it as final? Its mechanism is complex. Please refer to the explanation in the second Hero code.
Note: In jdk8, no mandatory final is required. If you don't write final, you won't make an error because the compiler adds invisible final to you secretly.
package charactor; public abstract class Hero { public abstract void attack(); public static void main(String[] args) { //Using external local variables in anonymous classes, external local variables must be modified to final final int damage = 5; Hero h = new Hero(){ public void attack() { System.out.printf("New means of attack, resulting in%d Point injury",damage ); } }; } } package charactor; public abstract class Hero { public abstract void attack(); public static void main(String[] args) { //The use of external local variable damage in anonymous classes must be modified to final int damage = 5; //Here we use the local class AnonymousHero to simulate the hidden attribute mechanism of anonymous classes. //In fact, an anonymous class declares a damage attribute in the anonymous class and initializes the value of the attribute with a constructor //The damage used in attack really uses the internal damage, not the external damage. //Assuming that external attributes do not need to be declared final //Modifying the damage value in attack is then implied as modifying the damage value of the external variable. //But they are different variables, and it is impossible to modify the external variable damage. //So in order to avoid misleading, external damage must be declared final, and "looks" cannot be modified. class AnonymousHero extends Hero{ int damage; public AnonymousHero(int damage){ this.damage = damage; } public void attack() { damage = 10; System.out.printf("New means of attack, resulting in%d Point injury",this.damage ); } } Hero h = new AnonymousHero(damage); } }