Java でのインターフェイスの実装の動的取得
オブジェクト指向プログラミングでは、インターフェイスの実装のリストを取得すると有益な場合があります。プログラム的にインターフェイスが与えられます。 Java では、これはリフレクションなどのさまざまな手法を通じて実現できます。
1 つのアプローチは、クラスをイントロスペクトし、クラス情報を抽出するための便利なメカニズムを提供するリフレクション ライブラリを使用することです。リフレクションを使用してこれを実現する方法は次のとおりです。
import org.reflections.Reflections; import org.reflections.SubTypesScanner; // Define the interface of interest interface Pet {} // Implementations of the Pet interface class Dog implements Pet {} class Cat implements Pet {} // Use Reflections to get a list of Pet implementations Reflections reflections = new Reflections("package.containing.implementations"); Set<class extends pet>> petImplementations = reflections.getSubTypesOf(Pet.class); // Iterate over and display the implementations for (Class extends Pet> implementation : petImplementations) { System.out.println(implementation.getSimpleName()); // prints Dog, Cat }</class>
あるいは、Java の ServiceLoader 機能を利用して、サービス プロバイダー インターフェイス (SPI) の実装を検出することもできます。
import java.util.ServiceLoader; // Define the Pet interface as an SPI interface Pet {} // Implementations of the Pet interface class Dog implements Pet {} class Cat implements Pet {} // Use ServiceLoader to load implemented services ServiceLoader<pet> loader = ServiceLoader.load(Pet.class); // Iterate over and display the implementations for (Pet implementation : loader) { System.out.println(implementation.getClass().getSimpleName()); // prints Dog, Cat }</pet>
この方法では、インターフェイスの完全な内容を含むファイルを resource/META-INF/services ディレクトリに作成して、インターフェイスを SPI として指定する必要があります。
別のオプションには、パッケージ レベルのアノテーションの定義が含まれます。
import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; // Package-level annotation to define implementations @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.PACKAGE) public @interface MyPackageAnnotation { Class>[] implementationsOfPet() default {}; } // Implementations of the Pet interface class Dog implements Pet {} class Cat implements Pet {} // Define the package-level annotation in a package-info.java file @MyPackageAnnotation(implementationsOfPet = {Dog.class, Cat.class}) package package.containing.implementations; // Iterate over and display the implementations Package[] packages = Package.getPackages(); for (Package p : packages) { MyPackageAnnotation annotation = p.getAnnotation(MyPackageAnnotation.class); if (annotation != null) { Class>[] implementations = annotation.implementationsOfPet(); for (Class> implementation : implementations) { System.out.println(implementation.getSimpleName()); } } }
このアプローチは、ClassLoader がすでに認識しているパッケージに対してのみ機能することに注意してください。より徹底的な検索を行うには、URLClassLoaders を検討してください。ただし、ロードされたクラスに特有の制限事項に注意してください。
以上がJava インターフェイスの実装を動的に取得するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

jvm'sperformanceiscompetitivewitherruntimes、sped、safety、andproductivityの提供

javaachievesplatformedentenceTheThejavavirtualMachine(JVM)、avainwithcodetorunonanyplatformwithajvm.1)codescompiledintobytecode、notmachine-specificcode.2)

thejvmisanabstractcomputingMachineCrucialForrunningJavaProgramsDuetoitsPlatForm-IndopentInterChitecture.Itincludes:1)ClassLoaderForloadingClasses、2)Runtimedataareaforforforatastorage、3)executionEngineWithinterter、Jitcompiler、およびGarbagecolfecolfecolfececolfecolfer

jvmhasacloserelationshiptheosasittrantesjavabytecodecodecodecodecodecodecodecodecodecodecodecodecodetructions、manageSmemory、およびhandlesgarbagecollection.thisrelationshipallowsjavatorunonvariousosenvirnments、Butalsedentsはspeedifediferentjvmbeviorhiorsandosendisfredediferentjvmbehbehioorysando

Javaの実装「Write and、Run Everywherewhere」はBytecodeにコンパイルされ、Java仮想マシン(JVM)で実行されます。 1)Javaコードを書き、それをByteCodeにコンパイルします。 2)JVMがインストールされたプラットフォームでByteCodeが実行されます。 3)Javaネイティブインターフェイス(JNI)を使用して、プラットフォーム固有の機能を処理します。 JVMの一貫性やプラットフォーム固有のライブラリの使用などの課題にもかかわらず、Woraは開発効率と展開の柔軟性を大幅に向上させます。

javaachievesplatformentenceTheTheTheJavavirtualMachine(JVM)、CodetorunondifferentoperatingSystemswithOutModification.thejvmcompilesjavacodeplatform-IndopentedbyTecodeを承認することを許可します

javaispowerfulfulduetoitsplatformindepentence、object-orientednature、richstandardlibrary、performancecapability、andstrongsecurityfeatures.1)platformendependenceallowseplicationStorunonaydevicesupportingjava.2)オブジェクト指向のプログラマン型

上位のJava関数には、次のものが含まれます。1)オブジェクト指向プログラミング、サポートポリ型、コードの柔軟性と保守性の向上。 2)例外処理メカニズム、トライキャッチ式ブロックによるコードの堅牢性の向上。 3)ゴミ収集、メモリ管理の簡素化。 4)ジェネリック、タイプの安全性の向上。 5)コードをより簡潔で表現力豊かにするためのAMBDAの表現と機能的なプログラミング。 6)最適化されたデータ構造とアルゴリズムを提供するリッチ標準ライブラリ。


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SublimeText3 中国語版
中国語版、とても使いやすい

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

MantisBT
Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

SAP NetWeaver Server Adapter for Eclipse
Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。
