一个对象中包含多个其他对象,成员对象的属性不能赋值(100)

  • 主题发起人 富翁中的穷人
  • 开始时间

富翁中的穷人

Unregistered / Unconfirmed
GUEST, unregistred user!
我的对象需要从多个对象继承,这个通过一般的父类解决有点麻烦,于是我想用类中再定义类型也是类的成员解决,例如:作为一个儿子,有父亲和母亲分别是Tson,TFather,Tmother要执行的操作是: son:=Tson.Create
son.Father.Name:='ABC'
// ==>出错 Son.Mother.Name:='DEF';到第2句就出错了。类定义如下:TFather=Class private _Name:string
published property Name:string Read _Name Write _Name
end
TMother=Class private _Name:string
published property Name:string Read _Name Write _Name
end
TSon=class Private _Name:string
_Father:TFather
_Mother:TMother
published property Name:string Read _Name Write _Name
property Father:TFather Read _Father Write _Father
property Mother:TMother Read _Mother Write _Mother
end;
 
L

liuls

Unregistered / Unconfirmed
GUEST, unregistred user!
PurchaseOrder1.Client.OrderSubNo:=1 就会出错。===>首先你要保证 PurchaseOrder1 <> nil, 再保证 PurchaseOrder1.Client <> nil。
 

富翁中的穷人

Unregistered / Unconfirmed
GUEST, unregistred user!
purchaseorder1肯定是已经Create了的。但Purchaseorder1.client我没有去create,对象purchase生成时其.client是不是应该也同时生成了啊。
 

富翁中的穷人

Unregistered / Unconfirmed
GUEST, unregistred user!
谢谢Liuls。问题还得不到解决。我的想法是我的对象需要从多个对象继承。例如:作为一个人,有其父亲和母亲,三个对象分别是Tson,TFather,Tmother要执行的操作是: son:=Tson.Create
son.Father.Name:='ABC'
// ==>出错 Son.Mother.Name:='DEF';到第2句就出错了。类定义如下:TFather=Class private _Name:string
published property Name:string Read _Name Write _Name
end
TMother=Class private _Name:string
published property Name:string Read _Name Write _Name
end
TSon=class Private _Name:string
_Father:TFather
_Mother:TMother
published property Name:string Read _Name Write _Name
property Father:TFather Read _Father Write _Father
property Mother:TMother Read _Mother Write _Mother
end;
 
F

fsse4000

Unregistered / Unconfirmed
GUEST, unregistred user!
TSon中沒有構造兩個類 TSon=class(TObject) Private FFather:TFather
FMother:TMother
public constructor Create
destructor Destroy
override
published property Father:TFather Read FFather
property Mother:TMother Read FMother
end;constructor TSon.Create;begin inherited
FFather :=TFather.Create
FMother :=TMother.Create ;end;destructor TSon.Destroy;begin FFather.Free
FMother.Free
inherited;end;
 
H

happycyp

Unregistered / Unconfirmed
GUEST, unregistred user!
不要将它们作为属性输出。直接用变量就不会错了。TSon=class Private _Name:string
published Father:TFather
Mother:TMother
property Name:string Read _Name Write _Name
end;
 
L

liuls

Unregistered / Unconfirmed
GUEST, unregistred user!
不是,要自己创建
 

富翁中的穷人

Unregistered / Unconfirmed
GUEST, unregistred user!
经试,fsse4000的解答完全正确。我原以为只要我创建了Son对象,其成员对象Mother与Father也会自动建立。谢谢。也非常感谢Happycyp和Liuls的解答。
 
顶部