C Sharp/Programowanie obiektowe: Różnice pomiędzy wersjami

Usunięta treść Dodana treść
Rozbudowa
m Ustandardyzowanie, porpawienie niewielkich błędów, utworzenie odstępów (tabulatorów, zamiast zpacji)
Linia 15:
class Klasa
{
public const int x = 5;
public const string y = "string";
}
class Program
{
static void Main()
{
//Prawidłowe odwołanie się do stałej
Console.WriteLine(Klasa.y);
Klasa c = new Klasa();
//Poniższa instrukcja spowoduje błąd
//Dostęp do stałej za pośrednictwem obiektu jest niemożliwy
Console.WriteLine(c.y);
}
}
</source>
Linia 42:
[atrybut] <modyfikator> <typ danych> <nazwa>
{
[modyfikator] get
{
......
return(wartość właściwości)
}
[modyfikator]set
{
//kod zapisujący do pola wartość reprezentowaną przez zmienną value
}
}
</source>
Linia 66:
class Klasa
{
int x;
int y;
void showShow()
{
Console.WriteLine("X:"+this.x+"Y:"+this.y);
}
}
</source>
Linia 84:
class Klasa
{
int x;
int y;
public Klasa(int x, int y)
{
this.x = x;
this.y = y;
}
void showShow()
{
Console.WriteLine("X:"+this.x+"Y:"+this.y);
}
}
</source>
Linia 107:
class Klasa
{
int x;
int y;
public Klasa(int x,int y)
{
this.x = x;
this.y = y;
}
void showShow()
{
Console.WriteLine("X:"+this.x+"Y:"+this.y);
}
}
 
class Klasa2: Klasa //Klasa2 dziedziczy po Klasa
{
int x, y, z;
public Klasa(int x,int y,int z)
{
this.x = x;
this.y = y;
this.z = z;
}
void showShow()
{
Console.WriteLine("X:"+this.x+"Y:"+this.y+"Z"+this.z);
}
}
</source>
Linia 141:
 
Należy jeszcze wywołać konstruktor:
<source lang=csharp>obiekt = new Klasa();</source>
 
Można to połączyć w jeden zapis:
<source lang=csharp>Klasa obiekt = new Klasa();</source>
 
{{prognaw|C_Sharp|[[../Instrukcje sterujące/]]|[[../Tablice/]]}}