Im plementasi methods

5.2 Im plementasi methods

5.2.1 s tatic methods

Method dapat di overload, a rtinya satu nama method bisa dipakai berkali-kali selama dia memiliki sesuatu yang unik. Sesuatu yang unik pada method tersebut dapat terdiri dari banyak nya parameter, tipe p arameter atau modifier parameter yang berbeda. Contoh nya seperti dibawah ini :

c l as s Tes t { s t at i c v oi d F( ) { Cons ol e. Wr i t eLi ne( " F( ) " ) ; } s t at i

c v oi d F( obj ec t o) { Cons o l e. Wr i t eLi ne( " F( obj ec t ) " ) ; } s t at i c v o i d F( i nt v al ue) {

Cons ol e . Wr i t eLi ne( " F( i nt ) " ) ; } s t at i c v oi d F( r ef i nt v al ue) {

Cons ol e. Wr i t eLi ne( " F( r ef i nt ) " ) ; } s t at i c v oi d F( i nt a, i nt b) {

Cons ol e . Wr i t eLi ne( " F( i nt , i nt ) " ) ; } s t at i c v oi d F( i nt [ ] v al ues ) {

Cons ol e. Wr i t eLi ne ( " F( i nt [ ] ) " ) ; } sta t i c v oi d Mai n( ) { F( ) ; F( 1) ;

Class diatas me miliki nama method F tapi memiliki jumlah, tipe dan modifier yang berbeda. Output d ari class diatas jika di eksekusi adalah :

F( ) F( i n t) F( r ef i nt ) F( obj ec t ) F( i nt , i nt ) F( i nt [ ] )

C# dapat mengembalikan banyak nilai dar i satu method dengan menggunakan out par ameter. Contoh nya seperti ini :

Us i ng Sy s t em;

Publ i c

c l as s Out Tes t { Publ i c s t at i c i nt Out pu t ( out i nt a) {

a = 25;

r et ur n 0; }

publ i c s t at i c v oi d mai n ( ) {

i nt a;

Cons ol e. Wr i t eLi ne ( Out put ( out a) ) ;

Cons ol e. Wr i t eLi ne ( a) ; } }

hasilnya adalah :

static method juga dapat mengembalikan lebih dari satu variabel dengan cara menggunakan keyword params. Contohnya adalah seperti ini :

us i ng Sy s t em; publ i c c l a s s Par ams (

publ i c s t at i c v oi d Par amet er ( par ams i nt [ ] l i s t ) {

f or ( i nt x = 0; x < l i s t . Lengt h ; x ++) Cons ol e. Wr i t eLi ne( l i s t [ x ] ) ; Cons ol e. Wr i t eLi ne( );

publ i c s t at i c v oi d M ai n( ) {

Par amet er ( 10, 15 , 20) ; } }

5.2.2 n on-static method

Telah dijela skan sebelu mnya bahwa static method dapat diakses hanya melalui class sedang kan non-static method dapat dia kses melalui instance.

Kita ak an mencoba melihat bagaimana implementasi non-static method dibandingkan dengan static method.

Buat satu cons e app ol lication project baru. ketikan di dalam class1 seperti ini (copy – past e saja semua code dibawah ini kedalam class1. cs) :

us i ng Sy s t em;

names pac e nons t at i c { names pac e nons t at i c {

{ Cons ol e. Wr i t eLi ne( " f i r s t one" ) ;

c l as s Sec ond: Fi r s t {

p ubl i c ov er r i de v oi d one( ) {

C ons ol e. Wr i t eLi ne( " Sec ond one" ) ;

/// <s ummar y > / / / Summar y d es c r i pt i on f or Cl as s 1. /// </ s ummar y >

c l as s Out put { /// <s ummar y > / / / The mai n en t r y poi nt f or t he appl i c at i on. // / </ s ummar y > [ STAThr ead] st at i c v oi d Mai n( )

{ // / / TODO: Add c ode t o s t ar t appl i c at i on her e //

Sec ond y = new Sec ond( ) ;

Fi r s t x = y; x . one( ) ;

y . one( ) ;

output code diatas m enjadi : s ec ond one s ec ond one

Output ini terjadi karena objek x sudah di-instance oleh objek y dimana ob jek y adalah class second yang turunan dari class First. (Penjelasan override dan virtual dapat dibaca pada materi Object Oriented).

jika class First dan class Second kita ubah seperti ini : cl as s Fi r s t {

publ i c v oi d one( ) { Cons ol e. Wr i t eLi ne( " f i r s t one" ) ; }

c l as s Sec ond: F irst { publ i c v oi

d one2( )

Cons ol e. Wr i t eLi ne( " Sec ond one" ) ; } }

Maka output code yang dihasilkan adalah : Fi r s t one Fi r s t one

Output ini dihasilkan karena perubahan yang dilakukan menyebabkan class Second menjadi m emiliki 2 non-static method yaitu method one dan method one2.

L alu, jika class First dan class Second kita ubah lagi seperti ini :

c l as s Fi rst { publ i c s t at i c v oi d one( ) { Cons ol e. Wr i t eLi ne( " f i r s t one" ) ; } }

c l as s Sec ond: Fi r s t { publ i c v oi d onen( ) {

C ons ol e. Wr i t eLi ne( " Sec ond one" ) ;

Maka akan muncul eror seperti ini : Static memb er 'nonstatic.First.one()' cannot be accessed with an instance reference; qualify it with a type name instead Static m ember 'nonstatic.First.one()' cannot be accessed with an instance reference; qualify it w ith a type name i nstead

Eror ini memperlihatkan bahwa static method tidak dapat diakses mengg unakan instance .