Welcome to Abdul Malik Ikhsan's Blog

Cara Extends Kelas di Delphi

Posted in Tutorial Delphi by samsonasik on October 3, 2007

Saya contohkan kita punya kelas manusia, dan kelas mahasiswa yang merupakan keturunan manusia :
Cara membuat kelas manusia, klik file – > new -> Unit, simpan dengan nama umanusia.pas

Code:
unit umanusia;
interface
type manusia = class
private
makan,minum,nama,alamat:String;
public
constructor manusia(x:String;y:String);
procedure setnama(n:String);
procedure setalamat(n:String);
function getnama:String;
function getalamat:String;
function getnyam2:String;
end;
implementation
constructor manusia.manusia(x:String;y:String);
begin
makan:=x;
minum:=y;
end;
procedure manusia.setnama(n:String);
begin
nama:=n;
end;
procedure manusia.setalamat(n:String);
begin
alamat:=n;
end;
function manusia.getnama:String;
begin
result:=nama;
end;
function manusia.getalamat:String;
begin
result:=alamat;
end;
function manusia.getnyam2:String;
begin
result:=’makan ‘+makan+’ dan minum ‘+minum;
end;
end.


———————–
Sekarang, kita buat kelas mahasiswa yang merupakan turunan dari manusia : –> umahasiswa

Code:
unit umahasiswa;
interface
uses umanusia;
type mahasiswa = class(manusia)
public
function belajar:String;
end;
implementation
function mahasiswa.belajar:String;
begin
result:=’harus rajin banget lho ‘;
end;end.


————
Now, kita bisa panggil kelas turunan tersebut di form, misalkan form1, unit1.pas

Code:

unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,umahasiswa,StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Label1: TLabel;
Edit2:TEdit;
procedure FormActivate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
o:mahasiswa;

implementation

{$R *.DFM}

procedure TForm1.FormActivate(Sender: TObject);
begin
o:=mahasiswa.manusia(‘sate’,’es teh’);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
o.setnama(edit1.Text);
o.setalamat(edit2.text);
label1.caption:=o.getnama+’ beralamat di ‘+o.getalamat+’ sedang ‘+o.getnyam2+’ dan habis itu belajar ‘+o.belajar;
end;

end.

Advertisements

2 Responses

Subscribe to comments with RSS.

  1. AngeL said, on February 15, 2011 at 12:52 am

    Mas, bisa bantu tutoriaL bwt project pake TmxProtector?

  2. samsonasik said, on February 16, 2011 at 3:20 pm

    saya belum bernah mencoba, mungkin nanti jika sudah mencoba akan saya publish 🙂


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: