3.11 Record
When a number of data items are chunked together into a unit, the result is a data structure. Data structures can have very complex structure, but in many applications, the appropriate data structure consists simply of a sequence of data items. Data structures of this simple variety can be either arrays or records.
The term “record” is not used in Java. A record is essentially the same as a Java object that has instance variables only, but no instance methods. Some other languages, which do not support objects in general, nevertheless do support records. The C programming language, for example, is not object-oriented, but it has records, which in C go by the name “struct.” The data items in a record —in Java, an object’s instance variables—are called the fields of the record. Each item is referred to using a field name. In Java, field names are just the names of the instance variables. The distinguishing characteristics of a record are that the data items in the record are referred to by name and that different fields in a record are allowed to be of different types. For example, if the class Person is defined as:
record在Java中没有被使用。本质上讲,一个记录与Java中的对象实例是一样的,但是它没有实例的方法。在Java中,字段名正好就是实例变量的名字。区分一个记录中的字段是通过记录中的数据项名字来实现的,并且记录中不同的字段允许有不同的类型。
class Person { String name; int id_number; Date birthday; int age; }
then an object of class Person could be considered to be a record with four fields. The field names are name, id_number, birthday, and age. Note that the fields are of various types: String, int, and Date.
Because records are just a special type of object, I will not discuss them further.