Professional Documents
Culture Documents
KLASE
KLASE
C#
► Klase (classes) su opisi koji definišu kako će se generisati objekti i koje
osobine i funkcionalnosti ti objekti poseduju.
► Klasa predstavlja strukturu u kojoj su grupisani podaci i funkcije, uključujući i
druge klase.
► Deklaracija klase
► class ime_klase : NazivOsnovneKlase
{
modifikator tip polje;
...
modifikator tip metoda(parametri);
...
}
►
Polje klase
► Za pristup članu sopstvene klase može se koristiti samo ime člana klase:
clan
► PRIVATE
The type or member can only be accessed by code in the same class or struct.
► PROTECTED
The type or member can only be accessed by code in the same class or struct, or in a derived class.
► INTERNAL
The type or member can be accessed by any code in the same assembly, but not from another assembly.
► PROTECTED INTERNAL
The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.
Enkapsulacija
}
}
class Program
{
class Krug
{
private int r;
public Krug() // Podrazumevani konstruktor
{
r = 0;
}
public double Povrsina()
{
return Math.PI * r * r;
}
public double Povrsina(int r)
{
return Math.PI * r * r;
}
}
static void Main(string[] args)
{
Krug kruzic = new Krug();
Console.WriteLine(kruzic.Povrsina());
//Console.WriteLine(kruzic.r); //neće da može r je private
int poluprecnik;
Console.WriteLine("Unesite poluprecnik kruga: ");
poluprecnik =Convert.ToInt32( Console.ReadLine());
Console.WriteLine(kruzic.Povrsina(poluprecnik)); //stampa povrsinu zavisno od promenljive poluprecnik
Console.WriteLine(kruzic.Povrsina()); //stampa povrsinu ponovo 0 } }
Razlike KLASE I STRUKTURE