Abstrakten razred

Ce jz naredim razred abstrakten, to pomeni, da ne morem naredit instance tega razreda, ne? Kaj mi to koristi, če lahko pole nardim takole finto:

public abstract class Proba
{
   public Proba(string _name)
   {
        this._name = _name;
   }

   private string  _name;
   public string  Name
   {
     get { return _name; }
   }   
}

public class ProbaTest : Proba
{
   public ProbaTest(string name) : base(name)
   {       
   }
}


in potem klicem takole:
ProbaTest test = new ProbaTest("Bojan 2");
((Proba)test).Name


sem mu explicitno povedal, naj mi poklice metodo iz Proba...
Zanima me predvsem, kaj tocno mi pol abstrakten razred koristi? Kdaj bi ga bilo pametno uporabljat?
A mi lahko to nekdo razjasni, ker mam malo zmesane pojme? Al je to sam finta, da ne podvajam kode?

Hvala!
Avtor: bojanv, objavljeno na portalu SloDug.si (Arhiv)

Leave a comment

Please note that we won't show your email to others, or use it for sending unwanted emails. We will only use it to render your Gravatar image and to validate you as a real person.

Wow, this paragraph is pleasant, my younger sister is analyzing such things, thus I am going to convey her.
Wow, this paragraph is pleasant, my younger sister is analyzing such things, thus I am going to convey her. - četrtek, 31. oktober 2024

Wow, this paragraph is pleasant, my younger sister is analyzing such things, thus I am going to convey her.

Hello to every , because I am really keen of reading  this blog's post to be updated daily. It consists of good information.
Hello to every , because I am really keen of reading this blog's post to be updated daily. It consists of good information. - ponedeljek, 07. oktober 2024

Hello to every , because I am really keen of reading this blog's post to be updated daily. It consists of good information.

fluethy
fluethy - petek, 30. avgust 2024

<a href=https://enhanceyourlife.mom/>priligy prescription</a> 0120132938 January 9, 2014 the Agency properly dismissed Complainant s complaint alleging discrimination with regard to a fully successful rating on one critical job element of her performance appraisal

bojanv
bojanv - ponedeljek, 20. februar 2006

&nbsp;&nbsp; Thnx...zdej mi je jasno....jz sem mislil, da ko cast naredis, da se prestavis en nivo nizje, ampak ocitno to pri override-anih metodah ne velja...edini dostop je potem mozen z base besedo...

MihaM
MihaM - ponedeljek, 20. februar 2006

Ko overridaš metode, se vedno (najprej*) izvede zgornja ne glede na cast (cast nikakor ne vpliva na vrstni red), torej v tvojem primeru ProbaTest.GetName(). Prav v tem je čar virtualnih/abstraktnih metod. najprej*...znotraj te metode seveda lahko kličeš base.Metoda() in potem se kliče metoda enega nivoja nižje.

bojanv
bojanv - ponedeljek, 20. februar 2006

Torej, ce sem prav razumel, ce naredim eno metodo abstraktno (brez body-a) in potem ta razred dedujem, morm implementirat to metodo, torej jo override-am. Kaj se pa pol zgodi, ko poklicem metodo in dam cast spredaj? Primer:public abstract class Proba{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public string _name;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public Proba( string name )&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this._name = name;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; public abstract string GetName();}class ProbaTest : Proba{&nbsp;&nbsp;&nbsp;&nbsp; public ProbaTest( string name )&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : base(name)&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp; public override string GetName()&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return _name;&nbsp;&nbsp;&nbsp;&nbsp; }}zdaj pa damProbaTest test = new ProbaTest("Bojan 2");((Proba)test).GetName()Kaj se zdaj poklice? Metoda, ki je implementirana v ProbaTest?

MihaM
MihaM - ponedeljek, 20. februar 2006

Abstrakten razred lahko razumeš v smislu: če hočem narediti instanco, moram podedovati razred. Recimo, da ima tvoj abstraktni razred neko abstraktno metodo, ki seveda ni implementirana. Hkrati pa to pomeni, da mora vsak podedovan razred implementirat tisto metodo (neke vrste zahteva). Torej, vsi podedovani razredi bodo implementirali metodo, ki jo abstraktni razred definira. Ker pa sam abstraktni razred te metode ne implementira ga pač nima smisla instancirat.