Burada yanlış bir anlaşılma oldu sanırım. Temel sınıfta da bu metod gayet hoş bir biçimde kullanılabiliyor. Yeter ki bunu kullanan
nesne o temel sınıftan oluşturulmuş olmasın.
Kod: Tümünü seç
type
TAritmetik=class
function islem(a,b:Integer):Integer;virtual;abstract;
function Hesapla:Integer;
end;
TTopla=class(TAritmetik)
function islem(a,b:Integer):Integer;override
end;
TCikar=class(TAritmetik)
function islem(a,b:Integer):Integer;override
end;
TCarp=class(TAritmetik)
function islem(a,b:Integer):Integer;override
end;
function TAritmetik.Hesapla:Integer;
begin
Result:=islem(15,5);
end;
function TTopla.islem(a,b:Integer):Integer;
begin
Result:=a+b;
end;
function TCikar.islem(a,b:Integer):Integer;
begin
Result:=a-b;
end;
function TCarp.islem(a,b:Integer):Integer;
begin
Result:=a*b;
end;
sınıfları bu şekilde düzenledikten sonra örnek olarak temel sınıftaki Hesapla metodunu kullanmak gerekirse
Kod: Tümünü seç
var a:TAritmetik;
begin
if CheckBoxTopla.Checked then a:=TTopla.Create
else if CheckBoxCikar.Checked then a:=TCikar.Create
else if CheckBoxCarp.Checked then a:=TCarp.Create
ShowMessage(IntToStr(a.Hesapla);
a.free
end;
Burada hesapla metodu sadece temel sınıfa aittir ve bu da islem metodunu çağırır. Tabi oluşturulan nesneye göre bu metod farklı çalışacağı için seçime göre farklı sonuçlar verecektir. Yeterki
şeklinde kullanılmasın. Çünkü TAritmetik sınıfında böyle bir metod (islem) abstract (soyut) olduğundan tanımlanmış her hangi bir kodu bulunmamaktadır.