Talk to the object-oriented thinking - Talk about Delphi Development (Part III)

zhaozj2021-02-11  175

("Talking to the object-oriented thought - Talk about the third article of Delphi development)

The first two discussions are related to encapsulation. Here, I want to talk to you about inheritance and polymorphism.

Inheritance and polymorphism are closely related. Object Pascal introduces a polymorphic mechanism called overload, its thoughts and object-oriented relationships, not discussive here. Polygonations closely related to object-oriented thinking are important discussion.

Polymorphism depends on the concept of abstract methods and the virtual method, and is also closely related to inheritance. I thought that we often define some underlying objects, and then define their implementations as abstraction, that is, we only define interfaces without defining specific implementation details. According to this idea, we will define multiple derivatives (inheritance) objects, which truly realize the details that have not been implemented in the ancestral class. This makes our previously defined underlying classes with polymorphic characteristics. The advantage of this mechanism is that when we use these classes, as long as a set of code, you can complete a variety of functions. The only thing that needs to be changed is part of the instance of the object.

Observe such a class:

TSTREAM = Class (TOBJECT)

......

public

Function Read (var buffer; count: longint; virtual; abstentract;

Function Write (CONST BUFFER): Longint; Virtual; ABSTRACT

......

END;

Virual and Abstract reserves indicate that READ and WRITE methods are pure virtual functions. This indicates that TSTREAM class cannot be truly used (unable to create instances of this class), it is just a class similar to the interface, which defines the basic functions that should be equipped as the TSTREAM class and need processing. And it also stipulates that other classes that are born from the TSTREAM class must go to implement (such as READ, WRITE, etc.).

For example, TFileStream implements a TSTREAM class in a disk file application; TMEMORYSTREAM implements TSTREAM classes in memory applications. Now, it is assumed that there is a class TMYClass method to provide a SaveTroupTream:

TMYCLASS = Class (TOBJECT)

Procedure Savetostream (Stream: TStream);

END;

The application of polymorphism, can have such code:

VAR

Strm: TSTREAM;

Myclass: TMYCLASS;

Begin

STRM: = TFileStream.create ('abc.txt'); // ß This is the real instance type of Stream here is TFileStream

Myclass: = TMYCLASS.CREATE;

Myclass.savetostream (strM);

... ..

END;

To put the content of myclass to memory, just change

STRM: = TFileStream.create ('abc.txt');

for:

Strm: = TMEMORYSTREAM.CREATE

I.e.

The use of polymorphisms requires two works, and one of course is a polymorphism in the classes, which can provide an intermediate class (abstract class) that implements a function; second, it is understandable to use these middle class, this Work is reflected in the definition of the parameters of the function.

In addition, it is important, I want to remind everyone that the plan is very important, in the object-oriented program, the framework of the class determines the framework of the program, determines the success or failure of software development. The structure is clear, the classification of the class is not only easy to divide and expand, but also easier for code maintenance. In this, the application of inheritance and polymorphism, introducing an abstract class, introducing an intermediate class, is a more desirable approach. The abstract classes and specific classes provided in Delphi are listed below:

Abstract class derived specific class

TSTREAM TFILESTREAM, TMEMORYSTREAM;

TcustomIfile Tinifile, Tmeminifile, TregiStryiniFile;

Tstrings Tstringlist, Tmemostrings, TListBoxStrings

There are still a lot, waiting for you, I will discover. The most commonly used TSTREAM is the most surprisingly amazing that tcustomifile, its TregistryInifile allows you to access the registry with access to inIfile! This makes I can use a set of code to implement the function of writing the registry and writing the INI file. Although this technology is simple, its significance is unfounded!

(Until it, to be continued)

More articles

转载请注明原文地址:https://www.9cbs.com/read-5337.html

New Post(0)