Osnove programskog jezika C# Čas 5. Delegati, događaji i interfejsi
DELEGATI
Bezbedni pokazivači na funkcije Jer garantuju vrednost deklarisanog tipa. Prevodilac prijavljuje grešku ako pokušate da povežete funkciju koja ne odgovara deklarisanom tipu delegata. public delegate <tip> Metoda (<parametri>)
Rad sa različitim metodama
Kreiranje vrednosti
Višeznačni delegati
Event posebna promenljiva tipa delegata Objekat koristi događaj da bi obavestio drugi objekat da se nešto desilo. Događaji su objekti tipa delegata: public event imedelegata imedogađaja Tip događaja
Primer i pitanja Pritiskom na dugme form2 otvara se forma2 sa samo jednom labelom. Pritiskom na dugme boje menja se boja forme. Koristeći delegate i događaje omogućiti da labela sadrži RGB komponente boje koja je na glavnoj formi.
Zadatak za vežbanje: Napisati aplikaciju koja može da otvori više identičnih formi sa jedne roditeljske. Sve novootvorene forme imaju jedno polje za unos teksta (textbox). Roditeljsak forma ima jedno dugme za otvaranje forme i onoliko labela koliko ima otvorenih formi iz nje. U svakoj labeli treba da bude naziv novootvorene forme i tekst u textbox-u te formme. Problem rešiti koristeći događaje.
IZUZECI. BLOK try, catch, finally
System.Exception
Izbacivanje izuzetaka
HIJERARHIJA
BLOK finally Blok koda koji će uvek biti izvršen bez obzira na to da li u ispitnom bloku nastaje izuzetak ili ne. finally blok će se uvek izvršiti.
Random Predstavlja generator pseudo-slučajnih brojeva. Konstruktori: Random (Int32) Random () Initializes a new instance of the Random class, using a time-dependent default seed value.
Generisanje slučajnih celobrojnih vrednosti Random.Next () Returns a nonnegative random number. Random.Next (Int32) Returns a nonnegative random number less than the specified maximum. Random.Next (Int32, Int32) Returns a random number within a specified range.
Generisanje slučajnih vrednosti od 0.0 do 1.0 public virtual double NextDouble () A double-precision floating point number greater than or equal to 0.0, and less than 1.0.
Rezultati testa FixedSeedRandoms(123); FixedSeedRandoms(123); FixedSeedRandoms(456); FixedSeedRandoms(456); Random numbers from a Random object with seed = 123: 2114319875 1949518561 1596751841 1742987178 1586516133 103755708 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146 Random numbers from a Random object with seed = 123: 2114319875 1949518561 1596751841 1742987178 1586516133 103755708 0.01700087 0.14935942 0.19470390 0.63008947 0.90976122 0.49519146 Random numbers from a Random object with seed = 456: 2044805024 1323311594 1087799997 1907260840 179380355 120870348 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170 Random numbers from a Random object with seed = 456: 2044805024 1323311594 1087799997 1907260840 179380355 120870348 0.21988117 0.21026556 0.39236514 0.42420498 0.24102703 0.47310170
Zadatak za razmišljanje Koristeći random generator osmisliti i realizovati zaštitni koder i dekoder za snimanje/prenos podataka.
PRIMER RandomObjectDemo.cs
Interfejsi Sličan klasi ali,... Njegova jedina svrha je da deklariše skup metoda a ne da ih realizuje. Interfejsi su dakle apstraktne klase koje sadrže samo potpuno virtuelne metode. Ne sadrži polja! Pristup je javan public. Ako sadrži svojstvo klasa mora realizovati bar jednu od metoda svojstva get ili set
Definisanje ponašanja je zadatak klasa (struktura) koje nasleđuju interfejs.
Interfejsi
Korišćenje interfejsa
Još o nizovima na drugi način Deklarišući niz, pravite novi tip podataka izveden iz klase System.Array.
System.Array Apstraktna klasa i ne možete da pravite objekte ove klase osim......koristeći statičku metodu CreateInstance()
Pretraživanje niza - IndexOf Array.IndexOf (Array, Object) Searches for the specified object and returns the index of the first occurrence within the entire one-dimensional Array. Array.IndexOf (Array, Object, Int32) Searches for the specified object and returns the index of the first occurrence within the range of elements in the onedimensional Array that extends from the specified index to the last element. Array.IndexOf (Array, Object, Int32, Int32) Searches for the specified object and returns the index of the first occurrence within the range of elements in the onedimensional Array that starts at the specified index and contains the specified number of elements. Array.IndexOf (T[], T) Searches for the specified object and returns the index of the first occurrence within the entire Array. Array.IndexOf (T[], T, Int32) Searches for the specified object and returns the index of the first occurrence within the range of elements in the Array that extends from the specified index to the last element. Array.IndexOf (T[], T, Int32, Int32) Searches for the specified object and returns the index of the first occurrence within the range of elements in the Array that starts at the specified index and contains the specified number of elements.
Pretraživanje niza - LastIndexOf Array.LastIndexOf (Array, Object) Searches for the specified object and returns the index of the last occurrence within the entire one-dimensional Array. Array.LastIndexOf (Array, Object, Int32) Searches for the specified object and returns the index of the last occurrence within the range of elements in the onedimensional Array that extends from the first element to the specified index. Array.LastIndexOf (Array, Object, Int32, Int32) Searches for the specified object and returns the index of the last occurrence within the range of elements in the one-dimensional Array that contains the specified number of elements and ends at the specified index.. Array.LastIndexOf (T[], T) Searches for the specified object and returns the index of the last occurrence within the entire Array. Array.LastIndexOf (T[], T, Int32) Searches for the specified object and returns the index of the last occurrence within the range of elements in the Array that extends from the first element to the specified index. Array.LastIndexOf (T[], T, Int32, Int32) Searches for the specified object and returns the index of the last occurrence within the range of elements in the Array that contains the specified number of elements and ends at the specified index.
Sortiranje niza Array.Sort (Array) Sorts the elements in an entire one-dimensional Array using the IComparable implementation of each element of the Array. Array.Sort (Array, Array) Sorts a pair of one-dimensional Array objects (one contains the keys and the other contains the corresponding items) based on the keys in the first Array using the IComparable implementation of each key. Array.Sort (Array, IComparer) Sorts the elements in a one-dimensional Array using the specified IComparer. Array.Sort (Array, Array, IComparer) Sorts a pair of one-dimensional Array objects (one contains the keys and the other contains the corresponding items) based on the keys in the first Array using the specified IComparer. Array.Sort (Array, Int32, Int32) Sorts the elements in a range of elements in a one-dimensional Array using the IComparable implementation of each element of the Array. Array.Sort (Array, Array, Int32, Int32) Sorts a range of elements in a pair of one-dimensional Array objects (one contains the keys and the other contains the corresponding items) based on the keys in the first Array using the IComparable implementation of each key. Array.Sort (Array, Int32, Int32, IComparer) Sorts the elements in a range of elements in a one-dimensional Array using the specified IComparer. Array.Sort (Array, Array, Int32, Int32, IComparer) Sorts a range of elements in a pair of onedimensional Array objects (one contains the keys and the other contains the corresponding items) based on the keys in the first Array using the specified IComparer.
PRIMER SamplesArray.cs
Nema više......za danas.