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.