JAVA-Interface and Inheritance (10) Inner Class

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

Non-static inner class

Non-static inner class BattleScore "Battle Score"
non-static inner class can be directly defined in a class

For example,
battle scores are only meaningful when a hero object exists.
Therefore , when instantiating BattleScore, it must be based on an existing hero.
Syntax: new external class (). new internal class ()
as a non-static Hero The inner class can directly access the private instance attribute name of the outer class

package 学习;


public class test {
    
    
	String name;
	public test(String name) {
    
    
		this.name = name;
	}
	
	class Battlescore{
    
    
		int kill;
		int die;
		int assit;
		public void legendary() {
    
    
			if(kill>7)
			System.out.println(name + "超神了");
			else
				System.out.println("还没有超神呢");
		}
	}
	
	public static void main(String args[]) {
    
    
		test a = new test("诺克萨斯之手");
		Battlescore b = a.new Battlescore();
		b.kill = 9;
		b.legendary();
	}
}

Static inner class

Syntax: new external class. Static internal class ();

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

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, for quick use, 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, but this class is not named.
Such classes are called anonymous classes

public abstract class test {
    
    
	String name;
	public abstract void utter();
	public static void main(String args[]) {
    
    
		test a = new test() {
    
    
			public void utter() {
    
    
				System.out.println("当场实现一个方法");
			}
		};
		a.utter();
	}
}

Local class

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

Use external local variables in anonymous classes

Use external local variables in anonymous classes, and external local variables must be modified as final

Why declare as final? Its mechanism is more complicated, please refer to the explanation in the second Hero code

Note: In jdk8, it is no longer necessary to compulsorily modify to final. If final is not written, no error will be reported, because the compiler secretly adds invisible final to you

Exercise-Internal Class ⭐⭐

public abstract class item {
    
    
	String name;
	int price;

	public abstract void disposiable();
	public static void main(String args[]) {
    
    
		item a = new item() {
    
    
			public void disposiable() {
    
    
				System.out.println("我叫做可以任意处理的");
			}
		};
		a.disposiable();
	}
}

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_17802895/article/details/108694428