English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Esempio di codice per la regola del nome qualificato dell'interno della classe Java

Breve descrizione:

Poiché di recente ho incontrato molte classi che richiedono la reflection, molte delle quali sono classi interne, elencherò di seguito le regole del nome completo delle classi interne.

Classe interna

I risultati del test dimostrano che, indipendentemente dal fatto che la classe interna sia statica o meno, il nome completo utilizzato è il seguente metodo di nomi:

Nome del pacchetto.Nome della classe esterna$Nome della classe interna

Codice di test:

package com.test;
public class InnerClassTest {
	class static StaticInner{
	}
	class Inner{}}
	}
	public static void main(String[] args) {
		StaticInner si = new StaticInner();
		Inner in = new InnerClassTest().new Inner();
		System.out.println(si.getClass());
		System.out.println(in.getClass());
	}
}

Stampa i risultati:

classcom.test.InnerClassTest$StaticInner
classcom.test.InnerClassTest$Inner

Inner class anonima

Il nome completo dell'inner class anonima soddisfa il seguente schema:

Il nome del pacchetto. Nome della classe esterna$da 1 in poi, in ordine crescente secondo l'ordine di caricamento della classe

Test code

package com.test;
public class InnerClassTest {
	static Object staticAnClass = new Object(){
	}
	;
	static Object staticBlockAnClass;
	static {
		staticBlockAnClass = new Object(){
		}
		;
	}
	Object anClass = new Object(){
	}
	;
	Object blockAnClass;
	{
		blockAnClass = new Object(){
		}
		;
	}
	public static void main(String[] args) {
		InnerClassTest functionClass = new InnerClassTest(){
		}
		;
		System.out.println(staticAnClass.getClass());
		System.out.println(staticBlockAnClass.getClass());
		InnerClassTest test = new InnerClassTest();
		System.out.println(test.anClass.getClass());
		System.out.println(test.blockAnClass.getClass());
		System.out.println(functionClass.getClass());
	}
}

Stampa i risultati:
class com.test.InnerClassTest$1
class com.test.InnerClassTest$2
class com.test.InnerClassTest$3
class com.test.InnerClassTest$4
class com.test.InnerClassTest$5

Local inner class

The full qualified name of the local inner class uses the following naming method:

The package name followed by the external class name, then followed by a positive integer starting from 1 and followed by the local class name - the number part is the order in which the local class appears in the context of the external class.

Test code

package com.test;
public class InnerClassTest {
	public static void main(String[] args) {
		functionA();
		functionB();
	}
	public static void functionA() {
		class Inner {
		}
		;
		System.out.println(new Inner().getClass());
	}
	public static void functionB() {
		class Inner {
		}
		;
		System.out.println(new Inner().getClass());
	}
}

The print result of the above example: 

class com.test.InnerClassTest$1Inner 
class com.test.InnerClassTest$2Inner 
The result after swapping the positions of functionA, B in main on the basis of the test code: 
class com.test.InnerClassTest$2Inner 
class com.test.InnerClassTest$1Inner 
The result of swapping the declaration positions of functionA, B on the basis of the test code: 
class com.test.InnerClassTest$2Inner 
class com.test.InnerClassTest$1Inner

Summary

This is the full content of the code examples of the full qualified name of Java inner classes, I hope it will be helpful to everyone. Those who are interested can continue to read other related topics on this site, and welcome to leave a message to point out any deficiencies. Thank you for your support to this site!

Declaration: The content of this article is from the network, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (when sending an email, please replace # with @) to report it, and provide relevant evidence. Once confirmed, this site will immediately delete the suspected infringing content.

Ti potrebbe interessare