Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Tech-prog(theory).docx
Скачиваний:
0
Добавлен:
01.03.2025
Размер:
113.55 Кб
Скачать

13) Working with I / o system in the programming language Java

Java I/O Overview

To understand the issues associated with performing I/O in Java, it is necessary to briefly review the Java

I/O model.

When discussing Java I/O, it is worth noting that the Java programming language assumes two distinct

types of disk file organization. One is based on streams of bytes, the other on character sequences. Byteoriented I/O includes bytes, integers, floats, doubles and so forth; text-oriented I/O includes characters and

text. In the Java language a character is represented using two bytes, instead of the one byte representation

in C/C++. Because of this, some translation is required to handle characters in file I/O. In this project, since our major concern is to compare Java I/O to that of C/C++, we will focus on the byte-oriented I/O.

In Java, byte-oriented I/O is handled by input streams and output streams, where a stream is an ordered

sequence of bytes of unknown length. Java provides a rich set of classes and methods for operating on byte input and output streams. These classes are hierarchical, and at the base of this hierarchy are the abstract classes InputStream and OutputStream. It is useful to briefly discuss this class hierarchy in order to clarify the reason why we are interested in FileInputStream/FileOutputStream, BufferedInputStream/

BufferedOutputStream, and RandomAccessFile in our test cases. Figure 2.1 provides a graphical

representation of this I/O hierarchy. Note that we have not included every class that deals with byteoriented I/O but only those classes that are pertinent to our discussion.

2.1 InputStream and OutputStream Classes

The abstract classes InputStream and OutputStream are the foundation for all input and output streams.

They define methods for reading/writing raw bytes from/to streams. For example, the InputStream class provides methods for reading a single byte, a byte array, or reading the available data into a particular region of a byte array. The OutputStream class provides methods for writing that are analogous to those of InputStream.

14. Definition and declaration of class methods in the programming language C++.

Each function that you declare for your class must have a definition. The definition is also called the function implementation. Like other functions, the definition of a class method has a function header and a function body.

The definition must be in a file that the compiler can find. Most C++ compilers want that file to end with.C or .CPP. This book uses .CPP, but check your compiler to see what it prefers.

The declaration of a class tells the compiler what the class is, what data it holds, and what functions it has. The declaration of the class is called its interface because it tells the user how to interact with the class. The interface is usually stored in an .HPP file, which is referred to as a header file. The function definition tells the compiler how the function works. The function definition is called the implementation of the class method, and it is kept in a .CPP file. The implementation details of the class are of concern only to the author of the class. Clients of the class--that is, the parts of the program that use the class--don't need to know, and don't care, how the functions are implemented.

15. Access specifiers: public, private and protected in the programming language C++.

Access labels

The access labels PublicProtected and Private are used within classes to set access permissions for the members in that section of the class. All class members are initiallyprivate by default. The labels can be in any order. These labels can be used multiple times in a class declaration for cases where it is logical to have multiple groups of these types. An access label will remain active until another access label is used to change the permissions.

We have already mentioned that a class can have member functions "inside" it; we will see more about them later. Those member functions can access and modify all the data and member function that are inside the class. Therefore, permission labels are to restrict access to member function that reside outside the class and for other classes.

public

This label indicates any members within the 'public' section can accessed freely anywhere a declared object is in scope

Note: Avoid declaring public data members, since doing so would contribute to create unforeseen disasters.

private

Members defined as private are only accessible within the class defining them, or friend classes. Usually the domain of member variables and helper functions. It's often useful to begin putting functions here and then moving them to the higher access levels as needed so to reduce complexity.

Note: It's often overlooked that different instances of the same class may access each others' private or protected variables. A common case for this is in copy constructors.

protected

The protected label has a special meaning to inheritance, protected members are accessible in the class that defines them and in classes that inherit from that base class, or friends of it. In the section on inheritance we will see more about it.

Note: Other instances of the same class can access a protected field - provided the two classes are of the same type. However, an instance of a child class cannot access a protected field or method of an instance of a parent class.

16. Using references in the programming language C++..

References are usually preferred over pointers whenever you don't need "reseating". This usually means that references are most useful in a class's public interface. References typically appear on the skin of an object, and pointers on the inside.

The exception to the above is where a function's parameter or return value needs a "sentinel" reference — a reference that does not refer to an object. This is usually best done by returning/taking a pointer, and giving the NULL pointer this special significance (references must always alias objects, not a dereferenced NULL pointer).

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]