Tlanya Litlhaloso ho Java

Tlanya Litlhaloso ho Java

Ka kenyelletso ea Java 8, phetolelo e hlahisitse likarolo tse peli tse ncha tse bitsoang ho pheta-pheta litlhaloso le litlhaloso tsa mofuta. Ho fihlela nakong ena, haeba u ne u batla ho sebelisa litlhaloso ho Java, u ne u ka etsa joalo feela liphatlalatsong. Ka Java 8, joale o ka eketsa litlhaloso ho mofuta ofe kapa ofe oa tšebeliso. Kae kapa kae moo o sebelisang mofuta (ho kenyelletsa mefuta ea liphatlalatso, li-generic, le li-cast), o ka tsamaisana le seo ka tlhaloso.

Type Annotation Syntax

Java 8 e ka phatlalatsa litlhaloso tsa mofuta ofe kapa ofe oa tšebeliso. Mohlala o mong ke snippet e ka tlase:

Lintlha tsa @Encrypted String; Lenane <@NonNull String> likhoele; myGraph = ( @Immutable Kerafo ) tmpGraph; U ka hlahisa mofuta o mocha oa tlhaloso. Ts'ebetso e tšoana le ho hlalosa tlhaloso e nang le ElementType.TYPE_PARAMETER target, ElementType.TYPE_USE target, kapa sepheo sa bobeli: @Target ( { ElementType.TYPE_PARAMETER, ElementType.TYPE_USE } ) public @interface Encrypted { }

The ElementType.TYPE_PARAMETER target e bolela hore o ka ngola tlhaloso ho phatlalatso ea mofuta o fapaneng (mohlala, sehlopha sa MyClass {….}). The Element.Type.TYPE_USE e bolela hore o ka ngola litlatsetso tsa mofuta ofe kapa ofe oa tšebeliso (e leng mefuta ea liphatlalatso, li-generics, le li-cast).

U ka boloka litlhaloso lifaeleng tsa sehlopha, empa ha e ame kapa hona ho ama ts'ebetso ea lenaneo. E le mohlala, o ka phatlalatsa mefuta e 'meli ea Faele le khokahano joalo ka ha ho bonts'itsoe khoutu e ka tlase:

Faele ea faele = ....; @Encryted File encryptedFile = …; @Open Connection connection = …; Ha u ntse u phethahatsa lenaneo, sephetho se tla tšoana haeba u fetisetsa e 'ngoe ea lifaele tse peli ho mokhoa oa ho romella () oa khokahanyo. kgokelo.romela(faele); khokahano.romela(encryptedFile);

 

Mehlala e meng ea Litlhaloso tsa Mofuta

Lethathamo la @nonNull Lenane <@NonNull String> str Arrays <@NonNegative Integer> hlopha @Encypted File file @Open Connection connection void divideInteger (int a, int b) throws @ZeroDivisior ArithmeticException

 

Leave ka tlhaloso

Sebaka sena se sebelisa Akismet ho fokotsa spam. Ithute kamoo litlhaloso tsa hau li hlahang kateng.