Java self - Interface with internal class inheritance

Java inner classes

Interior divided into four categories:

Non-static inner class
static inner classes
Anonymous class
local classes

Step 1: nonstatic inner classes

Non-static inner classes BattleScore "fighting performance"
non-static inner classes can be defined directly in a class inside

For example:
the battle results only in the presence of a target hero makes sense
so instantiated BattleScore time, must be based on the presence of a hero of the
syntax: new new outer class () .new inner class ()
as a non-static Hero inner classes, can directly access the outer class private instance attribute name is

package charactor;
 
public class Hero {
    private String name; // 姓名
 
    float hp; // 血量
 
    float armor; // 护甲
 
    int moveSpeed; // 移动速度
 
    // 非静态内部类,只有一个外部类对象存在的时候,才有意义
    // 战斗成绩只有在一个英雄对象存在的时候才有意义
    class BattleScore {
        int kill;
        int die;
        int assit;
 
        public void legendary() {
            if (kill >= 8)
                System.out.println(name + "超神!");
            else
                System.out.println(name + "尚未超神!");
        }
    }
 
    public static void main(String[] args) {
        Hero garen = new Hero();
        garen.name = "盖伦";
        // 实例化内部类
        // BattleScore对象只有在一个英雄对象存在的时候才有意义
        // 所以其实例化必须建立在一个外部类对象的基础之上
        BattleScore score = garen.new BattleScore();
        score.kill = 9;
        score.legendary();
    }
 
}

Step 2: internal static type

Declare a static inner class in which a class
such as Crystal enemy, when the enemy is not the blood of the crystal, own all heroes to win, not just one specific hero to victory.
And nonstatic inner classes, static inner class instance of the class of the crystal does not require an instance of the class on the basis of external , may be directly instantiated
Syntax: new new class internal static type external ();.
Since there is no instance of a class of external, so in the static inner classes inside instance properties and methods are not accessible outside of class
in addition to access to the outer class private static member outside the static inner classes and regular classes no big difference

package charactor;
  
    public class Hero {
        public String name;
        protected float hp;
    
      
    private static void battleWin(){
        System.out.println("battle win");
    }
     
    //敌方的水晶
    static class EnemyCrystal{
        int hp=5000;
         
        //如果水晶的血量为0,则宣布胜利
        public void checkIfVictory(){
            if(hp==0){
                Hero.battleWin();
                 
                //静态内部类不能直接访问外部类的对象属性
                System.out.println(name + " win this game");
            }
        }
    }
     
    public static void main(String[] args) {
        //实例化静态内部类
        Hero.EnemyCrystal crystal = new Hero.EnemyCrystal();
        crystal.checkIfVictory();
    }
  

}

Step 3: anonymous class

Anonymous category refers to the same time declare a class to instantiate it , make the code more simple and concise
Under normal circumstances, to use an interface or an abstract class, you must create a subclass

Sometimes, in order to quickly use, directly instantiate an abstract class, and " on the spot " of its abstract methods.
Now that implements the abstract method, then that is a new class, but this class is not named.
This class, called the anonymous class
Anonymous class

package charactor;
   
public abstract class Hero {
    String name; //姓名
      
    float hp; //血量
          
    float armor; //护甲
          
    int moveSpeed; //移动速度
  
    public abstract void attack();
      
    public static void main(String[] args) {
          
        ADHero adh=new ADHero();
        //通过打印adh,可以看到adh这个对象属于ADHero类
        adh.attack();
        System.out.println(adh);
          
        Hero h = new Hero(){
            //当场实现attack方法
            public void attack() {
                System.out.println("新的进攻手段");
            }
        };
        h.attack();
        //通过打印h,可以看到h这个对象属于Hero$1这么一个系统自动分配的类名
          
        System.out.println(h);
    }
  
}

Step 4: Local Class

Local classes can be understood as the name of the anonymous class
inner classes and anonymous classes is not the same, the internal class must declare the position of a member, that the properties and methods of equality position.
Local and anonymous classes as declared directly inside the block, may be a primary method, and the like in the local loop for

package charactor;
   
public abstract class Hero {
    String name; //姓名
      
    float hp; //血量
          
    float armor; //护甲
          
    int moveSpeed; //移动速度
      
    public abstract void attack();
      
    public static void main(String[] args) {
          
        //与匿名类的区别在于,本地类有了自定义的类名
        class SomeHero extends Hero{
            public void attack() {
                System.out.println( name+ " 新的进攻手段");
            }
        }
         
        SomeHero h  =new SomeHero();
        h.name ="地卜师";
        h.attack();
    }
      
}

Step 5: using an external local variable class anonymous

Use an external anonymous class local variables, external local variables must be modified to final

Why is declared as final, the mechanism is more complex, please refer to the interpretation of the second Hero code

Note: In jdk8, has been modified to not require mandatory final, if not write final, does not complain, because the compiler secretly to help you add the invisible final

package charactor;
   
public abstract class Hero {
 
    public abstract void attack();
      
    public static void main(String[] args) {
 
        //在匿名类中使用外部的局部变量,外部的局部变量必须修饰为final
        final int damage = 5;
         
        Hero h = new Hero(){
            public void attack() {
                System.out.printf("新的进攻手段,造成%d点伤害",damage );
            }
        };
 
    }
      
}
package charactor;
   
public abstract class Hero {
 
    public abstract void attack();
      
    public static void main(String[] args) {
 
        //在匿名类中使用外部的局部变量damage 必须修饰为final
        int damage = 5;
         
        //这里使用本地类AnonymousHero来模拟匿名类的隐藏属性机制
         
        //事实上的匿名类,会在匿名类里声明一个damage属性,并且使用构造方法初始化该属性的值
        //在attack中使用的damage,真正使用的是这个内部damage,而非外部damage
         
        //假设外部属性不需要声明为final
        //那么在attack中修改damage的值,就会被暗示为修改了外部变量damage的值
         
        //但是他们俩是不同的变量,是不可能修改外部变量damage的
        //所以为了避免产生误导,外部的damage必须声明为final,"看上去"就不能修改了
        class AnonymousHero extends Hero{
            int damage;
            public AnonymousHero(int damage){
                this.damage = damage;
            }
            public void attack() {
                damage = 10;
                System.out.printf("新的进攻手段,造成%d点伤害",this.damage );
            }
        }
         
        Hero h = new AnonymousHero(damage);
         
    }
      
}

Guess you like

Origin www.cnblogs.com/jeddzd/p/11577684.html
Recommended