type
TStaTime = class(TThread)
Fr:Tobject;
protected
procedure Execute; override;
procedure TStaTime.Execute;
begin
Fr:=Fr as Fr.className
//或:Fr:=Fr as Fr.ClassType
Fr.mothed1;
end;
end.怎么编译都通不过呢?
TStaTime = class(TThread)
Fr:Tobject;
protected
procedure Execute; override;
procedure TStaTime.Execute;
begin
Fr:=Fr as Fr.className
//或:Fr:=Fr as Fr.ClassType
Fr.mothed1;
end;
end.怎么编译都通不过呢?
Classes that implement interfaces can use the as operator for dynamic binding on the interface. In the following example:procedurePaintObjects(P: TInterfacedObject)var
X: IPaint;begin X := P as IPaint;
{ statements }
end;the variable P of type TInterfacedObject, can be assigned to the variable X, which is an IPaint interface reference. Dynamic binding makes this assignment possible. For this assignment, the compiler generates code to call the QueryInterface method of P抯 IInterface interface. This is because the compiler cannot tell from P抯 declared type whether P抯 instance actually supports IPaint. At runtime, P either resolves to an IPaint reference or an exception is raised. In either case, assigning P to X will not generate a compile-time error as it would if P was of a class type that did not implement IInterface.When you use the as operator for dynamic binding on an interface, you should be aware of the following requirements:Explicitly declaring IInterface: Although all interfaces derive from IInterface, it is not sufficient, if you want to use the as operator, for a class to simply implement the methods of IInterface. This is true even if it also implements the interfaces it explicitly declares. The class must explicitly declare IInterface in its interface list.
Using an IID: Interfaces can use an identifier that is based on a GUID (globally unique identifier). GUIDs that are used to identify interfaces are referred to as interface identifiers (IIDs). If you are using the as operator with an interface, it must have an associated IID. To create a new GUID in your source code you can use the Ctrl+Shift+G editor shortcut key.
findclass
最好还是用判断吧
procedure TForm1.Test(o: TObject);
begin
o := o as o.ClassType;
ShowMessage(o.ClassName);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
Test(Form1);
end;
还有,你的那段有帮助的代码有什么用?procedure TForm1.Test(o: TObject);
begin
o := o as o.ClassType;//这不是多此一举吗?
ShowMessage(o.ClassName);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
Test(Form1);
end;
begin
o := o as o.ClassType;//这不是多此一举吗?
ShowMessage(o.ClassName);
end;procedure TForm1.Button1Click(Sender: TObject);
begin
Test(Form1);//这里应该 Test(self);
end;
type
TStaTime = class(TThread)
Fr:TClass;
protected
procedure Execute; override;
procedure TStaTime.Execute;
begin
Fr := 你需要强制转换的类.ClassType;
你需要强制转换的类(Fr).mothed1;
end;
end
FindClass('Tfr') 返回实例
你的Fr是怎么创建的,是什么类型就用什么类型去转换啊。
Fr:=TForm.Create那么,Fr的类名一定是TForm啊。
TFormClass = class of TForm;procedure TfrmMain.myCreateForm(FormClass: TFormClass; FormObj: TForm);
var
i: integer;
Found: Boolean;
begin
try
Found := false;
for i := 0 to self.MDIChildCount - 1 do
if self.MDIChildren[i].Name = FormObj.Name then
begin
Found := true;
Break;
end;
if not Found then
FormObj := FormClass.Create(Application);
FormObj.Show;
except
end;
end;调用:
...
myCreateForm(TForm2, form2);
...希望对你有帮助。