您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes

Introduction

Content

Apply

Reflect

Extend

previous.gif
 (3087 bytes)

next.gif
 (2959 bytes)


Content Index

Content Page # 24

Attributes

Part of the definition of a class is the set of attributes for each object of the class. An attribute is some feature of an object that we wish to model. Examples of attributes for the LibraryItem class might be:

title
author(s)
publisher
loan status
borrower
loan duration
due date
loan fee
late return charge

An object of a class will have values for each of these attributes. For example the object for the copy of ‘Java How to Program’ in our imaginary public library might have the following values for the above attributes:

    • an object of LibraryItem class with title ‘Java How to Program’

Attribute

Value

title

Java How to Program

author(s)

H. M. Deitel
P. J. Deitel

publisher

Prentice-Hall International, Inc.

loan status

on loan

borrower 

Jim Jones

loan duration

2 weeks

due date

10 / Sep / 1999

loan fee

free

late return charge

0.05 pounds per day

A ‘Yellow Submarine’ CD object will have different values for its attributes, for example:

    • an object of LibraryItem class with title ‘Yellow Submarine’

Attribute

Value

title

Yellow Submarine

author(s)

The Beatles

publisher

K-Tel recordings 

loan status

on shelf

borrower 

 

loan duration

 

due date

 

loan fee

0.50 pounds per week

late return charge

0.25 pounds per day

Notice that in the above example, some attributes are empty. When writing computer programs to work with classes and objects, we must have some way to both store values for the attributes of an object of a class, and also to record when we have no value for an attribute.

(And remember, the attributes do not identify the objects in question.)

Taking the example Novel class described above, what attributes does an instance of Novel have? Here are some possibilities: author, date of writing, date of publication, number of pages, text. All these things may be information a Novel object might be responsible for. For a particular novel (that is, for an object of the Novel class) the attributes will have values. For example, you might expect an instance of Novel that had the value of its title ‘Great Expectations’ to have the value ‘Charles Dickens’ for its author attribute.

What operations does the Novel class have? That is, what names might we use to describe its behaviour. This rather depends on the application. The things that a library does with a novel will not be the same as, say, a publisher.

These ideas may seem a bit strange at first; you will not need to worry about them too much at first, as your programs will not be complex enough to need more than one class. However, when you start writing programs that have graphical user interfaces, you will need to ensure you have grasped the idea. In Java, user interface elements like buttons, windows, and menus are objects of the class Button, Window and Menu respectively.

 

A note on the values stored in the implementation of attributes

In a pure object-oriented language such as Smalltalk a value is a reference to an object. But in Java, which does not implement all object concepts in a ‘pure’ way, the implemented value of an attribute might be an object reference, or it might be a primitive value (such as an integer, or boolean true/false value).

Such questions become important when one begins to wish to implement object models in a particular programming language.

Back to top

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg