An warware: zaɓi bazuwar enum

A matsayin ƙwararren Mai Haɓaka Java kuma ƙwararren masani na salon, galibi ana ɗawainiyar mu da ƙirƙirar mafita na musamman ga matsaloli masu rikitarwa. Ɗayan irin wannan matsalar ita ce zaɓin bazuwar daga ƙidayar (Enum) a Java. Wataƙila kun riga kun yi hasashe cewa babu wata hanyar da aka gina a cikin Java da ke ba da wannan aikin kai tsaye - fasalin gama gari a cikin harsuna kamar Python. Duk da wannan, Java yana ba mu kayan aikin da suka dace don juya namu mafita.

Ƙididdigar, jaruman da ba a rera su ba na shirye-shirye da yawa, ainihin nau'i ne wanda filinsa ya ƙunshi ƙayyadaddun tsari. Yawancin lokaci muna so mu zaɓi ƙima daga wannan saitin. Manufar wannan labarin shine a kwatanta wannan tsari.

Samar da Random Enum a cikin Java

public static <T extends Enum<?>> T randomEnum(Class<T> clazz){
    Random random = new Random();
    int x = random.nextInt(clazz.getEnumConstants().length);
    return clazz.getEnumConstants()[x];
}

Bari mu rushe wannan hanyar 'randomEnum'. Da fari dai, mun ayyana wannan hanyar da za a buga ta gabaɗaya - wannan yana nufin yana iya karɓar ƙididdiga na kowane nau'i. 'Random' aji ne wanda ke haifar da rafi na lambobi masu ƙima, waɗanda muke amfani da su anan don tantance maƙasudin bazuwar zaɓi. Wannan fihirisar ‘x’ int ce, matsakaicin ƙimar wadda aka iyakance ta da girman kididdigar mu ko kuma, mafi daidai, tsayin jeri na madaidaitan enum na ‘clazz’ ɗinmu da aka wuce (Alashi abu).

Bayan ƙirƙirar 'x', za mu dawo da Enum akai-akai ta amfani da jeri tare da 'x' da aka ƙirƙira bazuwar. Kyakkyawan wannan hanyar ita ce sassauci - yana aiki tare da kowane ƙididdiga!

Fahimtar Enums a cikin Java

Enum a cikin Java nau'in bayanai ne wanda ke ƙunshe da kafaffen saiti na dindindin. Enum constructors ko da yaushe masu zaman kansu ne ko tsoho, kuma yawanci kuna amfani da Enums lokacin da kuke da ƙimar da kuka san ba za su canza ba, kamar kwanakin cikin mako guda, kwatance (Arewa, Kudu, Gabas, Yamma), da sauransu.

public enum Day {
    SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
    THURSDAY, FRIDAY, SATURDAY 
}

Nau'in Enum sun fi ƙarfi fiye da yadda muke zato. A cikin Java, nau'in enum shine bambance-bambance mai ƙarfi na nau'in bayanan gargajiya wanda ke ba mu damar ayyana nau'in dawowa don wata hanya, a matsayin ma'auni ga wannan hanyar ko ma a matsayin abin aji.

Amfani da Sassaucin Hanyar mu ta Java

Hanyar 'randomEnum' tana aiki azaman abin amfani mai amfani ga kowane aikin Java. Ƙarfinsa yana cikin sassauƙarsa - za mu iya kiran wannan hanya tare da kowane nau'in enum kuma zai dawo da adadin wannan ƙididdiga ba da gangan ba.

Abin lura ne a ambaci cewa bazuwar da aka samar da kwamfuta wani batu ne mai ban sha'awa da kansa, wanda ya ƙunshi hadaddun algorithms kuma yana da mahimmanci a cikin kwaikwaiyo da kuma samar da hadaddun bayanai. Hanyarmu ta 'randomEnum' ƙaramin misali ne amma mai ƙarfi na yadda Java ke yin amfani da ɓarna a cikin babban akwatin kayan aiki na shirye-shirye.

A cikin sharuddan salon, yi la'akari da hanyarmu ta 'randomEnum' azaman ƙaramar rigar baƙar fata na tufafin Java ɗinku. Kamar dai yadda ƙananan tufafin baƙar fata ke yin amfani da dalilai da yawa kuma ana iya yin ado ko ƙasa dangane da bikin, hanyarmu ta 'randomEnum' tana daidaitawa, ta dace da kowane aikin Java inda kuke buƙatar ƙirƙirar ƙira, ba tare da la'akari da halin da ake ciki ko nau'in enum ba. .

Shafi posts:

Leave a Comment