An warware: java samun aji ta kirtani

samun aji ta kirtani A cikin duniyar shirye-shirye, magance matsalolin da kyau da inganci yana da mahimmanci ga kowane mai haษ“akawa. ฦŠayan irin wannan matsala da ke tasowa a cikin ci gaban Java shine samun aji ta sunan kirtani. Wannan labarin yana da nufin samar da cikakkiyar mafita ga wannan matsala tare da zurfafa cikin ษ—akunan karatu da ayyukan da ke tattare da magance ta. Bari mu fara da fahimtar mahallin da wannan matsalar gabaษ—aya ke faruwa.

Masu haษ“aka Java sau da yawa suna fuskantar yanayi inda suke buฦ™atar aiki tare da azuzuwan waษ—anda aka ba da sunayensu azaman kirtani. Mafi yawan amfani-harka shi ne lokacin ฦ™irฦ™irar misalan aji cikin kuzari yayin lokacin aiki. Sanin yadda ake aiki tare da kirtani da canza su zuwa aji shine fasaha mai mahimmanci ga kowane mai haษ“aka aiki tare da Java.

Tunani Java

Don magance wannan matsala, Java yana ba da wani abu mai ฦ™arfi da ake kira Gani. Tunani yana ba da damar dubawa da hulษ—a tare da azuzuwan, musaya, filaye, da hanyoyi a lokacin aiki. Yana baiwa masu haษ“akawa damar ฦ™irฦ™irar misalan aji, kiran hanyoyin, samu ko saita ฦ™imar filayen, da ฦ™ari - duk sun dogara ne akan ฦ™irar sunan ajin.

Samun Daraja ta Sunan Kiษ—a

Yanzu bari mu binciko mafita ga matsalar samun class da sunan kirtani. Za mu yi amfani da Class.don Suna() Hanyar, wanda wani bangare ne na API Reflection na Java.

public class GetClassFromString {
    public static void main(String[] args) {
        try {
            String className = "java.util.ArrayList";
            Class<?> clazz = Class.forName(className);
            System.out.println("Class: " + clazz.getName());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

A cikin wannan misalin lambar, mun ฦ™irฦ™iri madaidaicin kirtani da ake kira Sunan aji kuma ya sanya shi darajar "java.util.ArrayList". Na gaba, muna amfani da Class.don Suna() hanya, wanda ke ษ—aukar Sunan aji na kirtani azaman hujja. Wannan hanyar tana dawo da misalin ajin, wanda muke adanawa a cikin nau'in nau'in clazz Class. A ฦ™arshe, muna buga sunan ajin ta amfani da shi Samun Suna () Hanya.

  • Class.don Suna() - yana loda ฦ™ayyadaddun ajin kuma ya dawo da bayanin nau'in Class<?>. Wannan hanyar za ta jefa ClassNotFoundException idan ba a sami ajin ba.
  • Class - Nau'in aji na geteric da aka yi amfani da shi don gudanar da tunani game da aji mai ษ—orewa.
  • Samun Suna () โ€“ ya dawo da cikakken sunan ajin a matsayin kirtani.

ฦ˜irฦ™irar Misalai da Amfani da Hanyoyin Aji

Yanzu da muka san yadda ake samun aji da sunan zaren sa, bari mu ga yadda ake ฦ™irฦ™irar misali na ajin da aka samu kuma mu yi hulษ—a da hanyoyinsa da filayensa.

public class CreateInstanceFromString {
    public static void main(String[] args) {
        try {
            String className = "java.util.ArrayList";
            Class<?> clazz = Class.forName(className);
            Object instance = clazz.getDeclaredConstructor().newInstance();
            Method addMethod = clazz.getMethod("add", Object.class);
            addMethod.invoke(instance, "Hello, world!");
            System.out.println("Instance: " + instance);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

A cikin wannan misali, mun fara samun aji kamar yadda kafin amfani Class.don Suna(). Na gaba, muna ฦ™irฦ™irar misali na ajin ta amfani da getDeclaredConstructor().newInstance(). Bayan haka, muna samun hanyar "ฦ™ara" daga aji ta amfani da samun Hanyar() kuma yi amfani da shi a matsayin misali kira() hanya tare da kirtani a matsayin hujja. A ฦ™arshe, muna buga misalin don ganin sakamakon.

A ฦ™arshe, masu haษ“aka Java sukan haษ—u da yanayi inda suke buฦ™atar samun aji daga kirtani kuma suyi aiki tare da hanyoyinsa da filayen sa yayin lokacin aiki. Yin amfani da Tunani na Java, masu haษ“akawa za su iya sarrafa waษ—annan yanayi yadda ya kamata kuma su ฦ™irฦ™iri ฦ™arfi, lambar daidaitawa. Tare da ilimin Tunani da misalan lambar da aka bayar a cikin wannan labarin, zaku iya da gaba gaษ—i magance matsalar samun aji ta sunan kirtani.

Shafi posts:

Leave a Comment