Chwetheza iinkcazo kwiJava

Chwetheza iinkcazo kwiJava

Ngokungeniswa kweJava 8, inguqulelo iye yazisa izinto ezimbini ezintsha ezibizwa ngokuba ziinkcazo eziphindaphindayo kunye nohlobo lweenkcazo. De kube leli xesha, ukuba ubufuna ukusebenzisa amanqakwana eJava, ungenza njalo kwizibhengezo. NgeJava 8, ngoku unokongeza amanqakwana kulo naluphi na uhlobo losetyenziso. Naphi na apho usebenzisa uhlobo (lubandakanya iindidi kwizibhengezo, iigenerics, kunye neecast), ungakhapha oko ngesichasiselo.

Uhlobo lweSintaksi yeSihlomelo

I-Java 8 inokubhengeza uhlobo lwenkcazo kulo naluphi na uhlobo lokusetyenziswa. Omnye umzekelo sisiqwengana esingezantsi:

@Idatha yomtya ofihliweyo; Uluhlu <@NonNull String> imitya; myGraph = ( @Igrafu engenakuguquleka ) tmpGraph; Ungazisa ngokulula uhlobo olutsha lwenkcazo. Inkqubo iyafana nokuchaza isichasiselo nge-ElementType.TYPE_PARAMETER ekujoliswe kuyo, i-ElementType.TYPE_USE ekujoliswe kuyo, okanye zombini iithagethi: @Target ( { ElementType.TYPE_PARAMETER, ElementType.TYPE_USE } ) kawonke-wonke @i-interface Efihliweyo { }

I-ElementType.TYPE_PARAMETER itekeni ibonisa ukuba ungabhala inkcazo kwisibhengezo sodidi oluguquguqukayo (umzekelo, iklasi yam {….}). I-Element.Type.TYPE_USE ibonisa ukuba ungabhala isichasiselo kulo naluphi na uhlobo losetyenziso (oluzintlobo zezibhengezo, iigenerics, kunye neecast).

Ungagcina amanqakwana kwiifayile zeklasi, kodwa oko akuchaphazeli okanye kuphembelele ukwenziwa kwenkqubo. Njengomzekelo, ungabhengeza iinguqu ezimbini zeFayile kunye noqhagamshelo njengoko kubonisiwe kwikhowudi engezantsi:

Ifayile yefayile = ….; @IFayile Efihliweyo i-encryptedFile = …; @Vula uQhagamshelwano loQhagamshelwano = …; Ngelixa usenza udweliso lwenkqubo, isiphumo siya kufana ukuba udlula nayiphi na yeefayile ezimbini kwindlela yokuthumela () yoqhagamshelwano. uxhumano.thumela(ifayile); connection.send(encryptedFile);

 

Eminye iMizekeliso yoHlobo lwamanqaku

@nonNull Uluhlu Uluhlu <@NonNull String> str Arrays <@NonNegative Integer> hlela @Ifayile yeFayile eKhuselweyo @Vula uQhagamshelwano loQhakamshelwano olungekhoyo ulwahluloInteger (int a, int b) iphosa @ZeroDivisior ArithmeticException

 

Ushiye uluvo

Le sayithi isebenzisa i-Akismet ukunciphisa ugaxekile. Funda indlela idatha yakho yokubhaliweyo isetyenziswe ngayo.