正在加载图片...
$34.4 OBJECT-ORIENTED PROGRAMMING AND C 1109 Basics As with any other language,you can apply to C the"disciplinary"technique of restricted data access,requiring all uses of data structures to go through functions.(All routines in C are functions;procedures are viewed as functions with a"void"result type.) Beyond this,the notion of file may serve to implement higher-level modules.Files are a C notion on the borderline between the language and the operating system.A file is a compilation unit;it may contain a number of functions and some data.Some of the functions may be hidden from other files,and some made public.This achieves encapsulation:a file may contain all the elements pertaining to the implementation of one or more abstract objects,or an abstract data type.Thanks to this notion of file,you can essentially reach the encapsulation language level in C,as if you had Ada or Modula-2. As compared to Ada,however,you will be missing genericity and the distinction between specification and implementation parts. In practice,a commonly used C technique is rather averse to O-0 principles.Most C programs use "header files",which describe shared data structures.Any file needing the data structures will gain access to them through an"include"directive (handled by the built-in C preprocessor)of the form #include <header.h> where header.h is the name of the header file (.h is the conventional suffix for such file names).This is conceptually equivalent to copying the whole header file at the point where the directive appears,and allows the including file to access directly the data structure definitions of the header file.As a result the C tradition,if not the language itself, encourages client modules to access data structures through their physical representations, which clearly contradicts the principles of information hiding and data abstraction.It is possible,however,to use header files in a more disciplined fashion,enforcing rather than violating data abstraction;they can even help you go some way towards defining interface modules in the style we studied for Ada in the preceding chapter. Emulating objects Beyond the encapsulation level,one of the more specialized and low-level features of C -the ability to manipulate pointers to functions-can be used to emulate fairly closely some of the more advanced properties of a true O-O approach.Although it is sufficiently delicate to suggest that its proper use is by compilers for higher-level languages rather than C programmers,it does deserve to be known. In we take a superficial look at the notion of object as it exists in object technology, we might say that"every object has access to the operations applicable to it".This is a little naive perhaps,but not altogether wrong conceptually.If,however,we take this view literally,we find that C directly supports the notion!It is possible for an instance of a "structure type"ofC(the equivalent of record types in Pascal)to contain,among its fields, pointers to functions.§34.4 OBJECT-ORIENTED PROGRAMMING AND C 1109 Basics As with any other language, you can apply to C the “disciplinary” technique of restricted data access, requiring all uses of data structures to go through functions. (All routines in C are functions; procedures are viewed as functions with a “void” result type.) Beyond this, the notion of file may serve to implement higher-level modules. Files are a C notion on the borderline between the language and the operating system. A file is a compilation unit; it may contain a number of functions and some data. Some of the functions may be hidden from other files, and some made public. This achieves encapsulation: a file may contain all the elements pertaining to the implementation of one or more abstract objects, or an abstract data type. Thanks to this notion of file, you can essentially reach the encapsulation language level in C, as if you had Ada or Modula-2. As compared to Ada, however, you will be missing genericity and the distinction between specification and implementation parts. In practice, a commonly used C technique is rather averse to O-O principles. Most C programs use “header files”, which describe shared data structures. Any file needing the data structures will gain access to them through an “include” directive (handled by the built-in C preprocessor) of the form #include <header.h> where header.h is the name of the header file (.h is the conventional suffix for such file names). This is conceptually equivalent to copying the whole header file at the point where the directive appears, and allows the including file to access directly the data structure definitions of the header file. As a result the C tradition, if not the language itself, encourages client modules to access data structures through their physical representations, which clearly contradicts the principles of information hiding and data abstraction. It is possible, however, to use header files in a more disciplined fashion, enforcing rather than violating data abstraction; they can even help you go some way towards defining interface modules in the style we studied for Ada in the preceding chapter. Emulating objects Beyond the encapsulation level, one of the more specialized and low-level features of C — the ability to manipulate pointers to functions — can be used to emulate fairly closely some of the more advanced properties of a true O-O approach. Although it is sufficiently delicate to suggest that its proper use is by compilers for higher-level languages rather than C programmers, it does deserve to be known. In we take a superficial look at the notion of object as it exists in object technology, we might say that “every object has access to the operations applicable to it”. This is a little naïve perhaps, but not altogether wrong conceptually. If, however, we take this view literally, we find that C directly supports the notion! It is possible for an instance of a “structure type” of C (the equivalent of record types in Pascal) to contain, among its fields, pointers to functions
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有