有两个类 一个父类 一个子类, 子继承父
 A对象在申明的时候 申明为父的对象, 在构造(creat)的时候能构造成子类的对象吗?
Type
TFather=Class
A: Integer;
End;
Tson=Class(TFather)
A: string;
End;
Var
  Myboject: TFather;
Begin
 Mobject:=Tson.Create;//(请问这里MYobject可以这样构造吗?)

解决方案 »

  1.   

    当然不可能,对象只能构造成自身,你这种情况应该定义成指针Type TFather=Class 
           A: Integer; 
    End; 
    Tson=Class(TFather) 
           A: string; 
    End; Var 
      Myboject: ^TFather; 
    Begin 
     Mobject:=Tson.Create;//
    PASCAL语法不熟悉,大概意思这样
      

  2.   

    那为什么有这段代码呢?  
    Type 
      TFatheer =Class
       i:Integer;
      End;
    TSon =Class(TFather)
      i:string;
      End;
     VAR 
       Myobject : TFather;
     begin 
       Myonject :=TSon.Creat;
       Myobject.i:='hello';//错误
       TSon(Myobject).i:='hello';//正常
     
      

  3.   

    那为什么有这段代码呢?   
    Type  
      TFatheer =Class 
       i:Integer; 
      End; 
    TSon =Class(TFather) 
      i:string; 
      End; 
     VAR  
       Myobject : TFather; 
     begin  
       Myonject :=TSon.Creat; 
       Myobject.i:='hello';//错误 
       TSon(Myobject).i:='hello';//正常 
      

  4.   

    应该声明父类指针,然后构造子类对象,然后把子类对象的地址赋给父类指针。楼主为什么跑这儿来写了一段delphi的代码,是不是发错地方了?