Delphi Object-oriented learning essay two writing the first class

Source: Internet
Author: User

Bahamut
(reproduced please specify the source and remain intact)

This time, we discuss how to write our own first class.
Before we write our own classes, the first thing to say is "Inheritance of classes".
I remember the other day, when I was talking to a friend about the features of the class, he said, "the class is no way to construct it!" "The class must have at least one construction method, but his words are not all wrong and can be understood as" we can not implement our own construction method. "
When we do not explicitly write a constructor for a class, it seems like there is no constructor for this class, but in fact, even if you do not write a constructor of your own for this class, the class has a constructor; Because, when you select a parent class and derive (Inherit) a new descendant class from the parent class, first, the compiler copies a copy of the "inheritable" data member from the parent class into the new subclass.
So, what is an inheritable data member, let's look at the following code:

TPersistent = class(TObject)private  procedureAssignError(Source: TPersistent);protected  procedureAssignTo(Dest: TPersistent); virtual;  procedureDefineProperties(Filer: TFiler); virtual;  functionGetOwner: TPersistent; dynamic;public  destructorDestroy; override;  procedureAssign(Source: TPersistent); virtual;  functionGetNamePath: string; dynamic;end;

Above is the VCL internal Tpersistent class declaration code, we can see a few keywords private, protected, public, these are the key words that describe the access restrictions of data members, where:

private:     存在于该访问限制下的数据成员只能被本类所访问。protected:   存在于该访问限制下的数据成员只能被本类及后代类所访问。public:      存在于该访问限制下的数据成员可以被任何类访问。published:   存在于该访问限制下的数据成员的访问限制与public一样,但是在本域下的属性将会在注册类的时候被注册给IDE。automated:   存在于该访问限制下的数据成员为自动成员,常用于定义嵌入(OLE)自动化类型信息的公共接口。 PS: 该域并不常用,这里不做介绍

Thus, the so-called "inheritable" part refers to the data member specified in the "Private" field.
Back to the question above, because when inheriting a new class, the compiler will copy a copy of all the inheritable parts of the parent class into the new subclass, and when the constructor method is not written for the new class, the new class calls the constructor of the parent class, and if no constructor method is written in the parent class, then the layer is upstream. Until the root class TObject.
Like the class above, we can see that there is really no rewriting of the new construction method in the class, but the method of constructing this class is real.

Now we can start writing the first simple class that belongs to us:

type// 说明我们从这里开始往下要声明的是自定义的数据类型  TNewClass = class(TPersistent)    // TNewClass   新类的类名    // TPersistent 父类的类名  private    // 私有域数据成员  protected    // 保护域数据成员  public    // 公共域数据成员    procedure SayHello(); // 我们为类添加的新的方法  published  end; // 类的声名结束implementation// 实现部分procedureTNewClass.SayHello;  // SayHello 方法的实现部分begin  ShowMessage(‘Hello‘);end;

At this point, we have finished writing a simple class.
We can write a simple code to test how the class works:

var  NewClass: TNewClass; // 定义一个类 TNewClass 的对象 NewClassbegin  NewClass:= TNewClass.Create; // 为对象 NewClass 分配内存空间,并创建它  NewClass.SayHello(); // 调用 SayHello 方法  NewClass.Free; // 销毁对象并释放对象的内存空间end;

Sometimes, we see this way of declaring:

type  TNewClass = class    ...  end;

This is a way of omitting the declaration of the parent class, when the parent class is omitted, the Delphi compiler will tobject the root class as the base class of the new class, and

type  TNewClass = class(TObject)    ...  end;

As a result, here I recommend the following approach, as this will make the code clearer.

Delphi Object-oriented learning essay two writing the first class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.