Delphi Object-Oriented learning essay one: The relationship between classes and objects
Bahamut
http://www.cnpack.org
(reproduced please specify the source and remain intact)
Work for several years, always want to do some summary, so with this article, called Essays it Ah
This article only writes my understanding to the object, the subjective component is many, may have the mistake, hoped that everybody corrected ^_^
PS: The demo code in this article is based on Delphi's Object Pascal syntax.
Class-to-object relationships
To discuss the relationship between classes and objects, you need to first say what is a class and what is an object.
class :
The class, I think, is a collection, like a set of mathematics, that is a general term for a class of things, such as "human beings".
In computer languages, data types such as classes and arrays or structs are User (programmer) custom data types, but we often say that classes are complex custom types, while others are called simple custom types. The reason is because the class has behavior!
In addition, the class is abstract existence, take "The human", we all know, in this world is the existence human, but who can say clearly "human" what looks like? How many?
Object :
The object is an individual in the class, such as me or you, who are looking at this article, as an individual in the human race.
In computer language, you can't have a class work directly for you (except for some special cases with class methods), just as no one in the world has the ability to make the whole "human" work for you; So to make your class work for you, we need an object of this class, and when we have an object, Then we can make him do what we want to do for us.
Instantiating an object:
As we all know, in the current computer operating system, any software operation requires a memory block, that is, the living space. Similarly, the object must be, as we are born to occupy the same place. Before you make a class object work for you, you need to allocate a memory space for it and create it, which is not the same as the other data types:
var i: Integer; //定义一个整形变量i s: TStringList; //定义一个TStringList类的对象sbegin i:= 0; { 这是对的,因为当定义了一个int类型(简单类型)的变量后, 编译器会自动为你这个变量分配内存空间 } s.LoadFromFile(...); { 错误,因为定义这个对象前,还没有为他创建内存空间, 直接这样执行会引起一个内存错误 }end; |
The correct approach should be to construct the calling class to be displayed before using the object of the class:
CODE: s:= TStringList.Create; |
Maybe some friends would say, why not S. What about create? That is because, before executing this code, there is no memory space allocated for S, and it is not possible to access his internal data members, which says, "The class is abstract", so after the compiler has packaged your code as an executable file, Tstringlist already exists in memory. Of course, this "class name. Method Name" is invoked only on the class (static) method, and the entry of the constructor of the class must exist statically.
properties, methods, events :
Properties are attributes of a class: for example, a red car, red is the color attribute of the car.
A method is a function or process in our traditional sense, which is what a class can do: For example, a car can move forward or it can be pulled back.
An event is a code snippet that is passively invoked under certain conditions, that is, the code we specify in the event may not be called manually, but it may be executed within a specific condition by the object's internal invocation (not excluding the manual call you have shown).
For the time being here, apply a storytelling in the words: "How to predict the funeral, and listen to tell" ^_^
Delphi Object-oriented learning (-)