Java learning interface and inherited internal class usage record

There are four types of internal classes: 
non-static internal classes, 
static internal classes, 
anonymous classes, 
local classes

Key records of non-static internal classes  :

When instantiating a non-static inner class , it must be based on an existing outer class.
Syntax: new outer class (). new inner class ()
non-static inner class, which can directly access the private instance properties of the outer class

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();
    }
 
}

 

Key records of static internal classes:

Syntax: new external class. Static internal class ();
because there is no instance of the external class, the instance properties and methods of the external class cannot be accessed in the static internal class.
In addition to the private static members of the external class, the static internal class No big difference from ordinary

 

Understanding: The instance attributes and methods of the external class cannot be accessed in the static inner class, but the static members and methods of the external class can be accessed!

 

package charactor;
  
public class Hero {
    public static String name;
    protected float hp;
  
    private static void battleWin(){
        System.out.println("battle win");
    }
     
    //敌方的水晶
    static class EnemyCrystal{
        int hp=5000;
         
        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();
    }
  
}

Anonymous key records:

Anonymous class refers to instantiating a class while declaring it, making the code more concise and concise.
Generally, to use an interface or abstract class, you must create a subclass.

Sometimes, in order to use it quickly, instantiate one directly Abstract classes and implement their abstract methods "on the spot".
Now that the abstract method is implemented, it is a new class, but this class is not named.
Such classes are called anonymous classes

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);
    }
      
}

Local key records:

A local class can be understood as a named anonymous class
. The difference between an inner class and an anonymous class is that the inner class must be declared in the position of the member, that is, the position equal to the attribute and method.
The local class is the same as the anonymous class, declared directly in the code block, can be the main method, in the for loop, etc.

 

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();
    }
      
}

 

Guess you like

Origin blog.csdn.net/qq_39418742/article/details/113108952