DELPHI 虚拟继承示例

评价:
0
(0用户)

type
t_object = class(tobject) //基类
private
public
m_int_age : integer;
m_str_name : string;
procedure setage(p_int_age : integer) ; virtual;
end;

t_child = class(t_object)
private
public
procedure setage(p_int_age : integer) ; override;
end;

t_child_child = class(t_child)
private
public
procedure setage(p_int_age : integer) ; override;
end;

implementation

procedure t_object.setage(p_int_age : integer);
begin
self.m_int_age := p_int_age;
showmessage('t_object.setage(p_int_age : integer)—' + inttostr(self.m_int_age));
end;

procedure t_child.setage(p_int_age : integer);
begin
inherited setage(p_int_age);
self.m_int_age := p_int_age + 20;
showmessage('t_child.setage(p_int_age : integer)—' + inttostr(self.m_int_age));
end;

procedure t_child_child.setage(p_int_age : integer);
begin
inherited setage(p_int_age);
self.m_int_age := p_int_age + 40;
showmessage('t_child_child.setage(p_int_age : integer)—' + inttostr(self.m_int_age));
end;

end.

//调用
var
obj : t_child_child;
begin
obj := t_child_child.create;
obj.setage(10);
obj.free;
end;

 

type
t_object = class(tobject) //基类
private
public
m_int_age : integer;
m_str_name : string;
procedure setage(p_int_age : integer) ; virtual;
end;

t_child = class(t_object)
private
public
procedure setage(p_int_age : integer) ; override;
end;

t_child_child = class(t_child)
private
public
procedure setage(p_int_age : integer) ; override;
end;

implementation

procedure t_object.setage(p_int_age : integer);
begin
self.m_int_age := p_int_age;
showmessage('t_object.setage(p_int_age : integer)—' + inttostr(self.m_int_age));
end;

procedure t_child.setage(p_int_age : integer);
begin
inherited setage(p_int_age);
self.m_int_age := p_int_age + 20;
showmessage('t_child.setage(p_int_age : integer)—' + inttostr(self.m_int_age));
end;

procedure t_child_child.setage(p_int_age : integer);
begin
inherited setage(p_int_age);
self.m_int_age := p_int_age + 40;
showmessage('t_child_child.setage(p_int_age : integer)—' + inttostr(self.m_int_age));
end;

end.

//调用
var
obj : t_child_child;
begin
obj := t_child_child.create;
obj.setage(10);
obj.free;
end;

注册并通过认证的用户才可以进行评价!

发表评论