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! Finally I got a website from where  I can genuinely take useful information regarding my study and knowledge.
Wow! Finally I got a website from where I can genuinely take useful information regarding my study and knowledge. - sobota, 21. december 2024

Wow! Finally I got a website from where I can genuinely take useful information regarding my study and knowledge.

Keep on working, great job!
Keep on working, great job! - petek, 20. december 2024

Keep on working, great job!

Hello, this weekend is pleasant designed for me, since this time i am reading this great informative post here at  my house.
Hello, this weekend is pleasant designed for me, since this time i am reading this great informative post here at my house. - torek, 17. december 2024

Hello, this weekend is pleasant designed for me, since this time i am reading this great informative post here at my house.

Thanks very interesting blog!
Thanks very interesting blog! - ponedeljek, 16. december 2024

Thanks very interesting blog!

Ridiculous quest there. What occurred after? Thanks!
Ridiculous quest there. What occurred after? Thanks! - četrtek, 12. december 2024

Ridiculous quest there. What occurred after? Thanks!

If you want to obtain a great deal from this piece of writing then you have to apply  these methods to your won webpage.
If you want to obtain a great deal from this piece of writing then you have to apply these methods to your won webpage. - nedelja, 24. november 2024

If you want to obtain a great deal from this piece of writing then you have to apply these methods to your won webpage.

I constantly spent my half an hour to read this web site's posts daily along with a cup of coffee.
I constantly spent my half an hour to read this web site's posts daily along with a cup of coffee. - četrtek, 21. november 2024

I constantly spent my half an hour to read this web site's posts daily along with a cup of coffee.

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.