Object 與 class 的區別
在 D2006中編譯DsPack2.34的時候,遇到以下代碼:
TBCRefTime = object
public
... //這裡省略了
end.
function TBCRefTime.SetTime(var ll: LONGLONG): TBCRefTime;
begin
FTime := ll;
end;
想問:= object 如何理解, = class 有什麼區別。
上面的代碼如果把 = object 改成 = class , 那麼在
function TBCRefTime.SetTime(var ll: LONGLONG): TBCRefTime;
這裡會有一個錯誤 :
[Pascal Error] BaseClass.pas(14756): E2064 Left side cannot be assigned to
在BCB2006中編譯 = object 會報錯。
[解决办法]
As an alternative to class types, you can declare object types using the syntax
type objectTypeName = object (ancestorObjectType)
memberList
end;
where objectTypeName is any valid identifier, (ancestorObjectType) is optional, and memberList declares fields, methods, and properties. If (ancestorObjectType) is omitted, then the new type has no ancestor. Object types cannot have published members.
Since object types do not descend from TObject, they provide no built-in constructors, destructors, or other methods. You can create instances of an object type using the New procedure and destroy them with the Dispose procedure, or you can simply declare variables of an object type, just as you would with records.
Object types are supported for backward compatibility only. Their use is not recommended.
[解决办法]
那么简单的E文还看不懂啊,看看FTime的定义吧
[解决办法]
在就的object pascal中面向对象类定义是用object
用object定义出来跟C++一样,也就是可以定义祖先类(真正没有父类的),新的一般从Tobject继承。即使Txxx=class,默认也是Tobject的子孙。
为了兼容,保留object。
这是我的理解,不知道是否有错误。
我也没怎么用过object
[解决办法]
record 只能有静态数据字段,而不能有过程方法函数及事件等
object则不受这些限制
[解决办法]
BCB和delphi定义类不一样, 默认都是从object定义,class是关键字